在 macOS High Sierra 上使用 Boost.Filesystem 编译时出现链接器错误

Linker error when compiling with Boost.Filesystem on macOS High Sierra

我目前正在尝试编译一个在 macOS High Sierra 10.13.4 上使用 Boost.Filesystem 的程序。我还使用 gcc 7.3 进行编译,这是我使用 Homebrew 手动安装的。该程序将编译,但在链接期间抛出以下错误

Undefined symbols for architecture x86_64:
  "boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)", referenced from:
      boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<boost::filesystem::directory_entry>::type>, boost::filesystem::path&>::type boost::filesystem::path::operator=<boost::filesystem::directory_entry>(boost::filesystem::directory_entry const&) in world.cc.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我正在使用 cmake 构建,我的 CMakeLists.txt 文件如下所示

cmake_minimum_required (VERSION 3.0)

MESSAGE(STATUS "Compiler: " ${CMAKE_CXX_COMPILER})

# variables for CMAKE
set(PROJECT sealab)
set(BINARY ${PROJECT}.out)

set(SRC_DIR src/)
set(INC_DIR inc/)

FILE(GLOB_RECURSE SRC ${SRC_DIR}/*.cc)

# library include dirs
set(IMGUI_INC libs/imgui/)
set(SPDLOG_INC libs/spdlog/include/)
set(JSON_INC libs/json/include/)
set(
    LIB_INC 
    ${IMGUI_INC} 
    ${SPDLOG_INC} 
    ${JSON_INC} 
    ${GLM_INC}
)

# library src files
FILE(GLOB IMGUI_SRC ${IMGUI_INC}*.cpp)
FILE(GLOB SPDLOG_SRC ${SPDLOG_INC}*.cpp)
set(
    LIB_SRC 
    ${IMGUI_SRC} 
    ${SPDLOG_SRC}
)

project(${PROJECT})
# version number
set(${PROJECT}_VERSION_MAJOR 0)
set(${PROJECT}_VERSION_MINOR 1)

# Compilation Database
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-local-typedefs")

# threads
set(THREADS_PREFER_PTHREAD_FLAG ON)

# adding packages
find_package(OpenGL    REQUIRED)
find_package(GLEW      REQUIRED)
find_package(glfw3 3.2 REQUIRED)
find_package(Threads   REQUIRED)
find_package(Assimp    REQUIRED)
find_package(glm       REQUIRED)
find_package(Boost COMPONENTS system filesystem REQUIRED)

set(Boost_USE_MULTITHREADED  ON)
set(Boost_USE_STATIC_LIBS    ON)
set(Boost_USE_STATIC_RUNTIME ON)

# adding directories and source files
include_directories(
    ${OPENGL_INCLUDE_DIRS} 
    ${GLEW_INCLUDE_DIRS} 
    ${ASSIMP_INCLUDE_DIRS} 
    ${GLM_INCLUDE_DIRS}
    ${Boost_INCLUDE_DIRS}
)
include_directories(${INC_DIR} ${LIB_INC})
link_directories(${INC_DIR} ${LIB_INC})

# adding source files
add_executable(${BINARY} ${SRC} ${LIB_SRC})
set_target_properties(${BINARY} PROPERTIES COTIRE_UNITY_TARGET_NAME "unity")
# linking libraries
target_link_libraries(
    ${BINARY} 
    OpenGL::GL 
    ${GLEW_LIBRARIES} 
    Threads::Threads 
    ${ASSIMP_LIBRARIES} 
    glfw 
    glm 
    ${Boost_LIBRARIES}
)

我使用命令cmake -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX .生成Makefile,环境变量$CC$CXX设置为Homebrew创建的gcc和g++版本。

我查看了其他几个 Whosebug 答案,其中人们有相同的错误,所有这些都是错误链接的结果。我按照正确答案所说的方式进行链接,但错误仍然存​​在。我仍然链接不正确还是这里发生了其他事情?

所以我知道发生了什么事。 This 让我意识到也许瓶装自制软件 Boost 1.66.0 库不是用 GCC 构建的。我把编译器换成了clang,现在编译正常了