Boost Asio GCC链路错误

Boost Asio GCC Link Error

本文关键字:错误 链路 GCC Asio Boost      更新时间:2023-10-16

我刚刚用xubuntu 12.10安装了一个干净的虚拟机,我正试图移植一些在Windows上完美工作的c++代码。首先,我已经安装了Virtualbox guest插件和GCC,可以编译代码了。

我已经从互联网上下载了boost库(boost_1_52),我已经从asio网站(boost_asio_1_4_8)中删除了asio库,我已经安装了多线程,使用这些说明共享链接版本:

./bootstrap.sh --prefix=/usr &&
./b2 stage threading=multi link=shared
as root: 

我知道一个事实,boost工作,因为我已经能够在这里编译测试应用程序(与lboost_regex链接),它工作得很好:

#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
    while (std::cin)
    {
       std::getline(std::cin, line);
        boost::smatch matches;
           if (boost::regex_match(line, matches, pat))
           std::cout << matches[2] << std::endl;
    }
}

所以我正在尝试构建一个ASIO示例,我之前在Windows上构建过,没有问题。文件在这里:

http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/examples.html

:

boost_asio/example/serialization/client.cpp
boost_asio/example/serialization/connection.hpp
boost_asio/example/serialization/server.cpp
boost_asio/example/serialization/stock.hpp

我抛出我的编译器this:

gcc client.cpp -I/usr/include/boost -lboostrongystem -lboost_thread -lboostrongerialization

这给了我这个错误:

connection.hpp:75:35: error: template argument 1 is invalid
connection.hpp:75:35: error: template argument 2 is invalid
connection.hpp:75:44: error: invalid type in declaration before ‘;’ token
connection.hpp:76:13: error: request for member ‘push_back’ in ‘buffers’, which is of non-class type ‘int’
connection.hpp:76:23: error: ‘asio’ is not a class or namespace
connection.hpp:77:13: error: request for member ‘push_back’ in ‘buffers’, which is of non-class type ‘int’
connection.hpp:77:23: error: ‘asio’ is not a class or namespace
connection.hpp:78:5: error: ‘asio’ is not a class or namespace
connection.hpp:78:23: error: ‘socket_’ was not declared in this scope
connection.hpp: In member function ‘void s11n_example::connection::async_read(T&, Handler)’:
connection.hpp:87:15: error: ‘asio’ does not name a type
connection.hpp:87:31: error: expected unqualified-id before ‘&’ token
connection.hpp:87:31: error: expected ‘)’ before ‘&’ token
connection.hpp:87:31: error: expected initializer before ‘&’ token
connection.hpp:90:5: error: ‘asio’ has not been declared
connection.hpp:90:22: error: ‘socket_’ was not declared in this scope
connection.hpp:90:31: error: ‘asio’ has not been declared
connection.hpp:91:21: error: ‘f’ was not declared in this scope
connection.hpp:92:17: error: ‘asio’ has not been declared
client.cpp: At global scope:
client.cpp:26:10: error: ‘asio’ has not been declared
client.cpp:26:26: error: expected ‘)’ before ‘&’ token
client.cpp:43:29: error: ‘asio’ does not name a type
client.cpp:43:45: error: expected unqualified-id before ‘&’ token
client.cpp:43:45: error: expected ‘)’ before ‘&’ token
client.cpp:43:35: error: expected ‘;’ at end of member declaration
client.cpp:43:47: error: ISO C++ forbids declaration of ‘e’ with no type [-fpermissive]
client.cpp:43:47: error: expected ‘;’ at end of member declaration
client.cpp:43:48: error: expected unqualified-id before ‘)’ token
client.cpp:125:1: error: expected ‘}’ at end of input
client.cpp:125:1: error: expected unqualified-id at end of input
client.cpp:125:1: error: expected ‘}’ at end of input

我真的很困惑,就好像我没有建立boost或者我错过了另一个链接。我也试过链接Winsock,没有结果。请帮助!

欢呼

您可以互换使用gcc和g++。不能工作的行使用gcc,但可以工作的行使用g++。使用g++而不是gcc可能会影响使用的默认包含路径。您最初的错误是没有链接。它在编译。此外,如果使用boost版本,则asio名称空间不是asio。boost:: asio。

看起来boost/asio.hpp没有被正确包含。

我不记得前缀选项的确切作用,但我认为你的问题可能在那里的某个地方。boost目录可能不在/usr/include/boost,而可能在/usr/boost

这是一种可能性。第二个是,您需要传递/usr/include,而不是传递/usr/include/boost,即

gcc client.cpp -I /usr/include -lboost_system -lboost_thread -lboost_serialization 

如果您查看示例文件,例如connection.cpp示例,它包含boost/asio.hppboost/部分指的是一个文件夹,编译器应该在使用-I指定的包含路径中查找该文件夹。因此,如果指定了/usr/include/boost,编译器将查找/usr/include/boost/boost/asio.hpp(注意boost出现了两次)。

我想我现在已经解决了这个问题。使用bjam和自动安装程序似乎完成不了多少工作(由于某些原因,它无法解析这些路径)。

无论如何,我下载了ASIO源代码(这次不是boost),并把它放在我桌面上的一个目录中。与我在windows中使用Visual Studio的方式类似,我设法让它链接到:

g++ client.cpp -I/home/devbox/desktop/asio-1.5.3/include -   L/home/devbox/Desktop/boost_1_53_0/stage/lib -lboost_system -lboost_thread -lboost_serialization -o test

欢呼所有的