# Not able to use .dll in swi-prolog.('$open\_shared\_object'/3: %1 is not a valid Win32 application.)

**URL:** <https://swi-prolog.discourse.group/t/not-able-to-use-dll-in-swi-prolog-open-shared-object-3-1-is-not-a-valid-win32-application/1936>\
**Category:** Help!\
**Created:** [February 28, 2020, 1:39pm UTC](https://swi-prolog.discourse.group/t/not-able-to-use-dll-in-swi-prolog-open-shared-object-3-1-is-not-a-valid-win32-application/1936 "2020-02-28T13:39:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xxjunxx](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/xxjunxx/32/923_2.png) [@xxjunxx](https://swi-prolog.discourse.group/u/xxjunxx)\
**Post date:** [February 28, 2020, 1:39pm UTC](https://swi-prolog.discourse.group/t/not-able-to-use-dll-in-swi-prolog-open-shared-object-3-1-is-not-a-valid-win32-application/1936/1 "2020-02-28T13:39:42Z")

</div>

I compiled the cpp file to .dll using the command:

> swipl-ld -c test.cpp test.o -shared -o test test.o

When I tried to use the dll file in swi-prolog, I got this error.

> :- use\_foreign\_library(test).  
> Initialization goal raised exception:  
> ‘$open\_shared\_object’/3: %1 is not a valid Win32 application.

The c++ code is from [GitHub - SWI-Prolog/packages-cpp: The SWI-Prolog C++ interface](https://github.com/SWI-Prolog/packages-cpp). I am using the MinGW32 compiler.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 28, 2020, 3:31pm UTC](https://swi-prolog.discourse.group/t/not-able-to-use-dll-in-swi-prolog-open-shared-object-3-1-is-not-a-valid-win32-application/1936/2 "2020-02-28T15:31:57Z")

</div>

The command should be

```
swipl-ld -o test -shared test.cpp

```

which should produce `test.dll`. A lot of stuff can go wrong though, especially on Windows. First, add `-v` to the above options and check it actually calls the right mingw-gcc and you create a matching dll (32/64 bit).

The above works flawlessly on Linux …

---

<div class="post-metadata">

**Author:** ![xxjunxx](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/xxjunxx/32/923_2.png) [@xxjunxx](https://swi-prolog.discourse.group/u/xxjunxx)\
**Post date:** [February 28, 2020, 3:51pm UTC](https://swi-prolog.discourse.group/t/not-able-to-use-dll-in-swi-prolog-open-shared-object-3-1-is-not-a-valid-win32-application/1936/3 "2020-02-28T15:51:38Z")

</div>

when I run this command, such error happen. So I changed the command. I also tried compile in Visual Studio and link using swipl-ld. As I also try to link using Visual Studio, I can’t find swipl.lib and swipl.dll

```
PS C:\Xiaojun\packages-cpp> swipl-ld -o test -shared test.cpp -v 
        eval `swipl.exe --dump-runtime-variables`
                PLBASE="c:/xiaojun/swipl"
                PLARCH="x64-win64"
                PLSOEXT="dll"
                PLLIBDIR="c:/xiaojun/swipl/bin"
                PLLIB="-lswipl"
                PLTHREADS="yes"
        g++.exe -c -D_REENTRANT -D __WINDOWS__ -D_WINDOWS -D __SWI_PROLOG__ -Ic:/xiaojun/swipl/include -o test.obj test.cpp
        g++.exe -o test.dll -shared test.obj -Lc:/xiaojun/swipl/bin -lswipl
Preformatted text`Preformatted text c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: c:/xiaojun/swipl/bin/libswipl.dll.a: error adding symbols: file format not recognized
collect2.exe: error: ld returned 1 exit status
g++.exe returned code 1
        rm test.obj
*** C:\Xiaojun\swipl\bin\swipl-ld.exe exit status 1
```

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [February 28, 2020, 5:21pm UTC](https://swi-prolog.discourse.group/t/not-able-to-use-dll-in-swi-prolog-open-shared-object-3-1-is-not-a-valid-win32-application/1936/4 "2020-02-28T17:21:09Z")

</div>

Bit odd. Compiling by hand using Linux (swipl-ld.exe doesn’t work in the emulated environement for several reasons), I get

```prolog
/usr/bin/x86_64-w64-mingw32-g++-win32 -I home/include/ -c ../packages/cpp/test.cpp 
/usr/bin/x86_64-w64-mingw32-g++-win32 -o test.dll -shared -lswipl -L src

```

Which nicely creates `test.dll`. SWI-Prolog can also load it, but it refuses to work as the predicates are not installed. Possibly something with handling C++ constructors? I’m mostly on alien territory here. Possibly some Windows user knows how to do this.

I think it is also possible to create a .lib file, but I’m not sure how. This has changed several times.

---

<div class="post-metadata">

**Author:** ![johedan20012](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/johedan20012/32/3104_2.png) [@johedan20012](https://swi-prolog.discourse.group/u/johedan20012)\
**Post date:** [November 19, 2020, 6:06am UTC](https://swi-prolog.discourse.group/t/not-able-to-use-dll-in-swi-prolog-open-shared-object-3-1-is-not-a-valid-win32-application/1936/5 "2020-11-19T06:06:51Z")

</div>

Any luck?, i’m stuck with the same problem

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [November 19, 2020, 7:56am UTC](https://swi-prolog.discourse.group/t/not-able-to-use-dll-in-swi-prolog-open-shared-object-3-1-is-not-a-valid-win32-application/1936/6 "2020-11-19T07:56:26Z")

</div>

The latest versions come with libswipl.lib, which is actually a mere copy of libswipl.dll.a from MinGW. This is reported to work with MSVC.

---

<div class="post-metadata">

**Author:** ![mjano314](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/mjano314/32/2621_2.png) [@mjano314](https://swi-prolog.discourse.group/u/mjano314)\
**Post date:** [November 19, 2020, 8:43am UTC](https://swi-prolog.discourse.group/t/not-able-to-use-dll-in-swi-prolog-open-shared-object-3-1-is-not-a-valid-win32-application/1936/7 "2020-11-19T08:43:24Z")

</div>

If you are compiling for 64-bit architecture, you could try to use [mingw-w64](http://mingw-w64.org/).  
There is mentioned on [MinGW releases site](https://osdn.net/projects/mingw/releases/) that they are offering a 32-bit compiler only.

---

<div class="post-metadata">

**Author:** ![johedan20012](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/johedan20012/32/3104_2.png) [@johedan20012](https://swi-prolog.discourse.group/u/johedan20012)\
**Post date:** [November 19, 2020, 3:46pm UTC](https://swi-prolog.discourse.group/t/not-able-to-use-dll-in-swi-prolog-open-shared-object-3-1-is-not-a-valid-win32-application/1936/8 "2020-11-19T15:46:18Z")

</div>

Thanks, i had to update swi, download mingw-64 and now it works
