如何使用CMake的提升链接C ++程序 [英] How to link C++ program with Boost using CMake
问题描述
我应该CMake的文件应该是什么样子的Ubuntu下我的程序与boost库链接?
该故障运行make的过程中显示:
的main.cpp :(文字+ 0x3b):未定义的引用`提振:: program_options :: options_description :: m_default_line_length
主文件是非常简单的:
的#include<升压/ program_options / options_description.hpp>
#包括LT&;升压/ program_options / option.hpp>
使用命名空间std;
#包括LT&;&iostream的GT;命名空间PO =的boost :: program_options;INT主(INT ARGC,字符** argv的){ PO :: options_description递减(允许的选项);
desc.add_options()
(帮助,生产帮助信息)
; 返回0;
}
我已经成功地做到这一点,我已经加入到我的CMake文件的唯一线路是:
target_link_libraries(
my_target_file
$ {} Boost_PROGRAM_OPTIONS_LIBRARY
)
在CMake的你可以使用 find_package
来找到你所需要的库。
目前通常是 FindBoost.cmake
与CMake的-安装沿。
据我记得它将被安装到的/ usr /共享/ cmake的/模块/
以及其他发现,脚本公共库。
你可以只检查该文件中的文档,它是如何工作的详细信息。
我不工作了,所以我可以提供我的头了一个例子:
FIND_PACKAGE(升压1.40组件program_options必填)
INCLUDE_DIRECTORIES($ {} Boost_INCLUDE_DIR)ADD_EXECUTABLE(anyExecutable myMain.cpp)TARGET_LINK_LIBRARIES(anyExecutable $ {} Boost_LIBRARIES)
希望这code有助于。
- 下面是关于FindBoost.cmake 官方文档。
- 和实际 FindBoost.cmake (托管在Github上)
What should my cmake file should look like for linking my program with boost library under Ubuntu?
The errors show during running 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:
target_link_libraries(
my_target_file
${Boost_PROGRAM_OPTIONS_LIBRARY}
)
In CMake you could use find_package
to find libraries you need.
There usually is a FindBoost.cmake
along with your CMake-Installation.
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 info about how it works.
I'm not at work now, so I could just provide 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 ${Boost_LIBRARIES} )
Hope this code helps.
- Here's the official documentation about FindBoost.cmake.
- And the actual FindBoost.cmake (hosted on Github)
这篇关于如何使用CMake的提升链接C ++程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!