What's the right way to work with a different C++ compiler in a CDT project?
Asked Answered
C

1

6

I use Eclipse CDT Mars.2 (and Neon RC), on Linux. My distribution's default C++ compiler is GCC 5.3.1, but for some of my work I use GCC 4.9.3. I would like everything regarding my project to use GCC 4.9.3: The tool discovery, the C++ standard library, the include file paths, the indexer, the preprocessing - all of it.

What's the right way to do this? It seems Eclipse has rather byzantine "providers" and "toolchains" configurations and I do not want to make settings I won't be able to undo later...

Note: I did try to replace ${COMMAND} with /usr/bin/g++-4.9 in some of the Preprocessor Includes etc. provider settings, and this did result in 4.9.3-related include files being discovered, but my indexer didn't like that and all of the std::stuff showed up red and unresolved. Then I tried looking for where I set the compiler version used for indexing but I couldn't find that.

Cardiogram answered 13/6, 2016 at 21:17 Comment(8)
I don't think Eclipse will come clean with a configuration like that. I use Eclipse CDT as well, but I just end up sticking with whatever compiler my distribution provides. But then one would ask why you'd want to use an older version of GCC?Twelvemonth
@Poriferous: Lots of reasons. Use of CUDA, C++11 ABI issues, cross-compilation etc. Also, if what you're saying is true then what's the use of all of their configuration machinery? I'm skeptical they actually hard-code /usr/bin/cc anywhere.Cardiogram
This is the problem with Eclipse CDT because it assumes a GCC version beforehand, much like it assumes a certain Java version beforehand. It's not as flexible as another IDE like Codeblocks. Unfortunately we bear witness that Linux lacks a concrete C++ IDE.Twelvemonth
I don't know if it's the right way, but setting environment variables (PATH and LD_LIBRARY_PATH) to make the compiler version you want the default one before launching eclipse is simple and works like a charm.Mandrake
@URaoul: But how can it work like a charm? Both my compilers are in the same directory (but have different names); and my LD_LIBRARY_PATH has nothing in it directly relating to the compiler (well, ok, there's my /opt/boost which was compiled with GCC 4.9.3, but that shouldn't matter much).Cardiogram
@Cardiogram you're right, i should have explained my context : compilers are installed in different folders. But, maybe you could restore that context by creating one folder by compiler and symlinking gcc and libs to the real compiler. And then, just set PATH (and LD_LIBRARY_PATH if installed in non standard path) in a way that your folder with symlinks is found first.Mandrake
I have tried to answer your question, but have not properly dealt with your "Note" about things going wrong. Hopefully my answer can help move in the right direction.Pintle
@Twelvemonth I'm using Qt Creator for all C/C++ project, even not related to Qt, on both Windows and Linux. It's written in C++/Qt so its blazingly fast. And it's not as complicated as Eclipse.Heretic
P
4

There are two possible answers, depending on whether you are doing "Standard Make" or "Mangaged Make". Standard Make means you are writing your own Makefiles and managing all that yourself. Managed Make means you are letting CDT create the Makefiles and manage them.

Standard Make

For standard make, everything is controlled by what the scanner discovers. The scanner handles finding all the system include files that are part of your project, and those are fed into the indexer to resolve symbols and also allows things like header file navigation.

To change the compiler that is used, you need replace ${COMMAND} with your compiler of choice. It is up to you (as the user) to ensure that this command matches what you are using in your Makefile.

To change the ${COMMAND}:

  1. Open Project Properties (by right-clicking on the project)
  2. Select C/C++ General -> Preprocessor Include Paths, Macros, etc in the tree.
  3. Select Providers tab
  4. Select CDT GCC Built-in Compiler Settings from the list
  5. Replace the ${COMMAND} in Command to get compiler specs: to your desired g++ executable.

Here is a screenshot to help:

command edit

To see this in action, here are some screenshots with and without the change described. On my machine I have /usr/bin/g++ which is version 5.3 and /usr/bin/g++-4.7 which is version 4.7.

With default g++

g++5

With overridden g++ to version 4

g++4

Use Environment manage

The problem with the above is that you need to coordinate g++ between your Makefile and the build settings. One solution to this is to use the C/C++ Build Environment settings to define CXX as the compiler to use. Set the CXX environment variable in the project settings (Project Properties -> C/C++ Build -> Environment) or global preferences (Preferences -> C/C++ -> Build -> Environment).

Then replace ${COMMAND} with ${CXX}.

Here is a screenshot that demonstrates what I described:

environment

Managed Make

If, instead, you are using Managed Make, you need to override the build settings for the individual tools. These settings then feed into the Preprocessor Include Paths, Macros, etc. settings as used directly by Standard Make.

To change the build settings, you need to override the command used for the compiler in a few places, once for each type of tool. Start in Project Properties -> C/C++ Build -> Settings and then edit each of these:

  • GCC C++ Compiler -> Normally set to g++
  • GCC C Compiler -> Normally set to gcc
  • GCC C++ Linker -> Normally set to g++

Here is a screenshot to demonstrate:

mananged make screenshot

Pintle answered 6/7, 2016 at 9:26 Comment(1)
I'll get back to you in a couple of days, thanks for the detailed answer.Cardiogram

© 2022 - 2024 — McMap. All rights reserved.