Makefile中的ccflag选项 [英] ccflag option in Makefile

查看:1950
本文介绍了Makefile中的ccflag选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编译我的C代码(在内核中),该代码需要包含另一个目录中的一些头文件.

I want to compile my c code (in kernel) which needs to include some header files from another directory.

我不想在c文件中指定头文件的完整路径,而是在Makefile中指定包含路径.

Instead of specifying the complete path to header files in c file, I would like to specify the include path in Makefile.

启用配置选项CONFIG_FEATURE_X后,将编译我的c文件. 我已经在Makefile中写了以下内容:

My c file gets complied when the config option CONFIG_FEATURE_X is enabled. I have written the following in Makefile:

obj-$(CONFIG_FEATURE_X) += my_file.o

ccflags-$(CONFIG_FEATURE_X) += -I$(obj)/../../path

  1. 使用make menuconfig在.config中启用(c)CONFIG_FEATURE_X时,它可以正常工作.

  1. When the CONFIG_FEATURE_X is enabled (Y) in .config using make menuconfig, it works fine.

但是当在.config中将CONFIG_FEATURE_X用作模块(m)时,它不包括来自指定路径的头文件,并给出文件未找到错误.

But when the CONFIG_FEATURE_X is enabled as module (m) in .config, this does not include the header files from the path specified and gives the file not found error.

我该怎么做?

推荐答案

使用make menuconfig在.config中启用(Y)CONFIG_FEATURE_X时,它工作正常.

When the CONFIG_FEATURE_X is enabled (Y) in .config using make menuconfig, it works fine.

那是因为

ccflags-$(CONFIG_FEATURE_X) += -I$(obj)/../../path  

将评估为

ccflags-y += -I$(obj)/../../path

根据 Documentation/kbuild/makefiles.txt :

--- 3.7 Compilation flags

    ccflags-y, asflags-y and ldflags-y
        These three flags apply only to the kbuild makefile in which they
        are assigned. They are used for all the normal cc, as and ld
        invocations happening during a recursive build.
        Note: Flags with the same behaviour were previously named:
        EXTRA_CFLAGS, EXTRA_AFLAGS and EXTRA_LDFLAGS.
        They are still supported but their usage is deprecated.

        ccflags-y specifies options for compiling with $(CC).

因此,您已经为内置案例定义了一个有效的编译标志.

So you have defined a valid compilation flag for the built-in case.

但是,在.config中将CONFIG_FEATURE_X启用为模块(m)时,它不包括来自指定路径的头文件,并导致找不到文件错误.

But when the CONFIG_FEATURE_X is enabled as module (m) in .config, this does not include the header files from the path specified and gives the file not found error.

那是因为

ccflags-$(CONFIG_FEATURE_X) += -I$(obj)/../../path  

将评估为

ccflags-m += -I$(obj)/../../path

根据当前版本的 Documentation/kbuild/makefiles.txt ,没有这样的编译标志,例如"ccflags-m".
因此,路径规范永远不会用于可加载模块.

According to the current version of Documentation/kbuild/makefiles.txt, there is no such compilation flag as "ccflags-m".
So the path specification is never used for the loadable module.

我该怎么做?

How can I do this?

您可以尝试使用CFLAGS _ $ @,而不是ccflags-$()标志,它是$(CC)的每个文件选项.

Instead of the ccflags-$() flag, you could try to use CFLAGS_$@, a per-file options for $(CC).

CFLAGS_$@, AFLAGS_$@

    CFLAGS_$@ and AFLAGS_$@ only apply to commands in current
    kbuild makefile.

    $(CFLAGS_$@) specifies per-file options for $(CC).  The $@
    part has a literal value which specifies the file that it is for.

    Example:
        # drivers/scsi/Makefile
        CFLAGS_aha152x.o =   -DAHA152X_STAT -DAUTOCONF
        CFLAGS_gdth.o    = # -DDEBUG_GDTH=2 -D__SERIAL__ -D__COM2__ \
                 -DGDTH_STATISTICS

    These two lines specify compilation flags for aha152x.o and gdth.o.

    $(AFLAGS_$@) is a similar feature for source files in assembly
    languages.

    Example:
        # arch/arm/kernel/Makefile
        AFLAGS_head.o        := -DTEXT_OFFSET=$(TEXT_OFFSET)
        AFLAGS_crunch-bits.o := -Wa,-mcpu=ep9312
        AFLAGS_iwmmxt.o      := -Wa,-mcpu=iwmmxt

因此在您的 Makefile 中:

CFLAGS_my_file.o =   -I$(obj)/../../path

这篇关于Makefile中的ccflag选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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