更改默认 make 命令

更改默认 make 命令

我正在学习 HarvardX:CS50x 计算机科学入门课程。通常,要完成这门课程,您必须使用虚拟机来运行 Fedora 系统,即 cs50 设备,所有操作都很简单,但我已经使用 Kubuntu,所以我下载了 clang 和 cs50 库,我想更改默认选项以make包含库并在我尝试编译的 c 程序中出现错误时停止。在设备中,make 命令执行:clang -ggdb3 -00 -std=c99 -Wall -Werror -argv-2.c -lcs50 -lm -o argv-2。如果您能解释应该包含什么以及为什么,那就太好了。当我使用 make 编译 .c 文件时,它不会加载 cs50 库,我得到:未定义的函数...

我尝试写入$ nano ~/.bashrc。然后转到文件末尾并输入

 #CS50 alias for C compiling:
 alias makec='gcc -ggdb3 -std=c99 -Wall -Werror -argv-2.c -lcs50 -lm -o -argv-2'

然后$ source .bashrc

当我$ makec hello现在这样做时它说:

gcc: error: hello: No such file or directory
gcc: error: unrecognized command line option ‘-argv-2.c’

答案1

您需要执行的操作与程序 gcc 和 ld 有关。环境变量 C_INCLUDE_PATH 和 LD_LIBRARY_PATH 分别是搜索头文件和库的位置。gcc 选项 -l 和 -L 很有用,通常声明如下内容

库=-lcs

LIBDIR=-L /usr/local/lib

然后在每个规则运行的命令中使用这些 make-variables。如果您使用 apt-get 安装了这些库,则可能有可用的包配置数据,请参阅命令 pkg-config

答案2

我认为这并不能解决您的问题,但可以回答如下问题:

有一些方法可以设置默认选项make

  • 使用 shell 别名,例如

    alias make='make --foo=bar'
    

    或类似~/.bashrc

  • 设置环境变量MAKEFLAGS以包含默认选项。

  • 使用环境变量MAKEFLAGS添加默认选项-e--environment-overrides),并定义与中的 makefile 相关的其他变量~/.bashrc,以供 拾取make,优先于 中的定义Makefile

使用每一种方法时,所有的用途make都会受到影响,而这并不是您真正想要的。

一个更好的解决方案似乎是不改变 的默认选项make
而是使用单独的别名以交互方式运行 make。该别名可以设置 的递归调用选项make

alias cs50make='make --foo=bar'

或者

alias cs50make='MAKEFLAGS="..." make'

甚至

alias cs50make='MAKEFLAGS="-e ..." VAR1="..." VAR2="..." make'


关于在出现make错误时停止:它通常应该默认这样做,但如果它在某处设置为不停止,则可以使用选项-S--no-keep-going--stop)来覆盖。

答案3

你制定的规则应该是这样的,

你好:你好.c

gcc -ohello hello.c -g -lcs50 -lm

现在只需输入命令

打招呼

答案4

export CC=gcc
export CFLAGS="-ggdb3 -O0 -std=c99 -Wall -Werror"
export LDLIBS="-lcs50 -lm"

-ggdb

       Produce debugging information for use by GDB.  This means
       to use the most expressive format available (DWARF 2,
       stabs, or the native format if neither of those are
       supported), including GDB extensions if at all possible.

-ggdb级别

       Request debugging information and also use level to specify
       how much information.  The default level is 2.

       Level 3 includes extra information, such as all the macro
       definitions present in the program.  Some debuggers support
       macro expansion when you use -g3.

-O0

       Reduce compilation time and make debugging produce the
       expected results.  This is the default. (Is a optimization option)

-墙

       Turns on all optional warnings which are desirable for
       normal code.  At present this is -Wcomment, -Wtrigraphs,
       -Wmultichar and a warning about integer promotion causing a
       change of sign in "#if" expressions.  Note that many of the
       preprocessor's warnings are on by default and have no
       options to control them.

-W错误

       Make all warnings into hard errors.  Source code which
       triggers warnings will be rejected.

-lm

       possibly loads a math library.

相关内容