代码之家  ›  专栏  ›  技术社区  ›  Scott

链接Linux中的Boost库

  •  28
  • Scott  · 技术社区  · 15 年前

    我正试图用Boost的ASIO构建一个项目,但我遇到了一些麻烦。最初,我尝试在没有任何附加库的情况下构建项目,因为所有内容都应该在头文件中。

    我尝试构建的程序如下所示:

    #include <iostream>
    #include <boost/asio.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>
    
    int main()
    {
        boost::asio::io_service io;
        boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
    
        t.wait();
    
        std::cout << "Hello, world!" << std::endl;
    
        return 0;
    }
    

    可以找到它 here 在Boost的网站上。

    所以,起初我只是:

    -I /usr/include/boost_1_40_0
    

    这导致以下错误:

    make -k all
    Building target: HelloWorld
    Invoking: GCC C++ Linker
    g++  -o"HelloWorld"  ./main.o  
    ./main.o: In function `__static_initialization_and_destruction_0':
    /usr/include/boost_1_40_0/boost/system/error_code.hpp:205: undefined reference to `boost::system::get_system_category()'
    /usr/include/boost_1_40_0/boost/system/error_code.hpp:206: undefined reference to `boost::system::get_generic_category()'
    /usr/include/boost_1_40_0/boost/system/error_code.hpp:211: undefined reference to `boost::system::get_generic_category()'
    /usr/include/boost_1_40_0/boost/system/error_code.hpp:212: undefined reference to `boost::system::get_generic_category()'
    /usr/include/boost_1_40_0/boost/system/error_code.hpp:213: undefined reference to `boost::system::get_system_category()'
    ./main.o: In function `boost::asio::error::get_system_category()':
    /usr/include/boost_1_40_0/boost/asio/error.hpp:218: undefined reference to `boost::system::get_system_category()'
    ./main.o: In function `error_code':
    /usr/include/boost_1_40_0/boost/system/error_code.hpp:312: undefined reference to `boost::system::get_system_category()'
    ./main.o: In function `posix_tss_ptr':
    /usr/include/boost_1_40_0/boost/asio/detail/posix_tss_ptr.hpp:47: undefined reference to `pthread_key_create'
    ./main.o: In function `~posix_tss_ptr':
    /usr/include/boost_1_40_0/boost/asio/detail/posix_tss_ptr.hpp:61: undefined reference to `pthread_key_delete'
    ./main.o: In function `boost::asio::detail::posix_thread::join()':
    /usr/include/boost_1_40_0/boost/asio/detail/posix_thread.hpp:77: undefined reference to `pthread_join'
    ./main.o: In function `~posix_thread':
    /usr/include/boost_1_40_0/boost/asio/detail/posix_thread.hpp:69: undefined reference to `pthread_detach'
    collect2: ld returned 1 exit status
    make: *** [HelloWorld] Error 1
    make: Target `all' not remade because of errors.
    

    看来我需要系统库。所以,我按照“入门指南”上的说明 here 它给了我一堆位于 /usr/include/boost_1_40_0/阶段/lib .其中包括 libboost系统.a . 因此,我试图用以下方法进行编译:

    -I /usr/include/boost_1_40_0
    -L /usr/include/boost_1_40_0/stage/lib
    -l libboost_system
    

    但是,我得到了:

    make -k all
    Building target: HelloWorld
    Invoking: GCC C++ Linker
    g++ -L/usr/lib -L/usr/include/boost_1_40_0/stage/lib -o"HelloWorld"  ./main.o   -llibboost_system
    /usr/bin/ld: cannot find -llibboost_system
    collect2: ld returned 1 exit status
    make: *** [HelloWorld] Error 1
    make: Target `all' not remade because of errors.
    

    我不知道为什么,但它似乎无法识别图书馆或我尝试的任何其他图书馆。我可能做错什么了?事先谢谢!

    3 回复  |  直到 7 年前
        1
  •  32
  •   Patrizio Bertoni    9 年前

    更改 -llibboost_system -lboost_system .

    在Linux中,在引用所述库时,不使用库前面的“lib”前缀。

        2
  •  25
  •   Sam Miller    12 年前

    在本例中,james的回答是正确的,但是如果有其他人像我一样偶然发现了这篇文章,那么请注意,如果将旧的boost头链接到新的库中,您就可以收到这条消息。 get_system_category() 具体来说已经被否决了。我在无意中包括发行版提供的头文件但与我自己的Boost内部副本链接时遇到了这个问题。

        3
  •  20
  •   Jeff    12 年前

    如果仍有问题,您可能希望通过添加到链接器标志来包括POSIX线程:

    -lpthread