简体   繁体   中英

How can I compile with different version of g++ in c++ Autotools project

I have a big autotools project, and one part of codes in the subdirectory use g++-4.9 to compile, others use g++8.2.

My question is how to compile the whole project with different version of g++.

I've see some related question is to change a different g++ compiler, and general answers are to set environment variables or make options.

However, my issue is to compile with both g++8.2 and g++4.9 in same time.

I expect there are some solutions to set Makefile.am like :

noinst_PROGRAMS=foo bar
foo_CXX_COMPILER=/usr/bin/g++-4.9

bar_CXX_COMPILER=/usr/bin/g++-8.2

EDIT:

More Details that I've tried :

  1. The third-party library in the sub-project will show a lot of warnings " auto_ptr is deprecated" when compiling with g++-4.9 -std=c++11 , but without any error and executing well.
  2. It compiled well with no error and warning with g++-4.9 -std=c++98 .
  3. It yells many errors "undefined reference to ..." when I compile with g++-8.2 , even if I add the flag -std=c++98.

I guess this is because g++-8.2 compiler cannot recognize auto_ptr usage!

I prefer to use only one compiler which makes the problem simply! But, if the case cannot be allowed to use only one, I would want to know how to set up Makefile.am with different two compilers, or any best way to solve this compilation problem !

My question is how to compile the whole project with different version of g++.

It is possible, but I wouldn't recommend it. The GNU Build System doesn't have any support for that, but you might have a look at how AX_CC_FOR_BUILD accomplishes a similar idea (enabling a compiler for build as well as for host).

Idea #2 is what @JohnBollinger suggested -- pick a compiler and build with it. End users of your build will appreciate this more than having to setup two compilers. This is the solution I would go with, since the GNU Build System works this way. g++-8.2 can be told to compile code from previous versions of C++ if the old code can't be modernized for some reason.

Idea #3 is basically to split your project into two autotools projects -- one compiling with the old compiler and one with the new compiler. Not really a fan of this idea either.

I have a big autotools project, and one part of codes in the subdirectory use g++-4.9 to compile, others use g++8.2.

My question is how to compile the whole project with different version of g++.

I don't recommend doing that. The ABI conventions might have changed (so compiling with two different GCCs of different ABIs is IMHO not recommended).

Actually, I recommend building all the project with the same (newest) GCC, that is with g++ 8.2

If some parts of the project are using an different dialect of C++, you could explicitly pass some -std=c++11 or -std=c++17 options to them.

So just configure your project to use the same (and latest) GCC. If some C++ dialects are different, pass a specific flag to it. See options controlling the C++ dialect and-std= option .

At last, you could consider patching the source code of the old library to make it C++14 compliant (in particular, remove every occurrence of auto_ptr and replace them wisely with unique_ptr etc...). In most cases, it is worthwhile to do that (and perhaps a newer version of that old library already exists, which is C++14 or C++17)

Mixing two different versions of the C++ standard library is certainly painful and should be avoided. If you insist trying that, you need to understand painfully all the gory details (so I really recommend not trying this). Then read Drepper's How to write shared libraries paper.

Another approach could be to have your software use two different processes (one for the old C++98 source code, another for the new C++14 code) and use inter-process communication facilities. This is perhaps the most robust solution.

For practical purposes, consider old C++98 and new C++14 as two different, and incompatible , programming languages (C++11 was really different of its predecessors).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM