为 Makefile 中的标题添加目录 [英] Adding a directory for the headers in a Makefile

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

问题描述

你好我想问你,如果有人知道如何在Makefile中为头文件添加一个目录以避免错误*.h not found,我尝试过这个选项但不起作用:

Hello I would like to ask you, If someone knows how can I add a directory for the header files in the Makefile to avoid the error *.h not found, I have tried this option but does not work:

INC_PATH := -I /directory/to/add

推荐答案

虽然最终目标是影响 CFLAGS 的值(正如@unwind 所建议的那样),但简单地设置 CFLAGS 的值通常不是一个好主意因为它通常由许多部分组成.您必须了解 makefile 的结构,以及使用的宏集.

Although the goal is ultimately to affect the value of CFLAGS (as suggested by @unwind), it is often not a good idea to simply set the value of CFLAGS as it is often built out of many pieces. You have to understand the structure of the makefile, and the set of macros used.

[已添加:

Eduardo 问:你能发布宏来做同样的事情吗?

Eduardo asked: Can you post macros to do the same?

是的,但它们是否有用取决于您的 makefile 的结构.这是我的一个 makefile 中的一个中等复杂的示例.

Yes, but whether they are helpful depends on how your makefiles are structured. Here's a moderately complex example from one of my makefiles.

CC        = gcc -g
XFLAGS    = -Wall -Wshadow -Wstrict-prototypes -Wmissing-prototypes 
            -DDEBUG -Wredundant-decls
#CC        = cc -g
#XFLAGS    =
UFLAGS    = # Always overrideable on the command line

DEPEND.mk  = sqlcmd-depend.mk
INSTALL.mk = sqlcmd-install.mk

ESQLC_VERSION = `esqlcver`
OFLAGS    = # -DDEBUG_MALLOC -g
OFLAGS    = -g -DDEBUG -O4
PFLAGS    = -DHAVE_CONFIG_H
OFILES.o  = # rfnmanip.o # malloc.o # strdup.o # memmove.o
VERSION   = -DESQLC_VERSION=${ESQLC_VERSION}
#INC1     = <defined in sqlcmd-depend.mk>
#INC2     = <defined in sqlcmd-depend.mk>
INC3      = /usr/gnu/include
INC4      = ${INFORMIXDIR}/incl/esql
INC5      = . #${INFORMIXDIR}/incl
INCDIRS   = -I${INC3} -I${INC1} -I${INC2} -I${INC4} -I${INC5}
LIBSQLCMD = libsqlcmd.a
STRIP     = #-s
LIBC      = #-lc_s
LIBMALLOC = #-lefence
LIBRDLN   = -lreadline
LIBCURSES = -lcurses
LIBPOSIX4 = -lposix4
LIBG      = #-lg
LIBDIR1   = ${HOME}/lib
LIBDIR2   = /usr/gnu/lib
LIBJL1    = ${LIBDIR1}/libjl.a
LIBJL2    = ${LIBDIR1}/libjlss-${ESQLC_VERSION}.a
LIBTOOLS  = ${LIBJL2} ${LIBJL1}
LDFLAGS   = ${LIBSQLCMD} ${LIBTOOLS} -L${LIBDIR2} ${LIBG} ${LIBMALLOC} 
            ${LIBPOSIX4} ${LIBC} ${STRIP}
CFLAGS    = ${VERSION} ${INCDIRS} ${OFLAGS} ${XFLAGS} ${PFLAGS} ${UFLAGS}

这是我的一个名为 sqlcmd 的程序的生成文件(这个名称在微软创建同名命令之前的十年甚至更长的时间里就选择了).我假设 make 程序具有将 C 代码编译为对象的规则,例如:

This a makefile for a program of mine called sqlcmd (a name chosen a decade and more before Microsoft created a command of the same name). I assume that the make program has a rule for compiling C code to object like:

${CC} ${CFLAGS} -c $*.c

从宏 OBJECTS 中列出的一组目标文件链接程序的规则如下所示:

and that the rule for linking a program from a set of object files listed in the macro OBJECTS looks like:

${CC} ${CFLAGS} -o $@ ${OBJECTS} ${LDFLAGS}

如您所见,ESQLC_VERSION(使用中的 Informix ESQL/C 版本,默认通过运行脚本 esqlcver 派生)有单独可设置的宏,然后通过 INC1 包含目录到 INC5 和 INCFLAGS(可能有很多,具体取决于平台),以及优化器标志 (OFLAGS)、额外标志 (CFLAGS)、用户定义标志 (UFLAGS - 我在大多数 makefile 中使用的习语;它允许用户在 make 命令行上设置 UFLAGS 并在构建中添加一个额外的标志),以及一堆与库相关的宏.这就是让我的开发 makefile 可以在对我的开发平台(可以是 Linux、Solaris 或 MacOS X)进行调整的情况下进行调整所需要的.对于程序的使用者,有一个由生成的 configure 脚本autoconf,所以他们不必担心这些位是否正确.但是,这与该代码具有很强的遗传相似性,包括 UFLAGS 选项.

As you can see, there are separately settable macros for the ESQLC_VERSION (the version of Informix ESQL/C in use, derived by default by runing a script esqlcver), then the include directories via INC1 to INC5 and INCFLAGS (there can be quite a lot of these, depending on platform), and optimizer flags (OFLAGS), extra flags (CFLAGS), user-defined flags (UFLAGS - an idiom I use in most of my makefiles; it allows the user to set UFLAGS on the make command line and add an extra flag to the build), and a bunch of library-related macros. This is what it takes for my development makefile to be tunable with minimal fuss to my development platform, which can be Linux, Solaris or MacOS X. For consumers of the program, there is a configure script generated by autoconf, so they don't have to worry about getting those bits right. However, that has a strong genetic resemblance to this code, including the UFLAGS option.

请注意,许多用于构建 makefile 的系统都有一种设置 CFLAGS 的机制,与此类似 - 简单地分配给 CFLAGS 会撤消系统所做的良好工作.但是您必须了解您的 makefile 才能合理地修改它.

Note that many systems for makefile building have a mechanism for setting CFLAGS faintly similar to this - and simply assigning to CFLAGS undoes the good work done by the system. But you have to understand your makefile to be able to modify it sanely.

]

这篇关于为 Makefile 中的标题添加目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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