【问题标题】:How to use and link boost on macOS using CMake?如何使用 CMake 在 macOS 上使用和链接 boost?
【发布时间】:2021-03-29 18:12:19
【问题描述】:

感谢所有帮助过的人!最终起作用的是改变:

set(CMAKE_CXX_COMPILER "gcc-10")
set(CMAKE_C_COMPILER "g++-10")

到:

set(CMAKE_CXX_COMPILER "/usr/local/opt/llvm/bin/clang")
set(CMAKE_C_COMPILER "/usr/local/opt/llvm/bin/clang++")

尝试将 boost 链接到我的程序时出现以下错误:

[ 83%] Linking CXX executable cartogram
Undefined symbols for architecture x86_64:
  "__ZN5boost15program_options11to_internalERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
      __ZN5boost15program_options11to_internalINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESt6vectorIS7_SaIS7_EERKS8_IT_SaISB_EE in main.cpp.o
  "__ZN5boost15program_options16validation_error12get_templateB5cxx11ENS1_6kind_tE", referenced from:
      __ZN5boost15program_options10validators17get_single_stringIcEERKNSt7__cxx1112basic_stringIT_St11char_traitsIS5_ESaIS5_EEERKSt6vectorIS9_SaIS9_EEb in main.cpp.o
  "__ZN5boost15program_options19options_descriptionC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjj", referenced from:
      _main in main.cpp.o
  "__ZN5boost15program_options20invalid_option_valueC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
      __ZN5boost15program_options8validateIicEEvRNS_3anyERKSt6vectorINSt7__cxx1112basic_stringIT0_St11char_traitsIS7_ESaIS7_EEESaISB_EEPT_l in main.cpp.o
  "__ZN5boost15program_options22error_with_option_nameC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES9_S9_i", referenced from:
      __ZN5boost15program_options10validators17get_single_stringIcEERKNSt7__cxx1112basic_stringIT_St11char_traitsIS5_ESaIS5_EEERKSt6vectorIS9_SaIS9_EEb in main.cpp.o
  "__ZN5boost15program_options3argB5cxx11E", referenced from:
      __ZNK5boost15program_options11typed_valueIbcE4nameB5cxx11Ev in main.cpp.o
      __ZNK5boost15program_options11typed_valueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcE4nameEv in main.cpp.o
      __ZNK5boost15program_options11typed_valueIicE4nameB5cxx11Ev in main.cpp.o
  "__ZN5boost15program_options6detail7cmdline21set_additional_parserENS_9function1ISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_ERKSA_EE", referenced from:
      __ZN5boost15program_options25basic_command_line_parserIcE12extra_parserENS_9function1ISt4pairINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESA_ERKSA_EE in main.cpp.o
  "__ZN5boost15program_options6detail7cmdlineC2ERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS9_EE", referenced from:
      __ZN5boost15program_options25basic_command_line_parserIcEC1EiPKPKc in main.cpp.o
  "__ZN5boost15program_options8validateERNS_3anyERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS9_EEPS9_i", referenced from:
      __ZNK5boost15program_options11typed_valueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcE6xparseERNS_3anyERKSt6vectorIS7_SaIS7_EE in main.cpp.o
  "__ZN5boost15program_options8validateERNS_3anyERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS9_EEPbi", referenced from:
      __ZNK5boost15program_options11typed_valueIbcE6xparseERNS_3anyERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EE in main.cpp.o
  "__ZN5boost15program_optionslsERSoRKNS0_19options_descriptionE", referenced from:
      _main in main.cpp.o
  "__ZNK5boost15program_options22abstract_variables_mapixERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
      _main in main.cpp.o
      __Z8read_csvN5boost15program_options13variables_mapEP8MapState in read_csv.cpp.o
  "__ZNK5boost15program_options22error_with_option_name23substitute_placeholdersERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE", referenced from:
      __ZTVN5boost10wrapexceptINS_15program_options20invalid_option_valueEEE in main.cpp.o
      __ZTVN5boost10wrapexceptINS_15program_options16validation_errorEEE in main.cpp.o
      __ZTVN5boost15program_options16validation_errorE in main.cpp.o
      __ZTVN5boost15program_options20invalid_option_valueE in main.cpp.o
  "__ZNK5boost15program_options29value_semantic_codecvt_helperIcE5parseERNS_3anyERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EEb", referenced from:
      __ZTVN5boost15program_options11typed_valueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEE in main.cpp.o
      __ZTVN5boost15program_options11typed_valueIicEE in main.cpp.o
      __ZTVN5boost15program_options11typed_valueIbcEE in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

但是,当运行 cmake 命令时,cmake 会通知我它已找到 boost 和所需的组件:

-- Found Boost: /usr/local/lib/cmake/Boost-1.74.0/BoostConfig.cmake (found suitable version "1.74.0", minimum required is "1.40") found components: program_options

我正在使用gccg++ 版本10.2.0。我使用 cmake -DCMAKE_CXX_COMPILER=g++-10 -DCMAKE_C_COMPILER=gcc-10 配置我的 CMake 文件。

我已经尝试了以下 2 个链接选项,但是,它们都导致相同的错误:

target_link_libraries(cartogram CGAL::CGAL Boost::program_options ${FFTW_LIBRARIES})target_link_libraries(cartogram CGAL::CGAL ${Boost_LIBRARIES} ${FFTW_LIBRARIES})

我还尝试在调用查找 boost(使用:find_package(Boost 1.40 COMPONENTS program_options REQUIRED))之前包含add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)(编辑:来自Boost Linking problems)。

附带说明,完全相同的 cmake 文件可以在 Ubuntu、Linux 上完美找到!

我该如何解决这个错误?

编辑:我什至不完全确定这是否是一个链接错误,因为我根本不包括要链接的 boost 库(因为我相信程序选项可以用作仅标头库),我仍然得到同样的错误!

编辑 2:这是我的全部 CMake 代码:

# code for findFFTW
configure_file(downloadFindFFTW.cmake.in findFFTW-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
        RESULT_VARIABLE result
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/findFFTW-download )
if(result)
    message(FATAL_ERROR "CMake step for findFFTW failed: ${result}")
    else()
    message("CMake step for findFFTW completed (${result}).")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
        RESULT_VARIABLE result
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/findFFTW-download )
if(result)
    message(FATAL_ERROR "Build step for findFFTW failed: ${result}")
endif()

set(findFFTW_DIR ${CMAKE_CURRENT_BINARY_DIR}/findFFTW-src)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${findFFTW_DIR}")

# As per https://github.com/CGAL/cgal/wiki/How-to-use-CGAL-with-CMake-or-your-own-build-system
cmake_minimum_required(VERSION 3.1)
project(cartogram)

# set(PLATFORM_SPECIFIC_LIBS "-lpthread")
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
set(CMAKE_CXX_STANDARD 20)
set(THREADS_PREFER_PTHREAD_FLAG ON)
set(CMAKE_BUILD_TYPE Release)

find_package(CGAL)
find_package(Boost 1.40 COMPONENTS program_options REQUIRED)
find_package(FFTW)
find_package(Threads REQUIRED)

add_executable(cartogram main.cpp
              geo_div.cpp
                map_state.cpp
                read_csv.cpp
                rescale_map.cpp
                write_eps.cpp
                read_geojson.cpp
                fill_with_density.cpp
              blur_density.cpp
              flatten_density.cpp
                ft_real_2d.cpp)

# Boost::program_options ${Boost_LIBRARIES}
target_link_libraries(cartogram
                      CGAL::CGAL
                      ${Boost_LIBRARIES}
                      ${FFTW_LIBRARIES}
                      ${CMAKE_THREAD_LIBS_INIT})

编辑 3:这是我在运行 cmake -DCMAKE_CXX_COMPILER=g++-10 -DCMAKE_C_COMPILER=gcc-10 时得到的:

CMake Warning:
  No source or binary directory provided.  Both will be assumed to be the
  same as the current working directory, but note that this warning will
  become a fatal error in future CMake releases.


CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


CMake Warning at /usr/local/Cellar/cmake/3.19.2/share/cmake/Modules/Platform/Darwin-Initialize.cmake:303 (message):
  Ignoring CMAKE_OSX_SYSROOT value:

   /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk

  because the directory does not exist.
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.19.2/share/cmake/Modules/CMakeSystemSpecificInitialize.cmake:21 (include)
  CMakeLists.txt:3 (project)


-- Configuring done
-- Generating done
-- Build files have been written to: /Users/adityasinghania/Desktop/gastner/cartogram_cpp-master/cartogram_generator/findFFTW-download
CMake step for findFFTW completed (0).
[ 11%] Performing update step for 'findFFTW_download'
[ 22%] No patch step for 'findFFTW_download'
[ 33%] No configure step for 'findFFTW_download'
[ 44%] No build step for 'findFFTW_download'
[ 55%] No install step for 'findFFTW_download'
[ 66%] No test step for 'findFFTW_download'
[ 77%] Completed 'findFFTW_download'
[100%] Built target findFFTW_download
-- The C compiler identification is GNU 10.2.0
-- The CXX compiler identification is GNU 10.2.0
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/local/bin/gcc-10 - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/local/bin/g++-10 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Using header-only CGAL
-- Targetting Unix Makefiles
-- Using /usr/local/bin/g++-10 compiler.
-- DARWIN_VERSION=19
-- Mac Leopard detected
-- Found GMP: /usr/local/lib/libgmp.dylib  
-- Found MPFR: /usr/local/lib/libmpfr.dylib  
-- Found Boost: /usr/local/lib/cmake/Boost-1.74.0/BoostConfig.cmake (found suitable version "1.74.0", minimum required is "1.48")  
-- Boost include dirs: /usr/local/include
-- Boost libraries:    
-- Using gcc version 4 or later. Adding -frounding-math
-- Found Boost: /usr/local/lib/cmake/Boost-1.74.0/BoostConfig.cmake (found suitable version "1.74.0", minimum required is "1.40") found components: program_options 
-- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) 
-- Found FFTW: /usr/local/include   
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/adityasinghania/Desktop/gastner/cartogram_cpp-master/cartogram_generator

【问题讨论】:

  • “我也尝试在调用查找 boost 之前包含 add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)” - 设置 _GLIBCXX_USE_CXX11_ABI 不会影响搜索 Boost。此设置应影响您的可执行文件的编译。如果设置 _GLIBCXX_USE_CXX11_ABI 后错误消息没有改变,那么您可能 错误地指定了它。我们需要查看更多您的代码。
  • “在我的问题中包含哪些 sn-ps 代码可能会有所帮助,以便你们可以帮助我” - 您现在显示的代码通常就足够了。 (对于未来的问题,请查看minimal reproducible example)。
  • 将任何命令放在cmake_minimum_required 之前,就像对add_compile_definitions 所做的那样,通常是错误的。编译器和链接器标志通常应该在project 调用之后设置,此时 CMake 已经知道编译器、链接器及其属性。
  • 配置日志与您的CMakeLists.txt不对应。不应为VERSION 3.1CMakeLists.txt:3 (project) 发出消息Compatibility with CMake &lt; 2.8.12 will be removed from a future version of CMake. 不对应于您有project() 呼叫的第2 行。至于第一个警告,最好执行 out-of-source 构建,其中构建目录与源目录不同。 (因为您已经执行了源内构建,对于源外构建,您需要从源目录中删除 CMakeCache.txt
  • 通过设置set(CMAKE_CXX_FLAGS "-std=c++20 -pthread"),您可以删除 CMake 在确定编译器时设置的编译器标志。这实际上可能是奇怪的不一致的原因。最好不要替换而是 append 标志。实际上,-std=c++20-pthread 都有自己的首选设置方法。前者通过CMAKE_CXX_STANDARD 变量设置:stackoverflow.com/questions/10851247/…。后者是通过find_package(Threads):stackoverflow.com/questions/1620918/cmake-and-libpthread设置的。

标签: c++ boost cmake boost-program-options cmake-modules


【解决方案1】:

您收到这些链接错误的原因可能是您尝试链接由AppleClang 使用g++ 编译的Boost 库。 Clang 和 g++ 的标准库实现与 ABI 不兼容,即您不能使用 g++ 链接到 Clang 编译的库。

如果您想在 MacOS 上使用 g++,请确保您使用的库也使用 g++ 编译。这可能有用:https://andschwa.com/post/boost-with-gcc-on-os-x/

【讨论】:

  • 就是这个!谢谢!或者,对我有用的是其他东西,我将在我的答案中更新。
【解决方案2】:

我最喜欢的方法是使用vcpkg

  1. 使用此link 安装vcpkg
  2. 使用终端导航到vcpkg 文件夹。
  3. 接下来,输入./vcpkg/vcpkg install boost。 此命令完成后,运行./vcpkg/vcpkg install integrate;这样,将出现一个关于如何将 boost 与您的 cmake 文件整合的命令。将其复制并粘贴到文本编辑器中以供将来参考
  4. 使用选项运行 CMake 文件 -DCMAKE_TOOLCHAIN_FILE=$vcpkgRoot/scripts/buildsystems/vcpkg.cmake(前面的命令给出的那个。) 或者,如果您使用 Visual Studio 代码,则运行 vcpkg integrate install 会自动将库连接到 Visual Studio 代码。

【讨论】:

  • 感谢您的回复!我会尝试一下,如果它有效,我会非常乐意将赏金奖励给你!如果我没记错的话,我相信您还应该能够在您的 CMakeLists.txt 文件中包含以下行:set(CMAKE_TOOLCHAIN_FILE=$vcpkgRoot\scripts\buildsystems\vcpkg.cmake)
  • 在输入 vcpkg 集成安装后,我得到了我必须包含的 cmake 选项。但是,我尝试通过在 cmake 文件中设置它甚至将其设置为选项来确保 CMAKE_TOOLCHAIN_FILE 设置正确。然而,没有一个工作,事实上find_package 现在抛出一个错误,指出它找不到 Boost。 :(
  • vcpkg 集成安装仅适用于 Visual Studio 和/或 Visual Studio 代码。如果您不使用这些中的任何一个,请选择退出使用常规的 cmake 选项。怎么可能做到这一点。 1. set(CMAKE_TOOLCHAIN_FILE=$vcpkgRoot\scripts\buildsystems\vcpkg.cmake) 或 -DCMAKE_TOOLCHAIN_FILE=$vcpkgRoot/scripts/buildsystems/vcpkg.cmake 2. 从终端复制粘贴命令。一句警告的话。 vcpkg 默认安装 32 位软件包,因此如果您想使用 64 位构建系统,则需要执行一个额外步骤,即 vcpkg install boost:x64-osx
猜你喜欢
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-29
  • 2012-05-22
相关资源
最近更新 更多