C++ 如何使用CMake在macOS上使用和链接boost?

C++ 如何使用CMake在macOS上使用和链接boost?,c++,boost,cmake,boost-program-options,cmake-modules,C++,Boost,Cmake,Boost Program Options,Cmake Modules,谢谢你的帮助!最终起作用的是变化: set(CMAKE_CXX_COMPILER "gcc-10") set(CMAKE_C_COMPILER "g++-10") 致: 尝试将boost链接到我的程序时出现以下错误: [ 83%] Linking CXX executable cartogram Undefined symbols for architecture x86_64: "__ZN5boost15program_options11

谢谢你的帮助!最终起作用的是变化:

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

尝试将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和所需组件:

--找到Boost:/usr/local/lib/cmake/Boost-1.74.0/BoostConfig.cmake(找到合适的版本“1.74.0”,最低要求为“1.40”)找到组件:程序选项

我使用的是
gcc
g++
版本
10.2.0
。我使用
CMake-DCMAKE\u CXX\u COMPILER=g++-10-DCMAKE\u C\u COMPILER=gcc-10来配置我的CMake文件

我尝试了以下两种链接选项,但是,它们都会导致相同的错误:

target\u link\u库(cartogram CGAL::CGAL Boost::program\u options${FFTW\u libraries})
target_link_库(cartogram CGAL::CGAL${Boost_库}${FFTW_库})


我还尝试在调用find boost(使用:
find\u package(需要boost 1.40组件程序选项)
)之前包含
add\u compile\u定义(\u GLIBCXX\u USE\u cx11\u ABI=0)
(编辑:from)

另一方面,完全相同的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\u CXX\u COMPILER=g++-10-DCMAKE\u C\u 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
CMake警告:
没有提供源或二进制目录。两者都将被假定为
与当前工作目录相同,但请注意,此警告将
在以后的CMake版本中成为致命错误。
CMakeLists.txt上的CMake弃用警告:1(需要CMake_最小值):
与CMake<2.8.12的兼容性将从的未来版本中删除
克马克。
更新版本参数值或使用。。。后缀
使项目不需要与旧版本兼容。
CMake警告位于/usr/local/ceral/CMake/3.19.2/share/CMake/Modules/Platform/Darwin Initialize。CMake:303(消息):
忽略CMAKE_OSX_SYSROOT值:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk
因为目录不存在。
调用堆栈(最新调用优先):
/usr/local/Cellar/cmake/3.19.2/share/cmake/Modules/CMakeSystemSpecificInitialize.cmake:21(包括)
CMakeLists.txt:3(项目)
--配置完成
--生成完成
--构建文件已写入:/Users/adityasinghania/Desktop/gastner/cartogram\u cpp-master/cartogram\u generator/findFFTW下载
FindFtw的CMake步骤已完成(0)。
[11%]正在为“FindFtw_下载”执行更新步骤
[22%]没有“FindFtw_下载”的修补程序步骤
[33%]没有“FindFtw_下载”的配置步骤
[44%]没有“FindFtw_下载”的构建步骤
[55%]没有“FindFtw_下载”的安装步骤
[66%]没有“FindFtw_下载”的测试步骤
[77%]已完成“FindFtw_下载”
[100%]内置目标FindFtw_下载
--C编译器标识为GNU10.2.0
--CXX编译器标识为GNU 10.2.0
--检查C编译器是否具有-isysroot
--检查C编译器是否具有-isysroot-yes
--检查C编译器是否支持OSX部署目标标志
--检查C编译器是否支持OSX部署目标标志-是
--检测C编译器ABI信息
--检测C编译器ABI信息-完成
--检查C编译器是否工作:/usr/local/bin/gcc-10-跳过
--检测C编译特性
--检测C编译特性-完成
--正在检查CXX编译器是否具有-isysroot
--正在检查CXX编译器是否具有-isysroot-yes
--检查CXX编译器是否支持OSX部署目标标志
--正在检查CXX编译器是否支持OSX部署目标标志-是
--检测CXX编译器ABI信息
--检测CXX编译器ABI信息-完成
--检查CXX编译器是否工作:/usr/local/bin/g++-10-跳过
--检测CXX编译特性
--检测CXX编译功能-完成
--仅使用标题CGAL
--以Unix生成文件为目标
--使用/usr/local/bin/g++-10编译器。
--达尔文版本=19
--检测到Mac Leopard
--找到GMP:/usr/local/lib/libgmp.dylib
--找到MPFR:/usr/local/lib/libmpfr.dylib
--找到Boost:/usr/local/lib/cmake/Boost-1.74.0/BoostConfig.cmake(找到合适的版本“1.74.0”,最低要求为“1.48”)
--Boost include dirs:/usr/local/include
--Boost库:
--使用gcc版本4或更高版本。加法运算
--找到的Boost:/usr/local/lib/cmake/Boost-1.74.0/BoostConfig.cmake(找到合适的版本“1.74.0”,最低要求为“1.40”)找到的组件:程序选项
--找不到PkgConfig(缺少:PKG\u CONFIG\u可执行文件)
--找到FFTW:/usr/local/include
--正在查找pthread.h
--正在查找pthread.h-找到
--正在执行测试CMAKE_HAVE_LIBC_PTHREAD
--执行测试CMAKE_HAVE_LIBC_PTHREAD-成功
--找到的线程:TRUE
--配置完成
--生成完成
--构建文件已写入:/Users/adityasinghania/Desktop/gasner/cartogram\u cpp-master/cartogram\u生成器

我最喜欢的方法是使用
vcpkg

  • 使用此安装
    vcpkg
  • 使用终端导航到
    vcpkg
    文件夹
  • 接下来,键入
    /vcpkg/vcpkg安装boost
    。 此命令完成后,运行
    /vcpkg/vcpkg install integrate
    ;这样,将出现一个关于如何将boost与cmake文件集成的命令。将其复制并粘贴到文本编辑器中以供将来参考
  • 使用该选项运行CMake文件
    -DCMAKE_TOOLCHAIN_FILE=$vcpkgRoot/scripts/buildsystems/vcpkg.cmake
    (由早期
    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