用-ansi -pedantic -Wall自动编译与gcc。

时间:2023-01-28 02:11:47

We are required to compile C source codes using gcc in this manner:

我们需要使用gcc以这种方式编译C源代码:

gcc -ansi -pedantic -Wall program.c

I'm wondering how can I 'automate' this so when I enter:

我在想,当我进入的时候,我怎么能“自动化”这个呢?

gcc program.c

It will automatically compile with the 3 switches. Is this possible?

它将自动编译与三个开关。这是可能的吗?

5 个解决方案

#1


9  

You can also use the implicit rules of make, so that you don't have to write a makefile for every program. Make will automatically call the compiler if you say make foo and there exists a foo.c file in the current directory. To add flags to this define the variable CFLAGS in your environment, e.g. in bash add export CFLAGS="-Wall -pedantic -ansi" to .bashrc.

您还可以使用make的隐式规则,以便不必为每个程序编写makefile。如果你说Make foo并且存在一个foo, Make会自动调用编译器。当前目录中的c文件。要向其添加标志,请在您的环境中定义变量CFLAGS,例如,在bash中向.bashrc添加CFLAGS=“-Wall -pedantic -ansi”。

If your program depends on multiple files however you'll have to create a makefile, but for C compilation you can get away with just listing dependancies so long as one of them has the same base name as a target.

如果您的程序依赖于多个文件,那么您必须创建一个makefile,但是对于C编译,您可以只列出依赖项,只要其中一个与目标具有相同的基名。

For example for this makefile:

例如这个makefile:

# Makefile
foo:foo.o bar.o

running make will execute the commands

运行make将执行命令

gcc $CFLAGS -c -o foo.o foo.c
gcc $CFLAGS -c -o bar.o bar.c
gcc -o foo foo.o bar.o

without you having to add any rules.

不需要添加任何规则。

#2


8  

To automate the build of any number of build steps / complex parameters, you should use a makefile.

要自动化任何数量的构建步骤/复杂参数的构建,您应该使用makefile。

Once you have a makefile you simply need to type: make

有了makefile之后,只需输入:make

#3


3  

alias gcc="gcc -ansi -pedantic -Wall"

But as @Brian said, you really should use a makefile, or better, a build system like CMake or SCons.

但是正如@Brian所说,您确实应该使用makefile,或者更好,使用CMake或SCons这样的构建系统。

#4


2  

A makefile would be the traditional way, especially as part of a larger build process.

makefile将是传统的方式,特别是作为更大的构建过程的一部分。

If you frequently want to build without a makefile, you could define an alias in your .bashrc or equivalent: alias gcc=gcc -ansi -pedantic -Wall.

如果您经常想在没有makefile的情况下构建,那么可以在.bashrc中定义一个别名,或者等效的:alias gcc=gcc -ansi -pedantic -Wall。

#5


2  

You can use a shell script that takes some cues by how its called and invokes make after setting CFLAGS appropriately for the occasional one-off build.

您可以使用一个shell脚本,该脚本在为偶尔的一次性构建适当地设置CFLAGS之后,通过它的调用和调用来获取一些提示。

Lets say you have /usr/bin/compile , which is a shell script that looks at $0 to see what name actually invoked it. You then make symbolic links to it named pedantic, fullwarn, etc.

假设您有/usr/bin/compile,它是一个shell脚本,它查看$0,以查看实际调用它的名称。然后,您将它的符号链接命名为pedantic、fullwarn等。

In the shell script itself, something like:

在shell脚本中,如下内容:

OLDCFLAGS=$CFLAGS
WHATAMI=$(basename $0)

case "$WHATAMI" in
    pedantic)
        export CFLAGS="-Wall -pedantic -ansi"
        make $@
        exit $?
    ;;
    c99)
        export CFLAGS="-std=c99 ... ... ..."
        ....

Then, to compile foo.c with the extra naggy flags:

然后,编译foo。c带有额外的naggy标志:

pedantic foo

迂腐的foo

This is handy, as I said for one-off builds, e.g trying to compile code that someone posted in a question, or working out how to use a new library, etc.

这很方便,正如我所说的一次性构建,e。尝试编译某人在问题中发布的代码,或者研究如何使用一个新的库,等等。

For anything else, just use a makefile, as others have said.

对于其他任何东西,只需使用makefile,就像其他人所说的那样。

#1


9  

You can also use the implicit rules of make, so that you don't have to write a makefile for every program. Make will automatically call the compiler if you say make foo and there exists a foo.c file in the current directory. To add flags to this define the variable CFLAGS in your environment, e.g. in bash add export CFLAGS="-Wall -pedantic -ansi" to .bashrc.

您还可以使用make的隐式规则,以便不必为每个程序编写makefile。如果你说Make foo并且存在一个foo, Make会自动调用编译器。当前目录中的c文件。要向其添加标志,请在您的环境中定义变量CFLAGS,例如,在bash中向.bashrc添加CFLAGS=“-Wall -pedantic -ansi”。

If your program depends on multiple files however you'll have to create a makefile, but for C compilation you can get away with just listing dependancies so long as one of them has the same base name as a target.

如果您的程序依赖于多个文件,那么您必须创建一个makefile,但是对于C编译,您可以只列出依赖项,只要其中一个与目标具有相同的基名。

For example for this makefile:

例如这个makefile:

# Makefile
foo:foo.o bar.o

running make will execute the commands

运行make将执行命令

gcc $CFLAGS -c -o foo.o foo.c
gcc $CFLAGS -c -o bar.o bar.c
gcc -o foo foo.o bar.o

without you having to add any rules.

不需要添加任何规则。

#2


8  

To automate the build of any number of build steps / complex parameters, you should use a makefile.

要自动化任何数量的构建步骤/复杂参数的构建,您应该使用makefile。

Once you have a makefile you simply need to type: make

有了makefile之后,只需输入:make

#3


3  

alias gcc="gcc -ansi -pedantic -Wall"

But as @Brian said, you really should use a makefile, or better, a build system like CMake or SCons.

但是正如@Brian所说,您确实应该使用makefile,或者更好,使用CMake或SCons这样的构建系统。

#4


2  

A makefile would be the traditional way, especially as part of a larger build process.

makefile将是传统的方式,特别是作为更大的构建过程的一部分。

If you frequently want to build without a makefile, you could define an alias in your .bashrc or equivalent: alias gcc=gcc -ansi -pedantic -Wall.

如果您经常想在没有makefile的情况下构建,那么可以在.bashrc中定义一个别名,或者等效的:alias gcc=gcc -ansi -pedantic -Wall。

#5


2  

You can use a shell script that takes some cues by how its called and invokes make after setting CFLAGS appropriately for the occasional one-off build.

您可以使用一个shell脚本,该脚本在为偶尔的一次性构建适当地设置CFLAGS之后,通过它的调用和调用来获取一些提示。

Lets say you have /usr/bin/compile , which is a shell script that looks at $0 to see what name actually invoked it. You then make symbolic links to it named pedantic, fullwarn, etc.

假设您有/usr/bin/compile,它是一个shell脚本,它查看$0,以查看实际调用它的名称。然后,您将它的符号链接命名为pedantic、fullwarn等。

In the shell script itself, something like:

在shell脚本中,如下内容:

OLDCFLAGS=$CFLAGS
WHATAMI=$(basename $0)

case "$WHATAMI" in
    pedantic)
        export CFLAGS="-Wall -pedantic -ansi"
        make $@
        exit $?
    ;;
    c99)
        export CFLAGS="-std=c99 ... ... ..."
        ....

Then, to compile foo.c with the extra naggy flags:

然后,编译foo。c带有额外的naggy标志:

pedantic foo

迂腐的foo

This is handy, as I said for one-off builds, e.g trying to compile code that someone posted in a question, or working out how to use a new library, etc.

这很方便,正如我所说的一次性构建,e。尝试编译某人在问题中发布的代码,或者研究如何使用一个新的库,等等。

For anything else, just use a makefile, as others have said.

对于其他任何东西,只需使用makefile,就像其他人所说的那样。