CFLAGS,CCFLAGS,CXXFLAGS - 这些变量究竟控制着什么? [英] CFLAGS, CCFLAGS, CXXFLAGS - what exactly do these variables control?

查看:306
本文介绍了CFLAGS,CCFLAGS,CXXFLAGS - 这些变量究竟控制着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GNU make来编译我的C ++代码,并且我想知道如何使我的编译可定制。



我在不同的地方阅读 CFLAGS CCFLAGS CXXFLAGS 用于此目的。那么我应该如何使用它们?如果我有更多的命令行参数给编译器,我应该把它们追加到 CFLAGS 或者预先加入它们吗?有没有普遍的做法?



为什么三个不同的变量?我想C编译器应该得到 CFLAGS CCFLAGS ,而C ++编译器应该得到 CFLAGS 和 CXXFLAGS - 我是否正确?

设置这些变量呢?做任何自动工具( automake autoconf 等)设置它们?我应该使用的linux系统没有定义任何这些变量 - 这是典型的吗?



目前我的Makefile看起来像这样,我觉得它有点儿dirty:

  ifdef code_coverage 
GCOV_FLAG:= -fprofile-arcs -ftest-coverage
else
GCOV_FLAG:=
endif

WFLAGS:= -Wall

INC_FLAGS:= -Istuff -Imore_stuff -Ietc

CCFLAGSINT: = -O3 $(WFLAGS)$(INC_FLAGS)$(CCFLAGS)

...(makefile中的某处,编译命令行如下所示)
$(CC) $(CCFLAGSINT)-c $ < -o $ @

...(makefile中的某处,链接的命令行如下所示)
$(CC)$(GCOV_FLAG)$(CCFLAGSINT)$(OBJLIST )$(LDFLAGS)-o $ @

我很确定这里没有错误; Makefile工作得很好。但是有没有违背约定的东西(比如 CCFLAGSINT - 我应该重写 CCFLAGS ),还是 CXXFLAGS ?FUD!)



对不起,有这么多问题;你显然不会回答他们,但我希望这些答案能帮助我理解这些设置背后的总体思路。

注意到,这些是Makefile {宏或变量},而不是编译器选项。他们实施了一套公约。 (宏是它们的一个旧名称,仍然被某些人使用,GNU make doc调用它们的变量)。

名字的唯一理由是默认的make规则,通过 make -p 可以看到,它们使用了其中的一些。



如果你编写所有你自己的规则,选择所有你自己的宏名称。



在vanilla gnu make中,没有CCFLAGS这样的东西。有 CFLAGS CPPFLAGS CXXFLAGS 。 C编译器的 CFLAGS ,C ++的 CXXFLAGS ,以及 CPPFLAGS for each。



为什么 CPPFLAGS 都在?通常,它是预处理器标志( -D -U )的起源,并且c和c ++都使用它们。现在,假设每个人都希望为c和c ++定义相同的环境,这可能是有问题的,但却是传统的。


I am using GNU make to compile my C++ code, and i would like to understand how to make my compilations customizable.

I read in different places that CFLAGS, CCFLAGS and CXXFLAGS are used for this purpose. So how should i use them? If i have additional command-line arguments to the compiler, should i append them to CFLAGS or prepend them? Is there a common practice?

Why the three different variables? I suppose the C compiler should get CFLAGS and CCFLAGS, while the C++ compiler should get CFLAGS and CXXFLAGS - did i get it right?

Is the human user supposed to set these variables at all? Do any automatic tools (automake, autoconf, etc) set them? The linux system that i am supposed to use doesn't define any of these variables - is this typical?

Currently my Makefile looks like this, and i feel it's a bit dirty:

ifdef code_coverage
    GCOV_FLAG := -fprofile-arcs -ftest-coverage
else
    GCOV_FLAG :=
endif

WFLAGS := -Wall

INC_FLAGS := -Istuff -Imore_stuff -Ietc

CCFLAGSINT := -O3 $(WFLAGS) $(INC_FLAGS) $(CCFLAGS)

... (somewhere in the makefile, the command-line for compilation looks like this)
    $(CC) $(CCFLAGSINT) -c $< -o $@

... (somewhere in the makefile, the command-line for linking looks like this)
    $(CC) $(GCOV_FLAG) $(CCFLAGSINT) $(OBJLIST) $(LDFLAGS) -o $@

I am pretty sure there are no bugs here; the Makefile works very well. But is there anything that goes against conventions (like CCFLAGSINT - should i just overwrite CCFLAGS instead? Or CXXFLAGS? FUD!)

Sorry for so many questions; you will obviously not answer them all but i hope the answers will help me understand the general idea behind these settings.

解决方案

As you noticed, these are Makefile {macros or variables}, not compiler options. They implement a set of conventions. (Macros is an old name for them, still used by some. GNU make doc calls them variables.)

The only reason that the names matter is the default make rules, visible via make -p, which use some of them.

If you write all your own rules, you get to pick all your own macro names.

In a vanilla gnu make, there's no such thing as CCFLAGS. There are CFLAGS, CPPFLAGS, and CXXFLAGS. CFLAGS for the C compiler, CXXFLAGS for C++, and CPPFLAGS for both.

Why is CPPFLAGS in both? Conventionally, it's the home of preprocessor flags (-D, -U) and both c and c++ use them. Now, the assumption that everyone wants the same define environment for c and c++ is perhaps questionable, but traditional.

这篇关于CFLAGS,CCFLAGS,CXXFLAGS - 这些变量究竟控制着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆