Search code examples
c++gcccmakemacos-high-sierraboost-filesystem

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


I'm currently trying to compile a program that uses Boost.Filesystem on macOS High Sierra 10.13.4. I'm also using gcc 7.3 to compile, which I manually installed using Homebrew. The program will compile, but then throws the following error during linking

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

I'm using cmake to build, and my CMakeLists.txt file looks like this

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}
)

I'm using the command cmake -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX . to generate the Makefile, and the environment variables $CC and $CXX are set to the versions of gcc and g++ created by Homebrew.

I've looked at several other stackoverflow answers where people have the same error, and all of those are the result of incorrect linking. I'm linking in the same way that the correct answers say to do, yet the error still persists. Am I still linking it incorrectly or is there something else going on here?


Solution

  • So I found out what was going on. This made me realized that maybe the bottled homebrew Boost 1.66.0 library wasn't built with GCC. I switched the compiler to clang, and now it compiles just fine.