0

I am trying to compile this code in CodeBlocks

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

using namespace boost::filesystem;

int main()
{


  if ( !boost::filesystem::exists( "myfile.txt" ) )
{
  std::cout << "Can't find my file!" << std::endl;
}
}

With this compile flags:

g++.exe -Wall -fexceptions -g -O3 -pedantic-errors -Wall -std=c++0x -lboost_system -IC:\Users\moe\Desktop\boost_1_67_0 -c C:\Users\moe\Desktop\oo\main.cpp -o obj\Debug\main.o

But I always receive this error:

boost::system::generic_category()

this is the error log, that i receive when i compile the code:

Untitled4.o: In function `boost::system::error_category::std_category::equivalent(std::error_code const&, int) const':
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:733: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:736: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:748: undefined reference to `boost::system::generic_category()'
Untitled4.o: In function `boost::system::error_category::std_category::equivalent(int, std::error_condition const&) const':
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:703: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:706: undefined reference to `boost::system::generic_category()'
Untitled4.o: In function `boost::filesystem::path_traits::convert(char const*, char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&)':
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path::codecvt()'
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path_traits::convert(char const*, char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&, std::codecvt<wchar_t, char, int> const&)'
Untitled4.o: In function `boost::filesystem::exists(boost::filesystem::path const&)':
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/operations.hpp:446: undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
collect2.exe: error: ld returned 1 exit status
5
  • boost::system needs to be built and linked, it is one of the parts of boost that is not header only Commented May 13, 2018 at 14:25
  • But how I can do that ?
    – user9021371
    Commented May 13, 2018 at 14:27
  • 1
    @UnholySheep: Landa uses -lboost_system Commented May 13, 2018 at 14:28
  • @ThomasSablik but the linker directory is not being specified (-L) Commented May 13, 2018 at 14:31
  • @UnholySheep: The error message for missing linker directory is cannot find -lboost_system Commented May 13, 2018 at 14:36

2 Answers 2

2

Put the libraries at the end of the linker command line:

1
  • In this way g++.exe -Wall -fexceptions -g -O3 -pedantic-errors -Wall -std=c++0x -IC:\Users\moe\Desktop\boost_1_67_0 -c C:\Users\moe\Desktop\oo\main.cpp -o obj\Debug\main.o -lboost_system
    – user9021371
    Commented May 13, 2018 at 15:03
-1

Since your erro log also contained these: C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path::codecvt()'

It is clear that you also lack -lboost_filesystem.

So there is no use to just add -lboost_system to the g++ command.

You can add -lboost_system -lboost_filesystem to your g++ command.

0