g++ 4.1.2 mixed with g++ 4.6

2019-02-25 21:30发布

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 ?

标签: gcc g++ version
1条回答
【Aperson】
2楼-- · 2019-02-25 22:11

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
查看更多
登录 后发表回答