4

Write a boost test whether the installation was successful demo

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

Compile error

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()':
sunysen
  • 2,265
  • 1
  • 12
  • 13
  • 10
    you are most probably invoking `gcc` instead of `g++`, the difference is that the latter, by default, brings in the relevant c++ data upon invoking the *linker*. if you, for some unknown reason, want to use `gcc` use something like the following `gcc test.cpp -lstdc++` – Filip Roséen - refp Sep 09 '13 at 12:22
  • Could you paste your compile command? Aren't you using gcc instead of g++ ? – Mateusz Kołodziejski Sep 09 '13 at 12:23

3 Answers3

8

If you use gcc instead of g++, the C++ library is not automatically linked. This is from man g++:

However, the use of gcc does not add the C++ library. g++ is a program that calls GCC and automatically specifies linking against the C++ library. It treats .c, .h and .i files as C++ source files instead of C source files unless -x is used. This program is also useful when precompiling a C header file with a .h extension for use in C++ compilations. On many systems, g++ is also installed with the name c++.

As others have stated, either use g++ directly or link -lstdc++ at the end of your invocation. Something like gcc main.cpp -lstdc++.

0

This compiles and runs no problem with g++ 4.8.1. Outputs:

123456
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
-1

Use this: g++ fileName.cpp -o Filename That will output the file to be run. I hope this will help you. Regards Martin Z