在 make 文件中包含 boost 库

发布于 2024-08-02 14:09:33 字数 5181 浏览 3 评论 0原文

我正在学习 Boost,但我的 make 文件遇到了问题。 这是我的基本 makefile:

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 库,这就是我遇到问题的地方。

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

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 

感谢您的帮助

I'm learning Boost and am having trouble with my makes files.
Here is my basic makefile:

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 

When acctime.cpp has no boost filesystem elements in it this m,ake file works fine.
As soon as I add boost filesystem elements I obviously need to make references to the boost libray in the make file this is where I am having issues.

The following line works for a single file compilation:

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

Now I'm trying to integrate this into the make file. I've tried many based on what information I can find on the web but none are working this is my latest:

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 

Unfortunately it stlill can't find the Boost libraries, can anyone help?
thanks

Having read the advice of the people who've answered I've now got this:

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 

But this still fails to link.

This is the error message I'm getting:

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

Following orsogufo's advice (thanks! much appreciated) now have this:

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 

Looking better, but still can't quite find the library:

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

I've double checked that location and the library is definately at:
/usr/local/boost/boost_1_39_0/stage/lib/libboost_filesystem-gcc41-mt.a

STill no joy, usimg this now:

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 

Getting:

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

It's working with this:

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 

Thanks for all your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空‖城人不在 2024-08-09 14:09:33

当您链接目标文件以创建可执行文件(您的第一个 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 code> (根据需要修改这两个)。

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

When you link the object files to create the executable (your first makefile rule) you must pass the location of the boost libraries with the -L flag and the names of the libraries with the -l flag.

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

where /usr/local/boost/boost_1_39_0/stage/lib is the directory containing the libraries and boost_filesystem the file name of the library without the beginning lib (modify those two as appropriate).

The .a file you're trying to link is the wrong one... the library should have no extension.

醉态萌生 2024-08-09 14:09:33

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

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

-L 库的路径
-l llibrary

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

以下是技巧:

  1. 打开终端并启动命令

    <块引用>

    $定位增强> 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. 这取决于您正在使用的 boost 功能/模块

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

    libboost_filesystem.so

    libboost_thread.so

    libboost_system.so

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

    -l lboost_filesystem

    -l lboost_thread

    -l lboost_system

希望这对您有帮助,如果有更简单的方法,请提出

Hi Following is the full procedure to add boost to cmake/make file. This answer is specially developed for the novice programmer in cpp.

If you want to add the boost library support with help of Makefile you need to specify library path (with -L option) and libraries (with -l option).

-L path/to/the/libraries
-l llibrary

**Now how to find path to libraries **

Following is the trick:

  1. Open terminal and fire command

    $ locate boost > libboost.txt

    $ gedit libboost.txt

  2. This command opens a text file which contains all boost library paths.

  3. Now find ("ctr+F") libboost, which highlights the library files of boost with extension .a and .so in this text file.

  4. Copy the path in which this .so files are present.

    eg: If .so file present as on /usr/lib/x86_64-linux-gnu/libboost_filesystem.so

    then specify path as : -L /usr/lib/x86_64-linux-gnu/

Now how to find respective library?

  1. This depends on what boost functionalities/modules you are using

    for example: If you are using boost threading, you will require following libraries

    libboost_filesystem.so

    libboost_thread.so

    libboost_system.so

  2. Add above libraries as follows with -l option:

    -l lboost_filesystem

    -l lboost_thread

    -l lboost_system

Hope this will helps you, further If there is more easy way, please suggest

我家小可爱 2024-08-09 14:09:33

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

You need to add the boost libraries to the linking phase (the accesstimer target line) and not to the compilation phase (where only the include path is needed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文