freebsd 代码移植时 gcc 相关bug

来源:互联网 发布:网络销售沟通技巧 编辑:程序博客网 时间:2024/05/19 01:08

在移植linux代码到 freebsd 系统时,出现各种错误。现将所有错误一一罗列下来,并给出相关的解决方案。

to_string is not a member of std

编辑如下文件 test.cpp

#include <string>using namespace std;int main(){    string s = std::to_string(42);    cout << s << endl;    return 0;}

编译指令:

gcc -std=c++11 test.cpp test

会报错:

test.cpp: In function 'int main()':test.cpp:6:18: error: 'to_string' is not a member of 'std'  std::string s = std::to_string(42);

freebsd 官方的mail list 里找到原因是默认的 g++ 没有打开对 c++11 的支持。详情见文末参考链接1。

On 8.x one of the gcc ports has to be used if you need a c++11 library,
but it appears that the declarations of to_string in bits/basic_string.h
are hidden behind #ifdef _GLIBCXX_USE_C99 and that macro isn’t defined
on FreeBSD because we are missing a few obscure c99 functions.
I think it would be best to patch the gcc ports to force the definition
of that macro.

问题在于没有定义宏_GLIBCXX_USE_C99 gcc 默认不开启 c++11 支持。

需要使用如下编译指令:

g++ -std=c++11 -D_GLIBCXX_USE_C99 test.cpp -o test

链接运行时库

我的工程的编译指令如下:

g++48 -std=c++11 -D_GLIBCXX_USE_C99 -Wall -g main.cpp server.cpp mysh.cpp pipeVector.cpp clientPool.cpp fifo.cpp -o remote_csh

编译能通过,但在运行时会报错:

/usr/local/lib/compat/libstdc++.so.6: version GLIBCXX_3.4.17 required by /net/other/2016_1/0440052/remote_work/remote_csh not found

又是在freebsd 官方的论坛里找到答案,详情见参考链接2。

运行指令

strings /usr/local/lib/gcc47/libstdc++.so.6|grep GLIBCXX

输出如下

GLIBCXX_3.4GLIBCXX_3.4.1GLIBCXX_3.4.2GLIBCXX_3.4.3GLIBCXX_3.4.4GLIBCXX_3.4.5GLIBCXX_3.4.6GLIBCXX_3.4.7GLIBCXX_3.4.8GLIBCXX_3.4.9GLIBCXX_3.4.10GLIBCXX_3.4.11GLIBCXX_3.4.12GLIBCXX_3.4.13GLIBCXX_3.4.14GLIBCXX_3.4.15GLIBCXX_3.4.16GLIBCXX_3.4.17GLIBCXX_3.4.18GLIBCXX_3.4.19GLIBCXX_DEBUG_MESSAGE_LENGTH

发现version GLIBCXX_3.4.17 就在动态库 /usr/local/lib/gcc47/libstdc++.so.6 中,而 gcc 默认链接的是库 /usr/local/lib/compat/libstdc++.so.6

可以使用ldd remote_csh 来验证。

如此以来,原因就清晰了,我们需要链接运行时库/usr/local/lib/gcc47/libstdc++.so.6

只需修改编译指令为:

g++48 -std=c++11 -D_GLIBCXX_USE_C99 -Wall -g main.cpp server.cpp mysh.c    pp pipeVector.cpp clientPool.cpp fifo.cpp -o remote_csh -R/usr/local/lib/gcc48/

这里需要值得注意的是:

现代连接器在处理动态库时将链接时路径(Link-time path)和运行时路径(Run-time path)分开,用户可以通过-L指定连接时库的路径,通过-R(或-rpath)指定程序运行时库的路径

关于 Linux 静态库与动态库搜索路径设置详解可进一步,见参考链接3。

参考

c++11-lib on FreeBSD 8.4:Mail List

Howto: building and running c++0x apps on FreeBSD

http://blog.chinaunix.net/uid-29025972-id-3855495.html

0 0
原创粉丝点击