2016-04-30 70 views
1

我正在尝试使用Boost的cpp-netlib编写一个超级小程序来解析示例C++文件中的HTTP URL。如何在我的C++代码中包含boost项目

的代码是微不足道的:

#include <boost/network/uri> 
#include <iostream> 

using namespace boost::network; 

int main() { 
    uri::uri instance("http://www.example.com"); 
    std::cout << "scheme: " << instance.scheme() << std::endl 
     << "host: " << instance.host() << std::endl; 

    return 0; 
} 

我的测试代码是在同一目录下,升压源。要编译我简单地做:

g++ -Icpp-netlib-0.12.0-final -o test test.cpp

它没有找到包括与-I虽然过去了,我得到一个致命的错误。

fatal error: 'boost/network/uri' file not found 
#include <boost/network/uri> 
     ^
1 error generated. 

什么是正确的方法来做到这一点,开始使用他们的库编写示例代码?

+0

我不知道增强多少,但你确定你应该使用boost/network/uri而不是'boost/network/uri.hpp'吗? – MikeCAT

+0

这给了我一个新的错误。致命错误:'#include '我在'boost/network/uri /'中看到了'config.hpp',但是在目录中没有看到'boost/config.hpp'。 – randombits

+0

我认为你的提升安装被破坏了。也许你没有安装开发包? – drescherjm

回答

2

从这个猜测:

My test code is in the same directory as where the boost source is.

如果你有什么是你解包boost头文件到你的源代码目录,那么你的包含语句不会工作。 #include <>仅搜索标准包含路径中的包含文件(/ usr/include &等等)。

所以,你可以尝试包括语句“”来代替,这将从目前的源目录搜索过,或者更好的是,使用包管理器在你的发行版安装官方推动开发包,您的计算机。

如果在源代码中有目录“cpp-netlib-0.12.0-final/boost”,编译器命令行参数“-Ipp-netlib-0.12.0-final”将帮助编译器找到boost你启动g ++的目录。

1
  1. 关于您的Boost安装,您是否正确安装并链接了Boost.date_time?大部分Boost只有标题;这里,Boost.systemBoost.Regex,并且不需要单独的二进制。但是,Boost.Date_time具有单独的二进制库,并且需要使用其他安装步骤。 [参考文献2]

    Although Boost is mostly header only, applications built using cpp-netlib still requires linking with Boost.System, Boost.Date_time, and Boost.Regex. [Ref 1]

  2. 是您的Boost库上面的1.50.0版本?

  3. 你设置你的BOOST_ROOT环境变量?

The path to the boost root directory (often /usr/local/boost_1_60_0) is sometimes referred to as $BOOST_ROOT in documentation and mailing lists [Ref 2]

  • 就是boost安装在/usr/local?您可能需要root访问权限才能执行此操作。

  • 您是否检查了安装的权限?如果你作为root安装,您可能需要chmod 755权限到非root帐户

  • 参考文献:
    1. http://cpp-netlib.org/0.12.0/getting_started.html#getting-started
    2。http://www.boost.org/doc/libs/1_60_0/more/getting_started/unix-variants.html

    希望这会有所帮助。