简体   繁体   English

MinGW 32/64 不能为 32 位编译

[英]MinGW 32/64 not compile for 32 bit

I've coded a DLL for usage with LabView 2020 (32-bit).我编码了一个 DLL 用于 LabView 2020(32 位)。 Developed in Code::Blocks IDE v20.03, with few days old installed Win-Builds (32/64 bit mingw tools g++ and gcc).Code::Blocks IDE v20.03 中开发,几天前安装了Win-Builds (32/64 位 mingw 工具 g++ 和 gcc)。

DLL is compiled well as 64-bit, works with Windows (VBA testing app, and rundll). DLL 编译为 64 位,适用于 Windows(VBA 测试应用程序和 rundll)。 Compilation for 32-bit is not successful, ends with errors. 32 位编译不成功,以错误结束。

In forums I read to use "-m32" parameter for compiling.在论坛上我读到使用“-m32”参数进行编译。

How can I do it?我该怎么做?

Compilation logs 64bit:编译日志 64 位:

-------------- Build: Release in SampleDLL1 (compiler: GNU GCC Compiler)---------------
g++.exe -Wall -DBUILD_DLL -O2  -c C:\Hello\SampleDLL1\main.cpp -o obj\Release\main.o
g++.exe -shared -Wl,--output-def=bin\Release\libSampleDLL1.def -Wl,--out-implib=bin\Release\libSampleDLL1.a -Wl,--dll  obj\Release\main.o  -o bin\Release\SampleDLL1.dll -s -luser32
Output file is bin\Release\SampleDLL1.dll with size 18.50 KB
Process terminated with status 0 (0 minute(s), 2 second(s))
0 error(s), 0 warning(s) (0 minute(s), 2 second(s))

*Compilation logs 32bit : (uses -m32 parameter and -lmingw32 for linker)
ERROR : some parameters are "cannot find"*
-------------- Build: Release in SampleDLL1 (compiler: GNU GCC Compiler)---------------
g++.exe -Wall -DBUILD_DLL -O2 -m32  -c C:\Hello\SampleDLL1\main.cpp -o obj\Release\main.o
g++.exe -shared -Wl,--output-def=bin\Release\libSampleDLL1.def -Wl,--out-implib=bin\Release\libSampleDLL1.a -Wl,--dll  obj\Release\main.o  -o bin\Release\SampleDLL1.dll -s -m32 -lmingw32  -luser32
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64/libstdc++.dll.a when searching for -lstdc++
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64/libstdc++.a when searching for -lstdc++
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64\libstdc++.a when searching for -lstdc++
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64/libstdc++.dll.a when searching for -lstdc++
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64/libstdc++.a when searching for -lstdc++
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lstdc++
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64/libgcc_s.a when searching for -lgcc_s
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64\libgcc_s.a when searching for -lgcc_s
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64/libgcc_s.a when searching for -lgcc_s
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgcc_s
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/libgcc.a when searching for -lgcc
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3\libgcc.a when searching for -lgcc
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/libgcc.a when searching for -lgcc
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgcc
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/lib/libpthread.dll.a when searching for -lpthread
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/lib/libpthread.a when searching for -lpthread
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/lib\libpthread.a when searching for -lpthread
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/lib/libpthread.dll.a when searching for -lpthread
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/lib/libpthread.a when searching for -lpthread
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lpthread
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64/libgcc_s.a when searching for -lgcc_s
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64\libgcc_s.a when searching for -lgcc_s
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible C:/Win-builds/lib64/libgcc_s.a when searching for -lgcc_s
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgcc_s
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/libgcc.a when searching for -lgcc
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3\libgcc.a when searching for -lgcc
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping incompatible c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/libgcc.a when searching for -lgcc
c:/win-builds/bin/../lib64/gcc/x86_64-w64-mingw32/4.8.3/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgcc
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
7 error(s), 0 warning(s) (0 minute(s), 1 second(s))
 

That typically means that you are trying a to build a 32 bit binary with a 64 bit compiler.这通常意味着您正在尝试使用 64 位编译器构建 32 位二进制文件。 MinGW-w64 that comes with Win-Builds (assuming you are using the latest version 1.5) is v3.3.0 , uses GCC-4.8.3 . Win-Builds附带的MinGW-w64 (假设您使用的是最新版本 1.5)是v3.3.0 ,使用GCC-4.8.3 As I recall, both DWARF and SEH variants of MinGW-w64 (available starting GCC-4.8.0) are single-target, ie, they contain only the libraries for either 64-bit or 32-bit but not both.我记得, MinGW-w64DWARFSEH变体(从 GCC-4.8.0 开始可用)都是单目标的,即它们只包含 64 位或 32 位的库,但不能同时包含两者。 Only the old SJLJ version remains actual dual target and for that supplying -m32 would work.只有旧的SJLJ版本仍然是实际的双重目标,为此提供-m32将起作用。 Given the fact that 64-bit builds are working fine for you and 32-bit builds are failing, it seems you are the former case.鉴于 64 位构建对您来说工作正常而 32 位构建失败,看来您是前一种情况。 You might have to build it using the 32-bit toolchain for the 32 bit compilation to work ( i686-w64-mingw32-g++ and 32 bit libraries).您可能必须使用 32 位工具链构建它才能使 32 位编译工作( i686-w64-mingw32-g++和 32 位库)。

I was just having the same problem as you and this is what I did to solve it.我只是遇到了和你一样的问题,这就是我为解决它所做的。

First I studied this answer首先我研究了这个答案

I had already installed MSYS2 from here but I had only installed 64 bits using:我已经从这里安装了 MSYS2,但我只安装了 64 位,使用:

pacman -S mingw-w64-ucrt-x86_64-gcc

The first thing is updating the MSYS2 installation and all its packages and dependencies.第一件事是更新 MSYS2 安装及其所有包和依赖项。 To do this on the console of msys2.exe type:要在 msys2.exe 类型的控制台上执行此操作:

pacman -Suy

end then然后结束

pacman -Syyu

Then for compiling 32 bits you have to install:然后编译 32 位你必须安装:

pacman -S mingw-w64-i686-gcc

Then you have to add this new version to your environment variables in the path variable.然后,您必须将这个新版本添加到路径变量中的环境变量中。 On my PC the path was C:\msys64\mingw32\bin .在我的电脑上,路径是C:\msys64\mingw32\bin I momentarily took the 64 bit gcc version in C:\msys64\mingw64\bin out of the path我暂时将C:\msys64\mingw64\bin中的 64 位 gcc 版本移出了路径

Finally to compile the library (I wanted a.SO) in 32 bits I did:最后,为了以 32 位编译库(我想要 a.SO),我做了:

gcc -m32 -fPIC -shared -o my_lib_name_x86.so my_source_code.c

And in my case I can load this using ctypes.cdll.LoadLibrary("./my_lib_name_x86.so") from Python 32 bits interpreter (which I momentarily use as replacement for the default 64 bit interpreter)在我的例子中,我可以使用ctypes.cdll.LoadLibrary("./my_lib_name_x86.so")从 Python 32 位解释器加载它(我暂时将其用作默认 64 位解释器的替代品)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM