Eclipse中gcc编译器的默认标志

时间:2022-03-24 07:01:05

I want all my C programs to be compiled with the options -Wall -pedantic -ansi by 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默认为所有项目向编译器命令添加这些标志?

5 个解决方案

#1


2  

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运行时它们会被继承),但是在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的错误轨道上。

#2


1  

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

假设您正在使用Eclipse的内部构建器goto首选项—>C/ c++构建—>设置

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

为编译器选择警告部分,在杂项中有-Wall和-pedantic for -ansi设置的标记框。

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

正如OP所指出的,这只是针对每个项目而不是全局设置

#3


1  

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++ Build ->设置,而不是“工具设置”选项卡。在右边选择“CGG c++编译器”,您将看到命令: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:此修改仅适用于当前项目和当前配置。如果您希望它适用于所有配置,请类似地修改每个配置(运行、调试)。

#4


-1  

EDIT: I see that the OP runs Windows from a prior comment, however the following information may benefit 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应用程序并忽略别名;我还没有测试过。

#5


-1  

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 -ansi and just apply then run. Every next time you run you'll have these arguments as default

是的,运行为->运行配置-> 1标签是“Main”,选择第二个标签(旁边的那个),你有参数框,粘贴- wall -pedantic -ansi,然后应用然后运行。每次运行时,这些参数都是默认的

#1


2  

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运行时它们会被继承),但是在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的错误轨道上。

#2


1  

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

假设您正在使用Eclipse的内部构建器goto首选项—>C/ c++构建—>设置

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

为编译器选择警告部分,在杂项中有-Wall和-pedantic for -ansi设置的标记框。

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

正如OP所指出的,这只是针对每个项目而不是全局设置

#3


1  

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++ Build ->设置,而不是“工具设置”选项卡。在右边选择“CGG c++编译器”,您将看到命令: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:此修改仅适用于当前项目和当前配置。如果您希望它适用于所有配置,请类似地修改每个配置(运行、调试)。

#4


-1  

EDIT: I see that the OP runs Windows from a prior comment, however the following information may benefit 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应用程序并忽略别名;我还没有测试过。

#5


-1  

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 -ansi and just apply then run. Every next time you run you'll have these arguments as default

是的,运行为->运行配置-> 1标签是“Main”,选择第二个标签(旁边的那个),你有参数框,粘贴- wall -pedantic -ansi,然后应用然后运行。每次运行时,这些参数都是默认的