Reputation: 247
My code below kept running into segmentation fault error and i can't seem to figure out why. Help pls~~ My goal is to read a folder and all of its subfolders to find all files ending with extension ".txt" so i am using boost's recurisve directory iterator to help achieve the task. This issue arrived suddenly as my code was running fine last week.
test.cpp:
#include <sstream>
#include <iostream>
#include "/home/dj/boost_1_65_1/boost/filesystem.hpp"
using namespace std;
int main()
{
using namespace boost::filesystem;
recursive_directory_iterator end;
for (recursive_directory_iterator it("./"); it != end; ++it)
{
std::cout << *it << endl;
}
return 0;
}
i am running on Linux and i compile my test.cpp with boost 1.65 as static :
g++ -g -I /home/dj/boost_1_65_1 test.cpp -static -static-libgcc -o delete -static-libstdc++ -std=c++11 -L/home/dj/boost_1_65_1 -lboost_filesystem -lboost_system
with gdb and some cout i found that error came from the line below.
for (recursive_directory_iterator it("./"); it != end; ++it)
somehow when i call for "recursive_directory_iterator", my system crashes giving this error
Program received signal SIGSEGV, Segmentation fault.
0x00000000004f1c8b in memcpy ()
the exact same error persists even if i reduce the code to
int main()
{
using namespace boost::filesystem;
recursive_directory_iterator it("./");
return 0;
}
Upvotes: 3
Views: 970
Reputation: 392893
The code is fine, the following is reduced for style:
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main()
{
for (recursive_directory_iterator it("./"), end; it != end; ++it) {
std::cout << it->path() << std::endl;
}
}
Notes:
the code is incorrect for more recent versions of boost (*it
is no longer output-streamable)
you attempt to link to a cusomt built version of the Boost libraries in /home/dj/boost_1_65_1
. However, you specify the linker directory -L/home/dj/boost_1_65_1
whereas usually the libraries get built into stage/lib
so you'd expect -L/home/dj/boost_1_65_1/stage/lib
to find the correct version of the libraries.
Most likely you link the wrong version of the libraries (not matching the headers you use at compile-time).
To diagnose what libraries are getting linked at runtime use ldd
. For example for my example:
g++ -L /home/sehe/custom/boost_1_67_0/stage/lib/ -I /home/sehe/custom/boost_1_67_0/ test.cpp -lboost_system -lboost_filesystem
You get for ldd a.out
:
linux-vdso.so.1 => (0x00007fff0bfaf000)
libboost_system.so.1.67.0 => not found
libboost_filesystem.so.1.67.0 => not found
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f5e8fcb2000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f5e8fa9a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5e8f6d0000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f5e8f3c7000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5e9003e000)
Indeed, you can see I have no libboost_system.so.1.67.0
or libboost_filesystem.so.1.67.0
in a system library directory, and it won't find them. Starting would fail:
$ ./a.out
./a.out: error while loading shared libraries: libboost_system.so.1.67.0: cannot open shared object file: No such file or directory
You can inform the runtime linker of your library path:
LD_LIBRARY_PATH=~/custom/boost_1_67_0/stage/lib ./a.out
Upvotes: 2