C++ Eclipse 生成无效的 exe

C++ Eclipse generates invalid exe

我正在使用 C++ Eclipse 和 MinGW/MSYS 生态系统来编译我的 C++ 程序。我在系统变量中为 MinGW/MSYS 设置了正确的路径。在构建 EXE 时,也会创建一个名为 LibTest 的 EXE,但格式为 x86/le。当我 运行 这个 EXE 时,我得到以下错误:

CreateProcess error=193, %1 is not a valid Win32 application

我在创建的 EXE 上使用了 Dependency walker,但出现了以下错误

Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: Modules with different CPU types were found.

我在下面附上构建日志:

**** Build of configuration Debug for project Test ****

make -k all 
Building file: ../src/Test.cpp
Invoking: GCC C++ Compiler
g++ -I"C:\MinGW\include" -O0 -g3 -Wall -c -fmessage-length=0 -v -MMD -MP -MF"src/Test.d" -MT"src/Test.d" -o"src/Test.o" "../src/Test.cpp"
Using built-in specs.
COLLECT_GCC=c:\MinGW\bin\g++.exe
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=mingw32 --without-pic --enable-shared --enable-static --with-gnu-ld --enable-lto --enable-libssp --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gmp-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --with-mpfr= --with-system-zlib --with-gnu-as --enable-decimal-float=yes --enable-libgomp --enable-threads --with-libiconv-prefix=/mingw32 --with-libintl-prefix=/mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIME_T
Thread model: win32
gcc version 4.8.1 (GCC) 
COLLECT_GCC_OPTIONS='-I' 'C:\MinGW\include' '-O0' '-g3' '-Wall' '-c' '-fmessage-length=0' '-v' '-MMD' '-MP' '-MF' 'src/Test.d' '-MT' 'src/Test.d' '-o' 'src/Test.o' '-shared-libgcc' '-mtune=generic' '-march=pentiumpro'
 c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/cc1plus.exe -quiet -v -I C:\MinGW\include -iprefix c:\mingw\bin\../lib/gcc/mingw32/4.8.1/ -MMD src/Test.d -MF src/Test.d -MP -MT src/Test.d -dD ../src/Test.cpp -quiet -dumpbase Test.cpp -mtune=generic -march=pentiumpro -auxbase-strip src/Test.o -g3 -O0 -Wall -version -fmessage-length=0 -o C:\Users\abc\AppData\Local\Temp\ccPn8RrE.s
GNU C++ (GCC) version 4.8.1 (mingw32)
    compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include/c++"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include/c++/mingw32"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include/c++/backward"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../../../../include"
ignoring duplicate directory "/mingw/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/include-fixed"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/4.8.1/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
ignoring duplicate directory "C:\MinGW\include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include/c++
 c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include/c++/mingw32
 c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include/c++/backward
 c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include
 c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../include
 c:\mingw\bin\../lib/gcc/mingw32/4.8.1/include-fixed
 c:\mingw\bin\../lib/gcc/mingw32/4.8.1/../../../../mingw32/include
End of search list.
GNU C++ (GCC) version 4.8.1 (mingw32)
    compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 1ebc2a6f92fbd3aadc367a20a63fdf9f
COLLECT_GCC_OPTIONS='-I' 'C:\MinGW\include' '-O0' '-g3' '-Wall' '-c' '-fmessage-length=0' '-v' '-MMD' '-MP' '-MF' 'src/Test.d' '-MT' 'src/Test.d' '-o' 'src/Test.o' '-shared-libgcc' '-mtune=generic' '-march=pentiumpro'
 c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/as.exe -v -I C:\MinGW\include -o src/Test.o C:\Users\abc\AppData\Local\Temp\ccPn8RrE.s
GNU assembler version 2.24 (mingw32) using BFD version (GNU Binutils) 2.24
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/4.8.1/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../
COLLECT_GCC_OPTIONS='-I' 'C:\MinGW\include' '-O0' '-g3' '-Wall' '-c' '-fmessage-length=0' '-v' '-MMD' '-MP' '-MF' 'src/Test.d' '-MT' 'src/Test.d' '-o' 'src/Test.o' '-shared-libgcc' '-mtune=generic' '-march=pentiumpro'
Finished building: ../src/Test.cpp

Building target: libTest.exe
Invoking: MinGW C++ Linker
g++ -shared -o"libTest.exe"  ./src/Test.o   
Finished building target: libTest.exe

似乎正在挑选一些 32 位库。

但是,如果我使用命令行编译,则会生成正确的 EXE。

看来问题出在真正使用 -shared 上。 -shared 用于动态 link 库(.DLLs)。对于构建应用程序 (.EXE),您不应该使用它。 (我说的是你的 makefile 中的最后一条命令:g++ -shared -o"libTest.exe" ./src/Test.o

一样建造它
g++ -o "libTest.exe"  ./src/Test.o