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.
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}
:
- Open Project Properties (by right-clicking on the project)
- Select C/C++ General -> Preprocessor Include Paths, Macros, etc in the tree.
- Select Providers tab
- Select CDT GCC Built-in Compiler Settings from the list
- Replace the
${COMMAND}
in Command to get compiler specs: to your desired g++ executable.
Here is a screenshot to help:
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++
With overridden g++ to version 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:
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:
来源:https://stackoverflow.com/questions/37799287/whats-the-right-way-to-work-with-a-different-c-compiler-in-a-cdt-project