C ++:所有提升路径操作段错误(OSX / GCC) [英] C++: All boost path operations segfault (OSX / GCC)
问题描述
我得到一致的设计缺陷与几乎所有的操作,我试图用升压路径来执行。
(编辑:看来,所有功能段错误都与的current_path()
)
样例程序:#包括LT&;升压/文件系统/ operations.hpp>
#包括LT&;升压/文件系统/ path.hpp>
#包括LT&;&iostream的GT;使用命名空间std;
使用空间boost ::文件系统;
使用空间boost ::制度;
INT主(INT ARGC,为const char * argv的[])
{
错误_ code走错了路。
自动P =路径(你好/../世界);
COUT<< p.string()&所述;&下; ENDL;
路径为c =规范(P,ERR);
COUT<< c.string()&所述;&下; ENDL;
}
以上只是一个例子,下面还有段错误:结果自动P =的current_path(ERR);
和:结果自动P = initial_path(ERR);
与编译:结果 G ++ - 4.9 -lboost_filesystem -lboost_system -std = C ++ 11的main.cpp -o ./path-test
输出:
你好/../世界
段故障:11
GCC和升压通过安装的家酿
系统规格:
OSX:10.9.4
GCC:4.9.1
升压:1.0.55_2
编辑:
编译时 -g
并安装了信号处理程序按照评论,输出:
你好/../世界
段错误:
0道测试0x000000010ea215b8 _Z7handleri + 28
1 libsystem_platform.dylib 0x00007fff8b9285aa _sigtramp + 26
2? 0x00007fff67bdf1a1为0x0 + 140734933889441
3道测试0x000000010ea2196d _ZN5boost10filesystem9canonicalERKNS0_4pathERNS_6system10error_ codeE + 69
4道测试0x000000010ea21518主+ 138
5 libdyld.dylib 0x00007fff832c35fd启动+ 1
6 ??? 0x0000000000000001为0x0 + 1
段错误信号处理器(从<一个拍摄href=\"http://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes\">this问题):
无效处理程序(INT SIG)
{
void *的数组[10];
为size_t的大小; 大小=回溯(阵列,10); fprintf中(标准错误,段错误:\\ n);
backtrace_symbols_fd(数组的大小,STDERR_FILENO);
出口(1);
}
您正在混合C ++标准库的实现。
升压,当通过BREW安装将使用编译铛++
。该工具链使用的libc ++
默认
G ++
坚持使用它自己的的libstdc ++
实施
这些实现不是二进制兼容的,这是问题所在而产生的。
我提取提升的全新副本到一个子目录,做了:
$ ./bootstrap.sh - preFIX =的/ usr /本地/ boost156 CXXFLAGS = - 拱i386的-arch x86_64的地址模型= 32_64线程=多的MacOS -version = 10.9工具= G ++ - 4.8级
然后内置它(仅限静态;还有的构建问题,即它无法在OSX在这种情况下动态库 - LD抱怨说, -h
选项不支持):
$ ./b2 --layout =标记线程=多链接=静态工具= GCC-4.8
当我编译你的code(因为线程=多,我不得不-mt添加到链接选项):
$ G ++ - 4.8 -g -std = C ++ 11 -Iboost_1_56_0 -Lboost_1_56_0 /台/ lib目录-lboost_filesystem-MT -lboost_system-MT的main.cpp -o ./path-测试
$ ./path-test
你好,世界$
即。它在这种情况下,工作就好了。
这是什么意思?
- C ++在OSX库是一个完整的PITA如果你想混
G ++
和铛++
- ,因为所有的
铛++
code默认为正与的libc ++
内置你将必须有任何C ++
库的专用副本,如果你打算用G ++
来构建它们 - 自制只是奉命行事当它与
铛++编译
这是一个烂摊子,但如果你坚持的&lt;&嘲讽GT; 一真编译器的&LT; /嘲讽&gt;中,你会没事的。 TBH我preFER铛的错误消息和静态分析是优秀的;但如果你必须使用 G ++
,你必须保持你想要的任何 C ++
图书馆传抄使用,还编译了 G ++
。
I am getting consistent segfaults with almost any operation I am trying to perform with boost path.
(Edit: It appears that all segfaulting functions are related to current_path()
)
Sample program:
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <iostream>
using namespace std;
using namespace boost::filesystem;
using namespace boost::system;
int main(int argc, const char * argv[])
{
error_code err;
auto p = path("hello/../world");
cout << p.string() << endl;
path c = canonical(p, err);
cout << c.string() << endl;
}
The above is just an example, the following also segfault:
auto p = current_path(err);
And:
auto p = initial_path(err);
Compiled with:
g++-4.9 -lboost_filesystem -lboost_system -std=c++11 main.cpp -o ./path-test
Output:
hello/../world
Segmentation fault: 11
Both GCC and Boost installed via Homebrew.
System specs:
OSX: 10.9.4
GCC: 4.9.1
Boost: 1.0.55_2
Edit:
Compiled with -g
and installed a signal handler as per comment, output:
hello/../world
Segfault:
0 path-test 0x000000010ea215b8 _Z7handleri + 28
1 libsystem_platform.dylib 0x00007fff8b9285aa _sigtramp + 26
2 ??? 0x00007fff67bdf1a1 0x0 + 140734933889441
3 path-test 0x000000010ea2196d _ZN5boost10filesystem9canonicalERKNS0_4pathERNS_6system10error_codeE + 69
4 path-test 0x000000010ea21518 main + 138
5 libdyld.dylib 0x00007fff832c35fd start + 1
6 ??? 0x0000000000000001 0x0 + 1
Segfault signal handler (Taken from this question):
void handler(int sig)
{
void *array[10];
size_t size;
size = backtrace(array, 10);
fprintf(stderr, "Segfault:\n");
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
You're mixing implementations of the C++ standard library.
Boost, when installed via brew will be compiled using clang++
. This toolchain uses libc++
by default.
g++
insists on using it's own libstdc++
implementation.
These implementations are not binary compatible, which is where the problems are arising.
I extracted a fresh copy of boost into a subdirectory, did a:
$ ./bootstrap.sh --prefix=/usr/local/boost156 cxxflags="-arch i386 -arch x86_64" address-model=32_64 threading=multi macos-version=10.9 toolset=g++-4.8 stage
Then built-it (static only; there's a build issue where it can't make the dynamic libraries in this situation under OSX - ld complains that the -h
option is not supported):
$ ./b2 --layout=tagged threading=multi link=static toolset=gcc-4.8
When I compiled your code (because of threading=multi, I had to add -mt to the link options):
$ g++-4.8 -g -std=c++11 -Iboost_1_56_0 -Lboost_1_56_0/stage/lib -lboost_filesystem-mt -lboost_system-mt main.cpp -o ./path-test
$ ./path-test
hello/../world
$
i.e. it worked just fine in this case.
What does this mean?
- C++ libraries on OSX are a complete PITA if you're trying to mix
g++
andclang++
- because all
clang++
code defaults to being built withlibc++
you're going to have to have private copies of anyc++
libraries if you intend to build them withg++
- homebrew is just following orders when it compiles with
clang++
It's a mess, but if you stick to the <sarcasm>one true compiler</sarcasm>, then you'll be fine. TBH I prefer clang's error messages and the static analysis is excellent; but if you have to use g++
, you'll have to keep private copies of any c++
libraries that you want to use, also compiled with g++
.
这篇关于C ++:所有提升路径操作段错误(OSX / GCC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!