4

写个boost测试是否安装成功demo

#include<iostream>
#include<boost/lexical_cast.hpp>
int main(){
    int a = boost::lexical_cast<int>("123456");
    std::cout << a <<std::endl;
    return 0;
}

编译错误

test.cpp:(.text+0x24): undefined reference to `std::cout'
test.cpp:(.text+0x29): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
test.cpp:(.text+0x31): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0x39): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccG8Wb2k.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x61): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x66): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccG8Wb2k.o: In function `std::exception::exception()':
4

3 回答 3

8

如果您使用gccinstead of g++,则不会自动链接 C++ 库。这是来自man g++

但是,使用 gcc 并没有添加 C++ 库。g++ 是一个调用 GCC 并自动指定与 C++ 库的链接的程序。除非使用 -x,否则它将 .c、.h 和 .i 文件视为 C++ 源文件而不是 C 源文件。在预编译扩展名为 .h 的 C 头文件以用于 C++ 编译时,此程序也很有用。在许多系统上,g++ 也以 c++ 的名称安装。

正如其他人所说,在调用结束时g++直接使用或链接。-lstdc++类似的东西gcc main.cpp -lstdc++

于 2014-12-17T04:20:10.107 回答
0

这编译并运行 g++ 4.8.1 没有问题。输出:

123456
于 2013-09-09T12:24:49.277 回答
-1

使用这个: g++ fileName.cpp -o Filename 这将输出要运行的文件。我希望这能帮到您。问候马丁 Z

于 2014-12-17T04:07:31.800 回答