I\'m trying to run program, using sample code of boost::filesystem on Ubuntu 12.10, but it doesn\'t want to build.
#include
#include
For some boost modules, you have to compile libraries and link them (using bootstrap.sh).
In your case, you have to compile and link Filesystem
, and probalbly System
too
Have a look here
For example:
If you are linking on Windows, you don't have to manually link your libraries, since they are automatically linked using pragma. On Linux, you have to do it.
According to documentation, these modules need you to acquire or build a library :
You need to add libboost_filesystem library when linking. Or libboost_filesystem-mt if your application is multi-threaded. Like this:
g++ -o file -lboost_filesystem-mt source_file.cpp
You need to add the following libraries:
g++ -o file -lboost_system -lboost_filesystem sourcefile.cpp
If you use a Makefile:
CC=gcc
CXX=g++
PROG = program
CXXFLAGS := -std=c++1y -g -Wall
LDFLAGS = -L/usr/local/lib -L/usr/lib/x86_64-linux-gnu
LIBS= -lboost_system -lboost_filesystem
SRCS= main.cpp
OBJS=$(subst .cpp,.o,$(SRCS))
all: $(OBJS)
$(CXX) $(CXXFLAGS) -o $(PROG) $(OBJS) $(LIBS) $(LDFLAGS)
Boost filesystem is one of the Boost library that have some ABI problem relative to function signature change due to C++0x or C++11. cf Boost ticket : https://svn.boost.org/trac/boost/ticket/6779
You have three solutions:
Inhibit C++11 scoped enums in concerned Boost header files included in your programs with #include (cf http://www.ridgesolutions.ie/index.php/2013/05/30/boost-link-error-undefined-reference-to-boostfilesystemdetailcopy_file/):
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
But this solution is not a complete one and I read it does not work for everybody.
Build BOOST with the C++11 option (the same options you use for your application): http://hnrkptrsn.github.io/2013/02/26/c11-and-boost-setup-guide
I read also it does not work for everybody.
Set up a cross compiler dedicated to your application where you rebuild all the libraries you need in a dedicated environment. That ensures coherence plus stability plus more maintainability, and is certainly the solution to recommend. I have not read if it has been tested - probably yes, and probably it works. Anyway, cross compiling is well mastered now in computer science. You will find many good tutorials and support for it. In Linux Gentoo, they have the marvelous sys-devel/crossdev package that makes it very easy.
In my own case, solution 1 has solved the problem. As soon as I encounter another one, I will switch to solution 3. So, I have not yet tested it.
The solution that worked for me is to compile with "-c" and then create the executable like this:
g++ -c -o main.o main.cpp
g++ -o my_prog main.o -lboost_system -lboost_filesystem