提升日志链接错误(Cmake Mingw32)

Boost log linking errors (Cmake Mingw32)

我有这个 cmake 文件:

cmake_minimum_required(VERSION 3.6)

find_package(Boost 1.54.0 REQUIRED COMPONENTS log)

add_executable(TestProject main.cpp)

target_include_directories(TestProject PUBLIC ${Boost_INCLUDE_DIRS})
target_link_libraries(TestProject Boost::log)
add_definitions("-DBOOST_ALL_NO_LIB") # Disable boost auto-linking.
add_definitions("-DBOOST_LOG_DYN_LINK") # Avoid linking errors with boost::log

如果我使用 Visual Studio 作为 Visual-Studio 开发者控制台的生成器构建它,我可以构建得很好。但是,在我的 MinGW32 环境中,出现链接器错误。我可以确认 boost 已安装在我的 mingw32 环境中,并且在其他项目(使用其他 boost 组件)中运行良好。

stewart@PC-1234 MINGW32 ~/src/testproj/buildmsys
$ tree /mingw32 -f | grep libboost_log
│   ├── /mingw32/bin/libboost_log_setup-mt.dll
│   ├── /mingw32/bin/libboost_log-mt.dll
│   ├── /mingw32/lib/libboost_log_setup-mt.a
│   ├── /mingw32/lib/libboost_log_setup-mt.dll.a
│   ├── /mingw32/lib/libboost_log-mt.a
│   ├── /mingw32/lib/libboost_log-mt.dll.a

我构建:

$ mkdir buildmsys && cd buildmsys
$ cmake .. -GNinja
$ cmake --build .

这是输出

stewart@PC-1234 MINGW32 ~/src/TestProject/buildmsys
$ cmake .. -GNinja
-- Boost version: 1.63.0
-- Found the following Boost libraries:
--   log
--   date_time
--   log_setup
--   system
--   filesystem
--   thread
--   regex
--   chrono
--   atomic
-- Configuring done
-- Generating done
-- Build files have been written to: 
C:/msys64/home/stewart/src/TestProject/buildmsys

stewart@PC-1234 MINGW32 ~/src/TestProject/buildmsys
$ cmake --build .
[1/1] Linking CXX executable TestProject.exe
FAILED: TestProject.exe
cmd.exe /C "cd . && C:\msys64\mingw32\bin\c++.exe CMakeFiles/TestProject.dir/main.cpp.obj  -o TestProject.exe -Wl,--major-image-version,0,--minor-image-version,0 C:/msys64/mingw32/lib/libboost_log-mt.dll.a C:/msys64/mingw32/lib/libboost_log_setup-mt.dll.a C:/msys64/mingw32/lib/libboost_filesystem-mt.dll.a C:/msys64/mingw32/lib/libboost_thread-mt.dll.a C:/msys64/mingw32/lib/libboost_date_time-mt.dll.a C:/msys64/mingw32/lib/libboost_regex-mt.dll.a C:/msys64/mingw32/lib/libboost_chrono-mt.dll.a C:/msys64/mingw32/lib/libboost_system-mt.dll.a C:/msys64/mingw32/lib/libboost_atomic-mt.dll.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
CMakeFiles/TestProject.dir/main.cpp.obj:main.cpp:(.text+0x151): undefined reference to `_imp___ZN5boost3log9v2_mt_nt64core3getEv'
CMakeFiles/TestProject.dir/main.cpp.obj:main.cpp:(.text+0x2b5): undefined reference to `_imp___ZN5boost3log9v2_mt_nt64core10set_filterERKNS1_6filterE'
CMakeFiles/TestProject.dir/main.cpp.obj:main.cpp:(.text+0x35a): undefined reference to `_imp___ZN5boost3log9v2_mt_nt64core3getEv'
CMakeFiles/TestProject.dir/main.cpp.obj:main.cpp:(.text+0x4e2): undefined reference to `_imp___ZN5boost3log9v2_mt_nt64core10set_filterERKNS1_6filterE'
CMakeFiles/TestProject.dir/main.cpp.obj:main.cpp:(.text+0x53c): undefined reference to `_imp___ZN5boost3log9v2_mt_nt67trivial6logger3getEv'
CMakeFiles/TestProject.dir/main.cpp.obj:main.cpp:(.text+0x5a2): undefined reference to `_imp___ZN5boost3log9v2_mt_nt67trivial6logger3getEv'
CMakeFiles/TestProject.dir/main.cpp.obj:main.cpp:(.text$_ZN5boost3log9v2_mt_nt614attribute_nameC1EPKc[__ZN5boost3log9v2_mt_nt614attribute_nameC1EPKc]+0x10): undefined reference to `_imp___ZN5boost3log9v2_mt_nt614attribute_name18get_id_from_stringEPKc'
CMakeFiles/TestProject.dir/main.cpp.obj:main.cpp:(.text$_ZNK5boost3log9v2_mt_nt615value_extractorINS1_7trivial14severity_levelENS1_16fallback_to_noneENS3_3tag8severityEEclERKNS1_14attribute_nameERKNS1_19attribute_value_setE[__ZNK5boost3log9v2_mt_nt615value_extractorINS1_7trivial14severity_levelENS1_16fallback_to_noneENS3_3tag8severityEEclERKNS1_14attribute_nameERKNS1_19attribute_value_setE]+0x19): undefined reference to `_imp___ZNK5boost3log9v2_mt_nt619attribute_value_set4findENS1_14attribute_nameE'
CMakeFiles/TestProject.dir/main.cpp.obj:main.cpp:(.text$_ZNK5boost3log9v2_mt_nt615value_extractorINS1_7trivial14severity_levelENS1_16fallback_to_noneENS3_3tag8severityEEclERKNS1_14attribute_nameERKNS1_19attribute_value_setE[__ZNK5boost3log9v2_mt_nt615value_extractorINS1_7trivial14severity_levelENS1_16fallback_to_noneENS3_3tag8severityEEclERKNS1_14attribute_nameERKNS1_19attribute_value_setE]+0x2e): undefined reference to `_imp___ZNK5boost3log9v2_mt_nt619attribute_value_set3endEv'
CMakeFiles/TestProject.dir/main.cpp.obj:main.cpp:(.text$_ZNK5boost3log9v2_mt_nt615value_extractorINS1_7trivial14severity_levelENS1_16fallback_to_noneENS3_3tag8severityEEclERKNS1_14attribute_nameERKNS1_19attribute_value_setE[__ZNK5boost3log9v2_mt_nt615value_extractorINS1_7trivial14severity_levelENS1_16fallback_to_noneENS3_3tag8severityEEclERKNS1_14attribute_nameERKNS1_19attribute_value_setE]+0xe0): undefined reference to `_imp___ZN5boost3log9v2_mt_nt63aux26attach_attribute_name_infoERNS_9exceptionERKNS1_14attribute_nameE'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

我的设置有什么问题?

注意:我已经检查(并尝试)了这些链接以防我的问题重复:

如果我查看这些解决方案,unicode 是唯一一个我还没有开始工作的解决方案。我已将 add_definitions("-Zc:wchar_t") 添加到我的 Cmake 文件中,但我从 c++.exe 收到无法识别的命令行选项错误(因为该标志是特定于 VS 的)。

问题可能是由于您的代码与 Boost.Log 之间的目标 Windows 版本不匹配造成的。看到这个答案:

很可能,您的项目将 _WIN32_WINNT 某处定义为 0x0600 或更高的值。

由于MinGW提供的WindowsSDK太不完整,Boost在MinGW编译时默认为WindowsXP