所以我有一个 C++ 项目,用于从 JSON 文件生成一些批处理文件,我使用 CMake 构建该文件。我使用的是 Windows,但使用 WSL2 可以在 Linux 中完成所有操作。我已经开发了所有内容并使用 GCC 构建了它,但现在我正在测试 MinGW,我在 MinGW 查找 Boost 库时遇到了问题。这是 CMake 配置:
cmake_minimum_required(VERSION 3.10)
# Set some basic project attributes
project (myproject
VERSION 1.0
DESCRIPTION "bla bla bla")
# C++ standard
set(CMAKE_CXX_STANDARD 20)
# Add definitions here
# add_definitions(-D....)
# Different versions for Debug and Release
set(CMAKE_CXX_FLAGS_DEBUG_INIT "-Wall -g3 -O0")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-Wall -O3 -s -pipe")
# Astyle Option in make
add_custom_target (astyle
COMMAND
COMMAND astyle -r ${PROJECT_SOURCE_DIR}/*.h ${PROJECT_SOURCE_DIR}/*.cpp --exclude=build
VERBATIM
)
# Find Doxygen to create documentation
FIND_PACKAGE(Doxygen)
# In case Doxygen is found create a target to create the documentation
if (DOXYGEN_FOUND)
# Set the input and output file paths
set(DOXYGEN_IN ${PROJECT_SOURCE_DIR}/Doxyfile)
set(DOXYGEN_OUT ${PROJECT_SOURCE_DIR}/Doxyfile.doxygen)
# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
message("Starting doxygen build process")
# Add a target to create the documentation
add_custom_target(doc_doxygen # ALL -> In case of automatic build always build all the documentation
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMENT "Create doxygen documentation"
VERBATIM )
else (DOXYGEN_FOUND)
message("Doxygen not found, not building documentation")
endif (DOXYGEN_FOUND)
# Export file for CMake configuration
configure_file(config.h.in ${PROJECT_SOURCE_DIR}/include/config.h)
# Find Boost
find_package(Boost REQUIRED log_setup log thread system filesystem chrono)
# If installed jsoncpp will be found automatically on UNIX Systems
IF (MINGW)
# Including JSON-CPP on Windows Systems
include_directories(${PROJECT_SOURCE_DIR}/jsoncpp/include)
link_directories(${PROJECT_SOURCE_DIR}/jsoncpp/lib)
ENDIF ()
# Includes
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/logging/include)
include_directories(${PROJECT_SOURCE_DIR}/buffer/include)
include_directories(${PROJECT_SOURCE_DIR}/options/include)
include_directories(${PROJECT_SOURCE_DIR}/secFunctions/include)
# Path for final executable (has to be defined before subdirectories)
add_executable(${PROJECT_NAME} main.cpp)
# Subdirectory
add_subdirectory(logging)
add_subdirectory(buffer)
add_subdirectory(options)
add_subdirectory(secFunctions)
# Add libraries
IF (MINGW)
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} jsoncpp wsock32 ws2_32)
ELSE (MINGW)
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} jsoncpp)
ENDIF ()
使用 GCC 11.4 构建时一切正常,但是当我使用 MinGW(GCC 10)时,一切都崩溃了。我收到以下错误消息:
CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
[cmake] Could NOT find Boost (missing: log_setup log thread system filesystem
[cmake] chrono) (found version "1.74.0")
[cmake] Call Stack (most recent call first):
[cmake] /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
[cmake] /usr/share/cmake-3.22/Modules/FindBoost.cmake:2360 (find_package_handle_standard_args)
[cmake] CMakeLists.txt:54 (find_package)
我尝试重写 MinGW 的 CMake 配置,然后在编译时“工作”,但在构建项目时,它根本无法识别 Boost。我做错了什么?
一个脏的解决方案是将 Boost Libraries 的根文件夹设置为包含它们的 GCC 文件夹。对我来说这是:
set(Boost_ROOT "usr/lib/x86_64-linux-gnu/cmake/")
一个更清洁的解决方案是执行以下命令:
(sudo) cp -r path_to_gcc/lib/cmake/ path_to_mingw/lib/
并删除上面的代码。
我不确定这是否是一个好的解决方案,因为我不知道 MinGW 是否需要不同的库才能与 CMake 和 Boost 真正兼容,但这应该有效。