如何使用 CMake 将 C++ 程序与 Boost 链接起来
在 Ubuntu 下将我的程序与 Boost 库链接起来时,我的 CMake 文件应该是什么样的?
What should my CMake file look like for linking my program with the Boost library under Ubuntu?
运行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 文件中的唯一几行是:
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}
)
推荐答案
在 CMake 中,您可以使用 find_package
来查找您需要的库.通常有一个 FindBoost.cmake
与您的 CMake 安装一起.
In CMake you could use find_package
to find libraries you need. There usually is a FindBoost.cmake
along with your CMake installation.
据我所知,它将与其他公共库的查找脚本一起安装到 /usr/share/cmake/Modules/
.您可以查看该文件中的文档,了解有关其工作原理的更多信息.
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.
我脑海中的一个例子:
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.
- 这是关于 FindBoost.cmake 的官方文档.
- 和实际的 FindBoost.cmake(托管在 GitHub 上))
- Here's the official documentation about FindBoost.cmake.
- And the actual FindBoost.cmake (hosted on GitHub)
相关文章