如何使用 CMake 将 C++ 程序与 Boost 链接起来 [英] How to link C++ program with Boost using CMake

查看:17
本文介绍了如何使用 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.

  • Here's the official documentation about FindBoost.cmake.
  • And the actual FindBoost.cmake (hosted on GitHub)

这篇关于如何使用 CMake 将 C++ 程序与 Boost 链接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆