g++ 4.1.2 mixed with g++ 4.6

此生再无相见时 提交于 2019-12-02 03:46:24

问题


So, here's the thing. To develop plugins for Maya on Linux, we have to compile with GCC 4.1.2. But this compiler doesn't support any of the new C++0x features.

would it be possible to do something like this:

gcc-4.6 -o test.cpp.o -c test.cpp
gcc-4.1.2 -o exec_test test.cpp.o

I have serious doubt it would be possible, but worth asking.

If this is not possible, is there a way to achieve something similar ?


回答1:


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


来源:https://stackoverflow.com/questions/11528885/g-4-1-2-mixed-with-g-4-6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!