我正在尝试编译并运行一个C ++ 11程序,该程序使用Boost.Filesystem - 比如列出目录的内容 - 在共享的Web托管环境中。由于此系统上安装的软件通常已有4年的历史,我需要使用更新版本的GCC和Boost来编译和运行该程序,我已经在我的主目录中编译并在本地安装了GCC和Boost。
我的问题是,虽然我现在能够编译和运行不使用Boost.Filesystem库的C ++ 11程序,但只要我尝试运行使用该库的代码,就会发生运行时错误。对于我编写的一个程序,它在堆中的地址上生成了无效的free()
。对于另一个程序(本文中显示的程序),我得到了一个分段错误。
可能导致此问题的原因,我该如何解决?
我怀疑这个问题存在于这个系统预先安装的GCC和Boost可能会重复编译器的事实。
我在~/local/
下安装了GCC版本6.0.0和Boost版本1.60.0。因此g++
可执行文件位于~/local/bin/g++
,C ++运行时和Boost的共享对象位于~/local/lib64/
和~/local/lib/
等等。当我编译或运行我的程序时,我设置LD_LIBRARY_PATH=$HOME/local/lib64:$HOME/local/lib
以便使用本地安装的共享对象(GCC需要自己运行三个外部库)。我已使用ldd
确认了这一点。
这是我试图开始工作的测试程序:
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost;
int main(int argc, char **argv) {
if(argc != 2) {
cerr << "Usage: " << argv[0] << " <directory>\n";
return 1;
}
const char *dirname = argv[1];
for(filesystem::directory_iterator i(dirname), n; i != n; ++i) {
cout << *i << '\n';
}
return 0;
}
编译和运行该程序的结果是:
% LD_LIBRARY_PATH=$HOME/local/lib64:$HOME/local/lib $HOME/local/bin/g++ -std=c++11 -Wall -B$HOME/local/bin -B$HOME/local/include -B$HOME/local/lib64 -B$HOME/local/lib -lboost_system -lboost_filesystem
% LD_LIBRARY_PATH=$HOME/local/lib64:$HOME/local/lib ./a.out .
Segmentation fault
回溯:
#0 0x00000037c42897fb in memcpy () from /lib64/libc.so.6
#1 0x00007ffff79217b4 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, unsigned long) ()
at /home/myuser/install-c++11/build-dir/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/char_traits.h:290
#2 0x00007ffff7921826 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_leak_hard() ()
at /home/myuser/install-c++11/build-dir/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.tcc:902
#3 0x00007ffff7bef6cc in boost::filesystem::path::m_append_separator_if_needed() () from /home/myuser/local/lib/libboost_filesystem.so.1.60.0
#4 0x00007ffff7bef76f in boost::filesystem::path::operator/=(boost::filesystem::path const&) () from /home/myuser/local/lib/libboost_filesystem.so.1.60.0
#5 0x00007ffff7bed472 in boost::filesystem::detail::directory_iterator_construct(boost::filesystem::directory_iterator&, boost::filesystem::path const&, boost::system::error_code*) ()
from /home/myuser/local/lib/libboost_filesystem.so.1.60.0
#6 0x0000000000402c17 in boost::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const&) ()
#7 0x0000000000402488 in main ()
可执行文件找到的共享对象是:
% LD_LIBRARY_PATH=$HOME/local/lib64:$HOME/local/lib ldd ./a.out
linux-vdso.so.1 => (0x00007fffa21e0000)
libboost_system.so.1.60.0 => /home/myuser/local/lib/libboost_system.so.1.60.0 (0x00007f4775468000)
libboost_filesystem.so.1.60.0 => /home/myuser/local/lib/libboost_filesystem.so.1.60.0 (0x00007f4775250000)
libstdc++.so.6 => /home/myuser/local/lib64/libstdc++.so.6 (0x00007f4774eb8000)
libm.so.6 => /lib64/libm.so.6 (0x00000037c4600000)
libgcc_s.so.1 => /home/myuser/local/lib64/libgcc_s.so.1 (0x00007f4774c88000)
libc.so.6 => /lib64/libc.so.6 (0x00000037c4200000)
librt.so.1 => /lib64/librt.so.1 (0x00000037c5600000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00000037c4a00000)
/lib64/ld-linux-x86-64.so.2 (0x00000037c3e00000)
系统信息:
% uname -a
Linux my.sharedhost.com 3.12.50-39.ELK6.x86_64 #1 SMP Mon Nov 2 03:10:26 CST 2015 x86_64 x86_64 x86_64 GNU/Linux