简体   繁体   English

如何使用 CMake 将 C++ 程序与 Boost 链接起来

[英]How to link C++ program with Boost using CMake

What should my CMake file look like for linking my program with the Boost library under Ubuntu?将我的程序与 Ubuntu 下的 Boost 库链接起来时,我的 CMake 文件应该是什么样的?

The errors shown during running make :运行make期间显示的错误:

main.cpp:(.text+0x3b): undefined reference to `boost::program_options::options_description::m_default_line_length'

The main file is really simple:主文件非常简单:

#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;
}

I've managed to do that.我已经做到了。 The only lines that I've added to my CMake files were:我添加到我的 CMake 文件中的唯一行是:

target_link_libraries(
my_target_file
${Boost_PROGRAM_OPTIONS_LIBRARY}
)

In CMake you could use find_package to find libraries you need.在 CMake 中,您可以使用find_package来查找您需要的库。 There usually is a FindBoost.cmake along with your CMake installation.通常有一个FindBoost.cmake与您的 CMake 安装一起。

As far as I remember, it will be installed to /usr/share/cmake/Modules/ along with other find-scripts for common libraries.据我所知,它将与公共库的其他查找脚本一起安装到/usr/share/cmake/Modules/ You could just check the documentation in that file for more information about how it works.您可以查看该文件中的文档以获取有关其工作原理的更多信息。

An example out of my head:我脑子里的一个例子:

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

I hope this code helps.我希望这段代码有帮助。

The following is my configuration:以下是我的配置:

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

Adapting @MOnsDaR answer for modern CMake syntax with imported targets, this would be:使用导入的目标为现代 CMake 语法调整 @MONsDaR 答案,这将是:

find_package(Boost 1.40 COMPONENTS program_options REQUIRED)

add_executable(anyExecutable myMain.cpp)

target_link_libraries(anyExecutable Boost::program_options)

Note that it is not necessary to specify the include directories manually, since it is already taken care of through the imported target Boost::program_options .请注意,没有必要手动指定包含目录,因为它已经通过导入的目标Boost::program_options

Two ways, using system default install path, usually /usr/lib/x86_64-linux-gnu/ :两种方式,使用系统默认安装路径,通常是/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}
        )

If you install Boost in a local directory or choose local install instead of system install, you can do it by this:如果您在本地目录中安装 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}
        )

Note the above dir /home/xy/boost_install/lib/ is where I install Boost:注意上面的目录/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/

If you are interested in how to use a local installed Boost, you can see this question How can I get CMake to find my alternative Boost installation?如果您对如何使用本地安装的 Boost 感兴趣,可以查看这个问题如何让 CMake 找到我的替代 Boost 安装? . .

Here is my take:这是我的看法:

cmake_minimum_required(VERSION 3.15)

project(TryOuts LANGUAGES CXX)

find_package(Boost QUIET REQUIRED COMPONENTS program_options)

if(NOT Boost_FOUND)
    message(FATAL_ERROR "Boost Not found")
endif()

add_executable(helloworld main.cpp)

target_link_libraries(helloworld PUBLIC Boost::program_options)

Which Boost library?哪个Boost库? Many of them are pure templates and do not require linking.其中许多是纯模板,不需要链接。

Now with that actually shown concrete example which tells us that you want Boost program options (and even more told us that you are on Ubuntu), you need to do two things:现在有了实际显示的具体示例,它告诉我们您想要 Boost 程序选项(甚至更多地告诉我们您使用的是 Ubuntu),您需要做两件事:

  1. Install libboost-program-options-dev so that you can link against it.安装libboost-program-options-dev以便您可以链接它。
  2. Tell cmake to link against libboost_program_options .告诉cmake链接libboost_program_options

I mostly use Makefiles so here is the direct command-line use:我主要使用 Makefile,所以这里是直接使用命令行:

$ g++ boost_program_options_ex1.cpp -o bpo_ex1 -lboost_program_options
$ ./bpo_ex1
$ ./bpo_ex1 -h
$ ./bpo_ex1 --help
$ ./bpo_ex1 -help
$

It doesn't do a lot it seems.它似乎并没有做很多事情。

For CMake, you need to add boost_program_options to the list of libraries, and IIRC this is done via SET(liblist boost_program_options) in your CMakeLists.txt .对于 CMake,您需要将 boost_program_options 添加到库列表中,而 IIRC 这是通过CMakeLists.txt SET(liblist boost_program_options)完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM