traverse directory with boost

G

Gary Wessle

hi
I did not find any way to traverse a given directory in c++ till I
found boost, not sure if there many boost(ers) out here. but this is
given me some error I cannot locate a fix.

thanks

**************** code ****************
#include <iostream>
#include <string>
#include "boost/filesystem/operations.hpp" // includes boost/filesystem/path.hpp
#include "boost/filesystem/fstream.hpp"
namespace fs = boost::filesystem;

using namespace std;

int main(){

fs::path full_path("../data");
if (exists(full_path)) cout << "yes" << endl;

}
//there exists data directory in the parent of the current directory.

**************** error ****************
$ make
g++ -gdwarf-2 -c -o try.o try.cpp
g++ -Wall -gdwarf-2 -o proj try.o
try.o: In function `main':
/home/fred/myPrograms/backtest/try/try.cpp:11: undefined reference to `boost::filesystem::path::path(char const*)'
/home/fred/myPrograms/backtest/try/try.cpp:12: undefined reference to `boost::filesystem::exists(boost::filesystem::path const&)'
collect2: ld returned 1 exit status
make: *** [proj] Error 1
 
A

Alex Buell

$ make
g++ -gdwarf-2 -c -o try.o try.cpp
g++ -Wall -gdwarf-2 -o proj try.o
try.o: In function `main':
/home/fred/myPrograms/backtest/try/try.cpp:11: undefined reference to
`boost::filesystem::path::path(char
const*)' /home/fred/myPrograms/backtest/try/try.cpp:12: undefined
reference to `boost::filesystem::exists(boost::filesystem::path
const&)' collect2: ld returned 1 exit status make: *** [proj] Error 1

you need:

g++ -Wall -gdwarf2 -o proj try.o -lboost_filesystem

(i.e. you aren't linking against the boost library)
 
G

Gary Wessle

Alex Buell said:
$ make
g++ -gdwarf-2 -c -o try.o try.cpp
g++ -Wall -gdwarf-2 -o proj try.o
try.o: In function `main':
/home/fred/myPrograms/backtest/try/try.cpp:11: undefined reference to
`boost::filesystem::path::path(char
const*)' /home/fred/myPrograms/backtest/try/try.cpp:12: undefined
reference to `boost::filesystem::exists(boost::filesystem::path
const&)' collect2: ld returned 1 exit status make: *** [proj] Error 1

you need:

g++ -Wall -gdwarf2 -o proj try.o -lboost_filesystem

(i.e. you aren't linking against the boost library)

why is it then that I was able to run this without linking against the
boost library


****************************************************************
#include <iostream>
using std::cout;
using std::endl;
#include<string>
using std::string;
#include <vector>
using std::vector;
#include "boost/format.hpp"
using boost::format;

int main(){
string s("abcde");
vector<char> v (s.begin(), s.end());
format fmter("%1% %3t%2%");
fmter % v[0];
fmter % v[1];
cout << fmter.str() << '\n';
}

no linking against the boost library here
**************** **************** ****************
CXXFLAGS = -gdwarf-2
OBJS := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
COMP = g++

proj: $(OBJS)
$(COMP) -Wall -gdwarf-2 -o proj $(OBJS)

#-Wall turns on all warnings
#-gdwarf-2 for dubugging note gdb manual 12.4.1
clean:
rm -rf *.o proj
 
G

Gary Wessle

I did not post the output in my last post, here it is

$ make clean; make
rm -rf *.o proj
g++ -gdwarf-2 -c -o try.o try.cpp
g++ -Wall -gdwarf-2 -o proj try.o
$
 
K

Kai-Uwe Bux

Gary said:
Alex Buell said:
$ make
g++ -gdwarf-2 -c -o try.o try.cpp
g++ -Wall -gdwarf-2 -o proj try.o
try.o: In function `main':
/home/fred/myPrograms/backtest/try/try.cpp:11: undefined reference to
`boost::filesystem::path::path(char
const*)' /home/fred/myPrograms/backtest/try/try.cpp:12: undefined
reference to `boost::filesystem::exists(boost::filesystem::path
const&)' collect2: ld returned 1 exit status make: *** [proj] Error 1

you need:

g++ -Wall -gdwarf2 -o proj try.o -lboost_filesystem

(i.e. you aren't linking against the boost library)

why is it then that I was able to run this without linking against the
boost library

There are several boost libraries.
****************************************************************
#include <iostream>
using std::cout;
using std::endl;
#include<string>
using std::string;
#include <vector>
using std::vector;
#include "boost/format.hpp"
using boost::format;

int main(){
string s("abcde");
vector<char> v (s.begin(), s.end());
format fmter("%1% %3t%2%");
fmter % v[0];
fmter % v[1];
cout << fmter.str() << '\n';
}

no linking against the boost library here
**************** **************** ****************
CXXFLAGS = -gdwarf-2
OBJS := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
COMP = g++

proj: $(OBJS)
$(COMP) -Wall -gdwarf-2 -o proj $(OBJS)

#-Wall turns on all warnings
#-gdwarf-2 for dubugging note gdb manual 12.4.1
clean:
rm -rf *.o proj

Different code, different requirements. Read the documentation for the
libraries you are using. They will tell you what to tell the linker.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top