简体   繁体   English

在CDT项目中使用不同C ++编译器的正确方法是什么?

[英]What's the right way to work with a different C++ compiler in a CDT project?

I use Eclipse CDT Mars.2 (and Neon RC), on Linux. 我在Linux上使用Eclipse CDT Mars.2(和Neon RC)。 My distribution's default C++ compiler is GCC 5.3.1, but for some of my work I use GCC 4.9.3. 我的发行版的默认C ++编译器是GCC 5.3.1,但对于我的一些工作,我使用的是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. 我想关于我的项目的一切使用GCC 4.9.3:工具发现,C ++标准库,包含文件路径,索引器,预处理 - 所有这些。

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... 似乎Eclipse有相当拜占庭的“提供者”和“工具链”配置,我不想进行设置,我以后无法撤消...

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. 注意:我确实尝试在某些预处理器包含等提供程序设置中用/usr/bin/g++-4.9替换${COMMAND} ,这确实导致发现4.9.3相关的包含文件,但我的索引器没有不喜欢这样,所有std ::的东西都显示为红色且未解决。 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. 标准制作意味着您正在编写自己的Makefile并自行管理所有这些。 Managed Make means you are letting CDT create the Makefiles and manage them. Managed Make意味着您让CDT创建Makefile并管理它们。

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. 要更改使用的编译器,您需要用您选择的编译器替换${COMMAND} It is up to you (as the user) to ensure that this command matches what you are using in your Makefile. 您(作为用户)可以确保此命令与您在Makefile中使用的命令相匹配。

To change the ${COMMAND} : 要更改${COMMAND}

  1. Open Project Properties (by right-clicking on the project) 打开项目属性 (通过右键单击项目)
  2. Select C/C++ General -> Preprocessor Include Paths, Macros, etc in the tree. 在树中选择C / C ++ General - > Preprocessor Include Paths,Macros等
  3. Select Providers tab 选择提供商选项卡
  4. Select CDT GCC Built-in Compiler Settings from the list 从列表中选择CDT GCC内置编译器设置
  5. Replace the ${COMMAND} in Command to get compiler specs: to your desired g++ executable. 替换${COMMAND}${COMMAND} 以获取编译器规范:到您想要的g ++可执行文件。

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. 在我的机器上,我有/usr/bin/g++是版本5.3,而/usr/bin/g++-4.7是版本4.7。

With default g++ 使用默认的g ++

克++ 5

With overridden g++ to version 4 将g ++重写为版本4

克++ 4

Use Environment manage 使用环境管理

The problem with the above is that you need to coordinate g++ between your Makefile and the build settings. 上面的问题是你需要在Makefile和构建设置之间协调g ++ One solution to this is to use the C/C++ Build Environment settings to define CXX as the compiler to use. 解决此问题的一种方法是使用C / C ++ Build Environment设置将CXX定义为要使用的编译器。 Set the CXX environment variable in the project settings ( Project Properties -> C/C++ Build -> Environment ) or global preferences ( Preferences -> C/C++ -> Build -> Environment ). 在项目设置( 项目属性 - > C / C ++构建 - > 环境 )或全局首选项( 首选项 - > C / C ++ - > 构建 - > 环境 )中设置CXX环境变量。

Then replace ${COMMAND} with ${CXX} . 然后用${CXX}替换${COMMAND} ${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. 相反,如果您使用的是Managed Make,则需要覆盖各个工具的构建设置。 These settings then feed into the Preprocessor Include Paths, Macros, etc. settings as used directly by Standard Make. 然后,这些设置将提供给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: 项目属性 - > C / C ++构建 - > 设置开始 ,然后编辑以下每个:

  • GCC C++ Compiler -> Normally set to g++ GCC C ++编译器 - >通常设置为g++
  • GCC C Compiler -> Normally set to gcc GCC C编译器 - >通常设置为gcc
  • GCC C++ Linker -> Normally set to g++ GCC C ++链接器 - >通常设置为g++

Here is a screenshot to demonstrate: 这是一个演示截图:

mananged制作截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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