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

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 I am using the MinGW32 compiler.

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 …

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`
        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

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

/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.

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

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.

1 Like

If you are compiling for 64-bit architecture, you could try to use mingw-w64.
There is mentioned on MinGW releases site that they are offering a 32-bit compiler only.

1 Like

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