IT story

CMakeLists.txt에 Boost 라이브러리를 어떻게 추가합니까?

hot-time 2020. 8. 5. 07:41
반응형

CMakeLists.txt에 Boost 라이브러리를 어떻게 추가합니까?


CMakeLists.txt에 Boost 라이브러리를 추가해야합니다. 어떻게합니까, 어떻게 추가합니까?


이것을 CMakeLists.txt파일에 넣으십시오 (원하는 경우 옵션을 OFF에서 ON으로 변경하십시오).

set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
find_package(Boost 1.45.0 COMPONENTS *boost libraries here*) 

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS}) 
    add_executable(progname file1.cxx file2.cxx) 
    target_link_libraries(progname ${Boost_LIBRARIES})
endif()

분명히 내가 원하는 곳에 라이브러리를 넣어야 *boost libraries here*합니다. 예를 들어 filesystemand regex라이브러리를 사용하는 경우 다음 같이 작성하십시오.

find_package(Boost 1.45.0 COMPONENTS filesystem regex)

find_package사용하여 사용 가능한 부스트 라이브러리를 검색 할 수 있습니다. CMake와 함께 기본적으로 설치되는 Boost to FindBoost.cmake 검색을 연기 합니다.

Boost를 찾으면 find_package()호출에 많은 변수가 채워집니다 ( FindBoost.cmake 참조 확인 ). 그중에는 BOOST_INCLUDE_DIRSBoost_LIBRARIES 및 Boost_XXX_LIBRARY 변수가 있으며 XXX는 특정 Boost 라이브러리로 대체되었습니다. 이를 사용하여 include_directoriestarget_link_libraries 를 지정할 수 있습니다 .

예를 들어 boost :: program_options 및 boost :: regex가 필요하다고 가정하면 다음과 같은 작업을 수행 할 수 있습니다.

find_package( Boost REQUIRED COMPONENTS program_options regex )
include_directories( ${Boost_INCLUDE_DIRS} )
add_executable( run main.cpp ) # Example application based on main.cpp

# Alternatively you could use ${Boost_LIBRARIES} here.
target_link_libraries( run ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_REGEX_LIBRARY} )

몇 가지 일반적인 팁 :

  • 검색 할 때 FindBoost는 환경 변수 $ ENV {BOOST_ROOT}를 확인합니다. 필요한 경우 find_package를 호출하기 전에이 변수를 설정할 수 있습니다.
  • 여러 빌드 버전의 부스트 (멀티 스레드, 정적, 공유 등)가있는 경우 find_package를 호출하기 전에 원하는 구성을 지정할 수 있습니다. 에 다음 변수 중 일부를 설정하여이 작업을 수행 On: Boost_USE_STATIC_LIBS, Boost_USE_MULTITHREADED,Boost_USE_STATIC_RUNTIME
  • Windows에서 Boost를 검색 할 때 자동 링크에주의하십시오. 에서 "비주얼 스튜디오 사용자를위한 참고"내용을 읽어 참조 .
    • 내 조언은 자동 연결을 비활성화하고 cmake의 종속성 처리를 사용하는 것입니다. add_definitions( -DBOOST_ALL_NO_LIB )
    • 경우에 따라 동적 부스트가 사용되도록 명시 적으로 지정해야 할 수도 있습니다. add_definitions( -DBOOST_ALL_DYN_LINK )

가져온 대상이있는 최신 CMake 구문에 대한 @LainIwakura의 답변을 적용하면 다음과 같습니다.

set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
find_package(Boost 1.45.0 COMPONENTS filesystem regex) 

if(Boost_FOUND)
    add_executable(progname file1.cxx file2.cxx) 
    target_link_libraries(progname Boost::filesystem Boost::regex)
endif()

이미 가져온 목표를 돌보아 이후에서, 수동으로 디렉토리를 포함 지정 더 이상 필요하지 않습니다 Boost::filesystemBoost::regex.
regex그리고 filesystem당신이 필요로하는 모든 부스트 라이브러리로 대체 될 수있다.


이것이 일부 사람들에게 도움이되기를 바랍니다. 잘못된 오류가 발생했습니다. '_ZN5boost6system15system_categoryEv'기호에 대한 정의되지 않은 참조 //usr/lib/x86_64-linux-gnu/libboost_system.so.1.58.0 : 기호 추가 오류 : 명령 줄에서 DSO 누락 cmakeList.txt에 문제가있었습니다. 어떻게 든 "system"및 "filesystem"라이브러리를 명시 적으로 포함시키지 못했습니다. 그래서 CMakeLists.txt 에이 줄을 썼습니다.

이 줄은 프로젝트 실행 파일을 생성하기 전에 처음에 작성됩니다.이 단계에서는 부스트 라이브러리를 프로젝트 실행 파일에 연결할 필요가 없습니다.

set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
set(Boost_NO_SYSTEM_PATHS TRUE) 

if (Boost_NO_SYSTEM_PATHS)
  set(BOOST_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../3p/boost")
  set(BOOST_INCLUDE_DIRS "${BOOST_ROOT}/include")
  set(BOOST_LIBRARY_DIRS "${BOOST_ROOT}/lib")
endif (Boost_NO_SYSTEM_PATHS)


find_package(Boost COMPONENTS regex date_time system filesystem thread graph program_options) 

find_package(Boost REQUIRED regex date_time system filesystem thread graph program_options)
find_package(Boost COMPONENTS program_options REQUIRED)

이제 파일 끝에서 "KeyPointEvaluation"을 프로젝트 실행 파일로 고려하여이 줄을 작성했습니다.

if(Boost_FOUND)
    include_directories(${BOOST_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
    add_definitions(${Boost_DEFINITIONS})

    include_directories(${Boost_INCLUDE_DIRS})  
    target_link_libraries(KeyPointEvaluation ${Boost_LIBRARIES})
    target_link_libraries( KeyPointEvaluation ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_SYSTEM_LIBRARY})
endif()

나는 답변 12에 동의합니다 . 그러나 각 라이브러리를 별도로 지정하는 것을 선호합니다. 이것은 큰 프로젝트에서 디펜 던시를 더 명확하게 만듭니다. 그러나 (대소 문자 구분) 변수 이름을 잘못 입력 할 위험이 있습니다. 이 경우 직접적인 cmake 오류는 없지만 나중에 정의되지 않은 참조 링커 문제가 발생하여 해결하는 데 약간의 시간이 걸릴 수 있습니다. 따라서 다음 cmake 함수를 사용합니다.

function(VerifyVarDefined)
  foreach(lib ${ARGV}) 
    if(DEFINED ${lib})
    else(DEFINED ${lib})
      message(SEND_ERROR "Variable ${lib} is not defined")
    endif(DEFINED ${lib})
  endforeach()
endfunction(VerifyVarDefined)

위에서 언급 한 예의 경우 다음과 같습니다.

VerifyVarDefined(Boost_PROGRAM_OPTIONS_LIBRARY Boost_REGEX_LIBRARY)
target_link_libraries( run ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_REGEX_LIBRARY} )

If I had written "BOOST_PROGRAM_OPTIONS_LIBRARY" there would have been an error triggered by cmake and not much later triggered by the linker.


Try as saying Boost documentation:

set(Boost_USE_STATIC_LIBS        ON)  # only find static libs
set(Boost_USE_DEBUG_LIBS         OFF) # ignore debug libs and 
set(Boost_USE_RELEASE_LIBS       ON)  # only find release libs 
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF) 
find_package(Boost 1.66.0 COMPONENTS date_time filesystem system ...)
if(Boost_FOUND)   
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(foo foo.cc)   
    target_link_libraries(foo ${Boost_LIBRARIES})
endif()

Don't forget to replace foo to your project name and components to yours!

참고URL : https://stackoverflow.com/questions/6646405/how-do-you-add-boost-libraries-in-cmakelists-txt

반응형