CMake를 사용하여 C ++ 프로그램을 Boost와 연결하는 방법
내 프로그램을 Ubuntu에서 부스트 라이브러리와 연결 비용으로 내 CMake 파일이 어떻게 표시되어야합니까?
실행 중 표시되는 오류 make
:
main.cpp:(.text+0x3b): undefined reference to `boost::program_options::options_description::m_default_line_length'
주 파일은 정말 간단합니다.
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/option.hpp>
using namespace std;
#include <iostream>
namespace po = boost::program_options;
int main(int argc, char** argv) {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
;
return 0;
}
나는 그것을 할 수 있었다. 내 CMake 파일에 추가 한 유일한 유일한 줄은 다음과 가변합니다.
target_link_libraries(
my_target_file
${Boost_PROGRAM_OPTIONS_LIBRARY}
)
CMake에서 find_package
필요한 라이브러리를 찾는 데 사용할 수 있습니다 . 일반적으로 FindBoost.cmake
CMake 설치와 함께 있습니다.
내가 기억하는 한, /usr/share/cmake/Modules/
공통 라이브러리에 대한 다른 찾기 펼쳐와 함께 설치 됩니다. 작동 방식에 대한 자세한 내용은 해당 파일의 설명서를 확인합니다.
내 머릿속의 예 :
FIND_PACKAGE( Boost 1.40 COMPONENTS program_options REQUIRED )
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
ADD_EXECUTABLE( anyExecutable myMain.cpp )
TARGET_LINK_LIBRARIES( anyExecutable LINK_PUBLIC ${Boost_LIBRARIES} )
이 코드가 도움이되기를 바랍니다.
- FindBoost.cmake에 대한 공식 문서는 다음과 가능 합니다.
- 그리고 실제 FindBoost.cmake (GitHub에 호스팅 됨)
다음은 내 구성입니다.
cmake_minimum_required(VERSION 2.8)
set(Boost_INCLUDE_DIR /usr/local/src/boost_1_46_1)
set(Boost_LIBRARY_DIR /usr/local/src/boost_1_46_1/stage/lib)
find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})
add_executable(main main.cpp)
target_link_libraries( main ${Boost_LIBRARIES} )
대응 대상이있는 최신 CMake 구문에 @MOnsDaR 답변을 적용하면 다음과 가변됩니다.
find_package(Boost 1.40 COMPONENTS program_options REQUIRED)
add_executable(anyExecutable myMain.cpp)
target_link_libraries(anyExecutable Boost::program_options)
포함되어 있습니다 Boost::program_options
.
어떤 부스트 라이브러리? 대부분은 순수한 템플릿이며 링크가 필요하지 않습니다.
이제 Boost 프로그램 옵션을 원한다는 것을 알려주는 구체적인 예제가 실제로 표시됩니다. (우분투에 더 많이 알려줍니다) 두 가지 작업을 수행해야합니다.
libboost-program-options-dev
링크 할 수 있어야 설치 하십시오.- 에게
cmake
에 대한 링크libboost_program_options
.
나는 주로 Makefile을 사용합니다.
$ g++ boost_program_options_ex1.cpp -o bpo_ex1 -lboost_program_options
$ ./bpo_ex1
$ ./bpo_ex1 -h
$ ./bpo_ex1 --help
$ ./bpo_ex1 -help
$
많이하지 않을 것입니다.
CMake를 들어, 라이브러리 목록에 boost_program_options를 추가해야하고, IIRC이를 통해 이루어집니다 SET(liblist boost_program_options)
당신에 CMakeLists.txt
.
일반적으로 시스템 기본 설치 경로를 사용하는 두 가지 방법 /usr/lib/x86_64-linux-gnu/
:
find_package(Boost REQUIRED regex date_time system filesystem thread graph)
include_directories(${BOOST_INCLUDE_DIRS})
message("boost lib: ${Boost_LIBRARIES}")
message("boost inc:${Boost_INCLUDE_DIR}")
add_executable(use_boost use_boost.cpp)
target_link_libraries(use_boost
${Boost_LIBRARIES}
)
Boost를 로컬 디렉토리에 설치하거나 시스템 설치 대신 로컬 설치를 선택하면 다음과 같이 할 수 있습니다.
set( BOOST_ROOT "/home/xy/boost_install/lib/" CACHE PATH "Boost library path" )
set( Boost_NO_SYSTEM_PATHS on CACHE BOOL "Do not search system for Boost" )
find_package(Boost REQUIRED regex date_time system filesystem thread graph)
include_directories(${BOOST_INCLUDE_DIRS})
message("boost lib: ${Boost_LIBRARIES}, inc:${Boost_INCLUDE_DIR}")
add_executable(use_boost use_boost.cpp)
target_link_libraries(use_boost
${Boost_LIBRARIES}
)
위의 디렉토리 /home/xy/boost_install/lib/
는 Boost를 설치하는 곳입니다.
xy@xy:~/boost_install/lib$ ll -th
total 16K
drwxrwxr-x 2 xy xy 4.0K May 28 19:23 lib/
drwxrwxr-x 3 xy xy 4.0K May 28 19:22 include/
xy@xy:~/boost_install/lib$ ll -th lib/
total 57M
drwxrwxr-x 2 xy xy 4.0K May 28 19:23 ./
-rw-rw-r-- 1 xy xy 2.3M May 28 19:23 libboost_test_exec_monitor.a
-rw-rw-r-- 1 xy xy 2.2M May 28 19:23 libboost_unit_test_framework.a
.......
xy@xy:~/boost_install/lib$ ll -th include/
total 20K
drwxrwxr-x 110 xy xy 12K May 28 19:22 boost/
로컬에 설치된 Boost를 사용하는 방법에 관심이 있다면이 질문을 볼 수 있습니다. CMake에서 대체 Boost 설치를 찾으려면 어떻게해야합니까? .
참고 URL : https://stackoverflow.com/questions/3897839/how-to-link-c-program-with-boost-using-cmake
'IT' 카테고리의 다른 글
three.js가있는 투명한 배경 (0) | 2020.08.20 |
---|---|
자바의 긴 if 문 목록 (0) | 2020.08.20 |
Ruby Strings의 gsub 메소드와 하위 메소드의 차이점은 무엇입니까? (0) | 2020.08.20 |
Chrome에서 웹 포장을로드 할 수 없습니다. (0) | 2020.08.20 |
클래스 이름으로 하위 요소를 얻는 방법은 무엇입니까? (0) | 2020.08.20 |