如何使用 CMake 将 C++ 程序与 Boost 链接起来
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3897839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to link C++ program with Boost using CMake
提问by Szymon Lipiński
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}
)
回答by MOnsDaR
In CMake you could use find_package
to find libraries you need. There usually is a FindBoost.cmake
along with your CMake installation.
在 CMake 中,您可以find_package
用来查找所需的库。通常有一个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. You could just check the documentation in that file for more information about how it works.
据我所知,它将/usr/share/cmake/Modules/
与其他公共库的查找脚本一起安装。您可以查看该文件中的文档以获取有关其工作原理的更多信息。
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.
我希望这段代码有帮助。
- Here's the official documentation about FindBoost.cmake.
- And the actual FindBoost.cmake(hosted on GitHub)
- 这是关于 FindBoost.cmake的官方文档。
- 和实际的FindBoost.cmake(托管在 GitHub 上)
回答by Dean Chen
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} )
回答by oLen
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
.
请注意,没有必要手动指定包含目录,因为它已经通过导入的 target 进行了处理Boost::program_options
。
回答by Jayhello
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 directoryor 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 安装?.
回答by Dirk Eddelbuettel
Which Boost library? Many of them are pure templates and do not require linking.
哪个Boost库?其中许多是纯模板,不需要链接。
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),您需要做两件事:
- Install
libboost-program-options-dev
so that you can link against it. - Tell
cmake
to link againstlibboost_program_options
.
- 安装
libboost-program-options-dev
以便您可以链接它。 - 告诉
cmake
链接反对libboost_program_options
。
I mostly use Makefiles so here is the direct command-line use:
我主要使用 Makefiles 所以这里是直接的命令行使用:
$ 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 这是通过SET(liblist boost_program_options)
在您的CMakeLists.txt
.
回答by adem
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)