0

I'm trying to compile a c++ program with boost library that I compiled myself.

Here is how I compiled boost in ~/libs/

tar -xvzf boost_1_80_0.tar.gz
cd boost_1_80_0/
./bootstrap.sh --prefix=. 
./b2 -std=C++11
./b2  headers
./b2 install

Among others, it creates the file boost_1_80_0/stage/lib/libboost_filesystem.a.

Here is my c++ code:

#include <boost/filesystem.hpp>
#include <iostream>

int main()
{
    boost::filesystem::path directory = ".";
    boost::filesystem::directory_iterator end_itr;
    for( boost::filesystem::directory_iterator itr(directory); itr!=end_itr;++itr  )
    {
        boost::filesystem::path pathname=itr->path();
        std::cout<< pathname << std::endl;
    }
}

Now I try to compile with

LC_ALL=C  c++ -std=c++11 -I ~/libs/boost_1_80_0/stage/lib  ~/libs/boost_1_80_0/stage/lib/libboost_filesystem.a  ecm.cpp -o ecm

I have a bunch of errors:

/usr/bin/ld: /tmp/ccXuD16q.o: in function `boost::filesystem::detail::dir_itr_imp::~dir_itr_imp()':
ecm.cpp:(.text._ZN5boost10filesystem6detail11dir_itr_impD2Ev[_ZN5boost10filesystem6detail11dir_itr_impD5Ev]+0x27): undefined reference to `boost::filesystem::detail::dir_itr_close(void*&, void*&)'
/usr/bin/ld: /tmp/ccXuD16q.o: in function `boost::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const&, boost::filesystem::directory_options)':
ecm.cpp:(.text._ZN5boost10filesystem18directory_iteratorC2ERKNS0_4pathENS0_17directory_optionsE[_ZN5boost10filesystem18directory_iteratorC5ERKNS0_4pathENS0_17directory_optionsE]+0x38): undefined reference to `boost::filesystem::detail::directory_iterator_construct(boost::filesystem::directory_iterator&, boost::filesystem::path const&, unsigned int, boost::system::error_code*)'
/usr/bin/ld: /tmp/ccXuD16q.o: in function `boost::filesystem::directory_iterator::increment()':
ecm.cpp:(.text._ZN5boost10filesystem18directory_iterator9incrementEv[_ZN5boost10filesystem18directory_iterator9incrementEv]+0x1d): undefined reference to `boost::filesystem::detail::directory_iterator_increment(boost::filesystem::directory_iterator&, boost::system::error_code*)'
collect2: error: ld returned 1 exit status

Am I missing an include ?

EDIT:

There is a difference between this question and some other linked ones : the precise directory which has to be included for an user-compiled boost.

SOLUTION:

There are two errors:

  • the order of the arguments as pointed out by Alan Birtles and n. 1.8e9-where's-my-share m.
  • the boost directory to be included (to find boost/filesystem.hpp)

The correct line seems to be

c++ -std=c++11 ecm.cpp -o ecm  -I /libs/boost/boost_1_80_0/  /libs/boost/boost_1_80_0/stage/lib/libboost_filesystem.a
Laurent Claessens
  • 547
  • 1
  • 3
  • 18
  • 1
    You need to put libraries after the objects/cpp files that use them on your command line – Alan Birtles Oct 01 '22 at 06:33
  • @AlanBirtles there is a specific canonical dupe for this (https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) I cannot edit and add it as am on a phone. – n. m. could be an AI Oct 01 '22 at 07:03

0 Answers0