【问题标题】:Including boost libraries in make files在 make 文件中包含 boost 库
【发布时间】:2010-11-21 06:56:51
【问题描述】:

我正在学习 Boost,但我的 make 文件有问题。 这是我的基本生成文件:

accesstimer: acctime.o btimer.o
    g++ acctime.o btimer.o -o accesstimer

acctime.o: acctime.cpp btimer.h
    g++ -c acctime.cpp 

bentimer.o: btimer.cpp btimer.h
    g++ -c btimer.cpp 

当 acctime.cpp 中没有 boost 文件系统元素时,这个 m,ake 文件可以正常工作。 一旦我添加了 boost 文件系统元素,我显然需要在 make 文件中引用 boost libray,这就是我遇到问题的地方。

以下行适用于单个文件编译:

g++ -I /usr/local/boost/boost_1_39_0 boosttest1.cpp -o bt1 /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a

现在我正在尝试将其集成到 make 文件中。根据我在网上可以找到的信息,我已经尝试了很多,但没有一个有效,这是我最新的:

accesstimer: acctime.o bentimer.o
    g++ acctime.o bentimer.o -o accesstimer

acctime.o: acctime.cpp bentimer.h
    g++ -c -I /usr/local/boost/boost_1_39_0 acctime.cpp /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a

bentimer.o: bentimer.cpp bentimer.h
    g++ -c bentimer.cpp 

不幸的是,它仍然找不到 Boost 库,有人可以帮忙吗? 谢谢

阅读了回答者的建议后,我现在明白了:

accesstimer: acctime.o bentimer.o
    g++ -L /usr/local/boost/boost_1_39_0 acctime.o /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a bentimer.o -o accesstimer

acctime.o: acctime.cpp bentimer.h
    g++ -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h
    g++ -c bentimer.cpp 

但这仍然无法链接。

这是我收到的错误消息:

g++ -L /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a /usr/local/boost/boost_1_39_0/stage/lib/libboost_system-gcc41-mt.a acctime.o  bentimer.o -o accesstimer
acctime.o: In function boost::enable_if<boost::filesystem::is_basic_path<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >, bool>::type boost::filesystem::exists<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)':
acctime.cpp:(.text._ZN5boost10filesystem6existsINS0_10basic_pathISsNS0_11path_traitsEEEEENS_9enable_ifINS0_13is_basic_pathIT_EEbE4typeERKS7_[boost::enable_if<boost::filesystem::is_basic_path<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >, bool>::type boost::filesystem::exists<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)]+0x26): undefined reference to `boost::filesystem::detail::status_api(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int&)'
collect2: ld returned 1 exit status
make: *** [accesstimer] Error 1

按照 orsogufo 的建议(谢谢!非常感谢)现在有了这个:

accesstimer: acctime.o bentimer.o
    g++ -L/usr/local/boost/boost_1_39_0/stage/lib -llibboost_filesystem-gcc41-mt.a -llibboost_system-gcc41-mt.a acctime.o bentimer.o -o accesstimer  

acctime.o: acctime.cpp bentimer.h
    g++ -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h
    g++ -c bentimer.cpp 

看起来更好,但仍然找不到库:

g++ -L/usr/local/boost/boost_1_39_0/stage/lib -llibboost_filesystem-gcc41-mt.a -llibboost_system-gcc41-mt.a acctime.o bentimer.o -o accesstimer
/usr/bin/ld: cannot find -llibboost_filesystem-gcc41-mt.a
collect2: ld returned 1 exit status
make: *** [accesstimer] Error 1

我已经仔细检查了那个位置,图书馆肯定在: /usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a

仍然不高兴,现在使用这个:

accesstimer: acctime.o bentimer.o
    g++  -L/usr/local/boost/boost_1_39_0 -lboost_filesystem-gcc41-mt acctime.o bentimer.o -o accesstimer    

acctime.o: acctime.cpp bentimer.h
    g++ -I /usr/local/boost/boost_1_39_0 -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h
    g++ -c bentimer.cpp 

获取:

g++  -L/usr/local/boost/boost_1_39_0/stage/lib/ -llibboost_filesystem-gcc41-mt acctime.o bentimer.o -o accesstimer
/usr/bin/ld: cannot find -llibboost_filesystem-gcc41-mt
collect2: ld returned 1 exit status
make: *** [accesstimer] Error 1

它正在处理这个:

accesstimer: acctime.o bentimer.o
    g++ -L/usr/local/boost/boost_1_39_0/stage/lib -lboost_filesystem acctime.o bentimer.o -o accesstimer    

acctime.o: acctime.cpp bentimer.h
    g++ -I /usr/local/boost/boost_1_39_0 -c acctime.cpp 

bentimer.o: bentimer.cpp bentimer.h
    g++ -c bentimer.cpp 

感谢大家的帮助

【问题讨论】:

  • 能否请您发布您收到的错误消息?
  • 当您使用 -l 标志链接库时,您总是会忽略前导的“lib”。所以链接libm.a是-lm,libboost是-lboost等

标签: boost makefile linker


【解决方案1】:

当您链接目标文件以创建可执行文件(您的第一个 makefile 规则)时,您必须使用 -L 标志传递 boost 库的位置并使用 -l 标志传递库的名称强>。

accesstimer: acctime.o bentimer.o
    g++ -L/usr/local/boost/boost_1_39_0/stage/lib -lboost_filesystem acctime.o bentimer.o -o accesstimer

其中/usr/local/boost/boost_1_39_0/stage/lib 是包含库的目录,boost_filesystem 是不以lib 开头的库的文件名(根据需要修改这两个)。

您尝试链接的 .a 文件是错误的...该库应该没有扩展名。

【讨论】:

    【解决方案2】:

    您好以下是向 cmake/make 文件添加 boost 的完整过程。这个答案是专门为cpp中的新手程序员开发的。

    如果你想在 Makefile 的帮助下添加 boost 库支持,你需要指定库路径(使用 -L 选项)和库(使用 -l 选项)。

    -L path/to/the/libraries -l 图书馆

    **现在如何找到库的路径**

    诀窍如下:

    1. 打开终端并触发命令

      $ locate boost > libboost.txt

      $ gedit libboost.txt

    2. 此命令打开一个包含所有 boost 库路径的文本文件。

    3. 现在找到 ("ctr+F") libboost,在这个文本文件中高亮显示扩展名为 .a 和 .so 的 boost 库文件。

    4. 复制此 .so 文件所在的路径。

      例如:如果 .so 文件存在于 /usr/lib/x86_64-linux-gnu/libboost_filesystem.so

      然后指定路径为:-L /usr/lib/x86_64-linux-gnu/

    现在如何找到相应的库?

    1. 这取决于您使用的增强功能/模块

      例如:如果您使用增强线程,则需要以下库

      libboost_filesystem.so

      libboost_thread.so

      libboost_system.so

    2. 使用 -l 选项添加上述库如下:

      -l lboost_filesystem

      -l lboost_thread

      -l lboost_system

    希望对你有所帮助,如果有更简单的方法,请建议

    【讨论】:

      【解决方案3】:

      您需要将 boost 库添加到链接阶段(accesstimer 目标行)而不是编译阶段(只需要包含路径)。

      【讨论】:

        猜你喜欢
        • 2012-06-26
        • 2014-05-18
        • 2017-05-17
        • 1970-01-01
        • 2011-12-14
        • 2021-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多