traverse directory with boost

Discussion in 'C++' started by Gary Wessle, Aug 6, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    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
     
    Gary Wessle, Aug 6, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Alex Buell Guest

    On 06 Aug 2006 13:37:20 +1000, I waved a wand and this message
    magically appeared from Gary Wessle:

    > $ 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)
    --
    http://www.munted.org.uk

    Take a nap, it saves lives.
     
    Alex Buell, Aug 6, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Gary Wessle Guest

    Alex Buell <> writes:

    > On 06 Aug 2006 13:37:20 +1000, I waved a wand and this message
    > magically appeared from Gary Wessle:
    >
    > > $ 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
     
    Gary Wessle, Aug 6, 2006
    #3
  4. Gary Wessle

    Gary Wessle Guest

    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
    $
     
    Gary Wessle, Aug 6, 2006
    #4
  5. Gary Wessle

    Kai-Uwe Bux Guest

    Gary Wessle wrote:

    > Alex Buell <> writes:
    >
    >> On 06 Aug 2006 13:37:20 +1000, I waved a wand and this message
    >> magically appeared from Gary Wessle:
    >>
    >> > $ 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
     
    Kai-Uwe Bux, Aug 6, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thieum22
    Replies:
    1
    Views:
    725
    Joe Smith
    Aug 6, 2004
  2. Newbie

    How to Traverse a Directory

    Newbie, Sep 21, 2006, in forum: Ruby
    Replies:
    6
    Views:
    170
    Newbie
    Sep 21, 2006
  3. jay
    Replies:
    0
    Views:
    139
  4. Traverse a directory

    , Jul 13, 2006, in forum: Perl Misc
    Replies:
    11
    Views:
    294
  5. glob and Traverse Directory

    , Jul 14, 2006, in forum: Perl Misc
    Replies:
    7
    Views:
    127
    Tad McClellan
    Jul 15, 2006
Loading...

Share This Page