CrazyC
CrazyC

Reputation: 1896

Compilation error with boost on LINUX

I just installed the boost library on Linux and written a sample application :

#include <iostream>
#include <string>
#include "boost/date_time/gregorian/gregorian.hpp"

int main()
{
  std::string ds("2002-JAN-01");
  boost::gregorian::date d(boost::gregorian::from_string(ds));
  std::cout<< boost::gregorian::to_simple_string(d) <<std::endl;
  std::cout<< d<<std::endl;
}

I am compiling it as

gcc -I /home/test/code/thirdParty/boost_1_46_1/ -L /home/test/code/thirdParty/boost_1_46_1/stage/lib/ test.cpp

but getting lots of error as:

/tmp/ccAfgB8z.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0xd3): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0xec): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0xf8): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x104): undefined reference to `boost::system::system_category()'
/tmp/ccAfgB8z.o: In function `__tcf_4':
test.cpp:(.text+0x2be): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccAfgB8z.o: In function `main':
test.cpp:(.text+0x2d5): undefined reference to `std::allocator<char>::allocator()'
test.cpp:(.text+0x2e7): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
test.cpp:(.text+0x2f0): undefined reference to `std::allocator<char>::~allocator()'
test.cpp:(.text+0x2fd): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test.cpp:(.text+0x316): undefined reference to `std::allocator<char>::~allocator()'
test.cpp:(.text+0x33e): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
test.cpp:(.text+0x357): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
test.cpp:(.text+0x379): undefined reference to `std::cout'
test.cpp:(.text+0x37e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test.cpp:(.text+0x386): 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+0x38b): 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> >&))'
test.cpp:(.text+0x394): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
test.cpp:(.text+0x3ad): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
test.cpp:(.text+0x3c2): undefined reference to `std::cout'
test.cpp:(.text+0x3cf): 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+0x3d4): 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> >&))'
test.cpp:(.text+0x418): undefined reference to `std::locale::locale()'
test.cpp:(.text+0x431): undefined reference to `std::locale::~locale()'
test.cpp:(.text+0x43d): undefined reference to `std::cout'
test.cpp:(.text+0x442): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test.cpp:(.text+0x462): undefined reference to `std::locale::~locale()'
test.cpp:(.text+0x470): 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> >&)'

Anyone have any idea? In normal compilation i don't use any std library in the command line option.

Upvotes: 0

Views: 2491

Answers (2)

GrafikRobot
GrafikRobot

Reputation: 3049

As mentioned by Mat you need to use g++ (or the language option of gcc). But when you resolve that you will also need to link to the Boost System library (-lboost_system -- plus possibly a tag on the name to match your build type).

Upvotes: 1

Mat
Mat

Reputation: 206659

You need to compile C++ code with g++, not gcc.

Upvotes: 3

Related Questions