Eclipse 中 gcc 编译器的默认标志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2183888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:22:42  来源:igfitidea点击:

Default flags for gcc compiler in Eclipse

ceclipsegcceclipse-cdt

提问by Pieter

I want all my C programs to be compiled with the options -Wall -pedantic -ansiby default. Is there a way to have Eclipse add these flags to the compiler command by default for all projects?

我希望我的所有 C 程序都-Wall -pedantic -ansi默认使用这些选项进行编译。有没有办法让 Eclipse 默认将这些标志添加到所有项目的编译器命令中?

采纳答案by user151019

Assign CFLAGS to include those values, and have Eclipse run a tool that uses that environment variable by default when compiling (such as make).

分配 CFLAGS 以包含这些值,并让 Eclipse 运行一个在编译时默认使用该环境变量的工具(例如 make)。

You may have to specify environment variables before running Eclipse (and then they get inherited when Eclipse runs make) but there might be a way to specify default environment in Eclipse.

您可能必须在运行 Eclipse 之前指定环境变量(然后在 Eclipse 运行 make 时它们会被继承),但可能有一种方法可以在 Eclipse 中指定默认环境。

(I don't use Eclipse, so I'll have to see about installing and testing this; or maybe this answer can jog someone's memory, if so, feel free to edit.)

(我不使用 Eclipse,所以我必须看看安装和测试这个;或者这个答案可以唤起某人的记忆,如果是这样,请随意编辑。)



As an aside, you might want -std=c99 instead of -ansi. The -ansi option simply means -std=c89 or -std=c++98, depending on whether you're compiling C or C++, and both of those standards are showing their age.

顺便说一句,您可能需要 -std=c99 而不是 -ansi。-ansi 选项仅表示 -std=c89 或 -std=c++98,这取决于您编译的是 C 还是 C++,而这两个标准都显示出它们的时代性。



I installed Eclipse inside a VM running Windows to test this, and, even though CFLAGS is in the environment, Eclipse doesn't use it. Eclipse also pretends (by displaying text like "make all" and "make clean") that it's running make in a few situations/projects I tried, when it is not really using make (probably using some internal engine). This answer was on the wrong track for Eclipse.

我在运行 Windows 的 VM 中安装了 Eclipse 来测试这个,并且,即使 CFLAGS 在环境中,Eclipse 也不使用它。Eclipse 还假装(通过显示诸如“make all”和“make clean”之类的文本)它在我尝试过的一些情况/项目中运行 make,当它没有真正使用 make(可能使用某些内部引擎)时。这个答案对 Eclipse 来说是错误的。

回答by user151019

Assuming you are using Eclipse's internal builder goto Preferences->C/C++ Build->Settings

假设您使用的是 Eclipse 的内部构建器,请转到 Preferences->C/C++ Build->Settings

Choose the warnings section for the compiler, there are tick boxes for -Wall and -pedantic For -ansi set in Miscellaneous

选择编译器的警告部分, -Wall 和 -pedantic 的勾选框为 -ansi 设置在 Miscellaneous

As the OP notes this is just for each project not a global setting

正如 OP 所指出的,这仅适用于每个项目,而不是全局设置

回答by fyal

Eclipse on Windows: For a project: Properties -> C/C++ Build -> Setting than "Tool Setting" tab. select "CGG C++ Compiler" than at the right side you will see Command : g++ modify it to Command: g++ CFLAGS for instance if you like to have C++11 support modify as Command: g++ --std=c++11

Windows 上的 Eclipse:对于项目:属性 -> C/C++ 构建 -> 设置而不是“工具设置”选项卡。选择“CGG C++ Compiler”,然后在右侧您将看到 Command : g++ 将其修改为 Command: g++ CFLAGS 例如,如果您希望 C++11 支持修改为 Command: g++ --std=c++11

PS: This modification will valid for only current project and for only current configuration. If you want it for all configurations modify each configuration (Run, Debug) similarly.

PS:此修改仅对当前项目和当前配置有效。如果您希望它用于所有配置,请类似地修改每个配置(运行、调试)。

回答by Ricket

EDIT: I see that the OP runs Windows from a prior comment, however the following information maybenefit users of Eclipse on the Linux platform, if Eclipse honors the alias.

编辑:我从先前的评论中看到 OP 运行 Windows,但是如果 Eclipse 尊重别名,以下信息可能会使 Linux 平台上的 Eclipse 用户受益。



Are you running Eclipse in Linux? If so, try aliasing the gcc command; run this at a terminal:

你是在 Linux 中运行 Eclipse 吗?如果是这样,请尝试别名 gcc 命令;在终端上运行:

alias gcc='gcc -Wall -pedantic -ansi'

This is a common method in Linux to specify default parameters for an application. However, Eclipse might execute the actual gcc application and ignore the alias; I have not tested it.

这是 Linux 中为应用程序指定默认参数的常用方法。但是,Eclipse 可能会执行实际的 gcc 应用程序并忽略别名;我没有测试过。

回答by ant

Yes, Run as -> Run configuration -> 1st Tab is "Main" , choose the second tab(the one next to it) , you have there arguments box, paste -Wall -pedantic -ansiand just apply then run. Every next time you run you'll have these arguments as default

是的,Run as -> Run configuration -> 1st Tab is "Main" ,选择第二个选项卡(它旁边的那个),你有参数框,粘贴-Wall -pedantic -ansi并应用然后运行。每次运行时,您都会将这些参数作为默认值