Windows+MinGW编译生成无libgcc_s_dw2-1.dll,libstdc++-6.dll,libwinpthread-1.dll依赖的终极完美解决方案:
CMakeFiles.txt中添加:
set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc -static-libstdc++")
然后执行cmake命令:
===============编译32位=============
mkdir build32
cd build32
cmake -G "MinGW Makefiles" .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS="-m32 -static -static-libgcc -static-libstdc++" -DCMAKE_CXX_FLAGS="-m32 -static -static-libgcc -static-libstdc++"
mingw32-make && mingw32-make install
或
cmake -G "Unix Makefiles" .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS="-m32 -static -static-libgcc -static-libstdc++" -DCMAKE_CXX_FLAGS="-m32 -static -static-libgcc -static-libstdc++"
mingw32-make && mingw32-make install
===============编译64位=============
mkdir build64
cd build64
cmake -G "MinGW Makefiles" .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS="-m64 -static -static-libgcc -static-libstdc++" -DCMAKE_CXX_FLAGS="-m64 -static -static-libgcc -static-libstdc++"
mingw32-make && mingw32-make install
或
cmake -G "Unix Makefiles" .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS="-m64 -static -static-libgcc -static-libstdc++" -DCMAKE_CXX_FLAGS="-m64 -static -static-libgcc -static-libstdc++"
mingw32-make && mingw32-make install
FAQ:
1.如果出现编译错误:
undefined reference to `WinMain@16
解决方案:
cmake 命令行去掉“-static-libstdc++”标识
错误原因:
如果有效,请检查源代码是否为C代码,故只需“-static-libgcc”标识即可。无需“-static-libstdc++”标识