Eclipse中gcc编译器的默认标志

时间:2022-07-08 07:00:49

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运行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的错误轨道上。

#2


1  

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 -pedanticFor -ansi set in Miscellaneous

选择编译器的警告部分,在“杂项”中设置-Wall和-pedanticFor -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 ++构建 - >设置而不是“工具设置”选项卡。选择“CGG C ++ Compiler”而不是右侧你会看到命令:g ++将其修改为命令: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

是的,运行为 - >运行配置 - >第一个选项卡是“主”,选择第二个选项卡(旁边的选项卡),你有参数框,粘贴-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运行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的错误轨道上。

#2


1  

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 -pedanticFor -ansi set in Miscellaneous

选择编译器的警告部分,在“杂项”中设置-Wall和-pedanticFor -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 ++构建 - >设置而不是“工具设置”选项卡。选择“CGG C ++ Compiler”而不是右侧你会看到命令:g ++将其修改为命令: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

是的,运行为 - >运行配置 - >第一个选项卡是“主”,选择第二个选项卡(旁边的选项卡),你有参数框,粘贴-Wall -pedantic -ansi然后只需应用然后运行。每次下次运行时,都会将这些参数作为默认值