Building 64 bit dll with MinGW 32 bit in Eclipse

2020-02-05 09:40发布

问题:

I installed the 32 bit version of Mingw 4.7.2 (using the installer) on my Windows 7 64 bit. I use MinGW in an Eclipse C++ project in order to build a .dll file. So far everything works.

However I use this .dll to be included in a java project via JNI. And when I call a function of the .dll in the java project the exception "Exception in thread "main" java.lang.UnsatisfiedLinkError: D:\path\mylib.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform" is thrown. So it seem like I need to build an 64 bit version of the DLL.

So my questions are:

  1. Can I build an 64 bit DLL with MinGW 32 bit or do I need the MinGW 64 bit version for that?
  2. If yes how do I need to adjust the MinGW and/or Eclipse Settings to do so?

Regards Marc

Edit: As you can see in the comment below, I already tried to set the -m64 Flag to build a 64 bit dll. This results in the error: "sorry, unimplemented: 64-bit mode not compiled in". So is there a way to get the 64 bit mode running in mingw32.

回答1:

I recently faced the same problem, installing the MinGW-64 version enabled the -m64 flag for me. You can get an automated build from here.

EDIT : Some guy (rubenvb) made some good job in the Personal Builds :

There's gcc 4.7.4 here and even 4.8.0 here.



回答2:

You can download the TDM-GCC compiler with a nice easy Windows installation from http://tdm-gcc.tdragon.net/.

Then you can run the following to generate a 64-bit C Code Object file from the C Code Source File HelloWorld.c.

"C:\MinGW64\bin\gcc.exe" -m64 -c -I"C:\Program Files\Java\jdk1.6.0_26\include" -I"C:\Program Files\Java\jdk1.6.0_26\include\win32" HelloWorld.c 

This should be run from the same directory as HelloWorld.c and will generate the HelloWorld.o file in that directory. The -m64 makes it 64 bit. You can specify -m32 to make a 32 bit object file and specify -o, to give the output as mentioned in the comment above.



回答3:

You can set -m64 switch to compile a 64 bit lib.

These ‘-m’ switches are supported in addition to the above on x86-64 processors in 64-bit environments.

-m32 -m64 -mx32 Generate code for a 32-bit or 64-bit environment. The -m32 option sets int, long, and pointer types to 32 bits, and generates code that runs on any i386 system.

The -m64 option sets int to 32 bits and long and pointer types to 64 bits, and generates code for the x86-64 architecture. For Darwin only the -m64 option also turns off the -fno-pic and -mdynamic-no-pic options.

The -mx32 option sets int, long, and pointer types to 32 bits, and generates code for the x86-64 architecture.

( source: http://gcc.gnu.org/onlinedocs/gcc/i386-and-x86_002d64-Options.html )

Example:

32 Bit: gcc -m32 -o exmaple32 example.c

64 Bit: gcc -m64 -o exmaple64 example.c

(same with g++)

You can set them in eclipse: (right click on your project) -> Properties -> C/C++ Build -> Settings