C语言 如何使用终端在 gcc 中启用 c99 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25566597/
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-02 11:19:06  来源:igfitidea点击:

How enable c99 mode in gcc with terminal

cgccc99

提问by user297904

I want to activate c99 mode in gcc compiler to i read in other post in this forum that -stdshould be equal to -std=c99but i don't know how to set it to this value using command line so please help.

我想在 gcc 编译器中激活 c99 模式,我在本论坛的其他帖子中读到-std应该等于-std=c99但我不知道如何使用命令行将其设置为该值,所以请帮助。

回答by jpw

Compile using:

编译使用:

gcc -std=c99 -o outputfile sourcefile.c

gcc --helplists some options, for a full list of options refer to the manual. The different options for C dialect can be found here.

gcc --help列出了一些选项,有关选项的完整列表,请参阅手册。可以在此处找到 C 方言的不同选项。

As you are using makeyou can set the command line options for gcc using CFLAGS:

在使用时,make您可以使用以下命令设置 gcc 的命令行选项CFLAGS

# sample makefile
CC = gcc
CFLAGS = -Wall -std=c99
OUTFILE = outputfile
OBJS = source.o
SRCS = source.c

$(OUTFILE): $(OBJS)
        $(CC) $(CFLAGS) -o $(OUTFILE) $(OBJS)
$(OBJS): $(SRCS)
        $(CC) $(CFLAGS) -c $(SRCS)

Addendum (added late 2016): C99 is getting kind of old by now, people looking at this answer might want to explore C11instead.

附录(2016 年末添加):C99 现在有点老了,看到这个答案的人可能想要探索C11

回答by Rahul Tripathi

You may try to use the -std=c99flag.

您可以尝试使用该-std=c99标志。

Try to complile like this:

尝试像这样编译:

gcc -Wall -std=c99 -g myProgram.c

Also note that -gis for debugging option(Thanks Alter Mann for pointing that).

另请注意,这-g是用于调试选项(感谢 Alter Mann 指出)。

回答by hyde

Based on the comments under another answer, perhaps you are using the implicit make rules and don't have a Makefile. If this, then you are just runing make tstto generate tstbinary from tst.c. In that case you can specify the flags by setting the environment variable CFLAGS. You can set it for the current shell, or add it to your ~/.bashrcto have it always, with this:

根据另一个答案下的评论,也许您正在使用隐式 make 规则并且没有 Makefile。如果是这样,那么您只是在运行make tst以从tst.c生成tst二进制文件。在这种情况下,您可以通过设置环境变量来指定标志CFLAGS。您可以为当前 shell 设置它,或者将其添加到您的~/.bashrc始终使用它,如下所示:

export CFLAGS='-Wall -Wextra -std=c99'

Or specifying it just for the single command:

或者只为单个命令指定它:

CFLAGS='-Wall -Wextra -std=c99' make tst

(Note: I added warning flags too, you should really use them, they will detect a lot of potential bugs or just bad code you should write differently.)

(注意:我也添加了警告标志,您应该真正使用它们,它们会检测到许多潜在的错误或只是您应该以不同方式编写的错误代码。)