g++ 4.1.2 mixed with g++ 4.6

£可爱£侵袭症+ 提交于 2019-12-02 01:03:13

The ABI for those two GCC versions is compatible, the problem is that the object compiled by GCC 4.6 might depend on symbols that are only defined by the newer GCC's C++ standard library (e.g. if you use the std::fstream constructor taking a std::string your object will have a dependency on that symbol, which is only present in recent versions of GCC that support C++11.)

It will work as long as you link to the libstdc++ from GCC 4.6 (which is libstdc++.so.6.0.16) i.e. by linking with -L /path/to/gcc-4.6/lib

You must also ensure that newer version of the library is found at run-time, i.e. by telling the dynamic loader to use that library, using one of the methods listed in the libstdc++ manual

For example:

$ cat x.cc
#include <vector>
#include <fstream>
#include <string>
int main()
{
    std::string s = "output";
    std::ofstream f(s);
    std::vector<int> v(3);
    int n;
    for (auto i : v)
        ++n;
    f << n << '\n';
}
$ g++-4.6 -std=c++0x x.cc -c
$ g++-4.1 x.o
x.o: In function `main':
x.cc:(.text+0x5c): undefined reference to `std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::_Ios_Openmode)'
collect2: ld returned 1 exit status
$ g++-4.1 x.o -L /path/to/4.6/lib64 -Wl,-rpath,/path/to/4.6/lib64
$ ./a.out
$ cat output
3
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!