在Makefile中为头文件添加一个目录 [英] Adding a directory for the headers in a Makefile

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

问题描述

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

  INC_PATH:= -I / directory / to / add 


解决方案

虽然目标最终会影响CFLAGS的价值(正如@unwind所建议的),但通常不是一个好主意CFLAGS的价值通常是由许多部分构成的。您必须了解makefile的结构和使用的宏集合。



[新增



Eduardo问道:你可以发布宏来做同样的事吗?

是的,但它们是否有用取决于你的makefile的结构。

  CC = gcc -g 
XFLAGS = -Wall -Wshadow这是一个来自我的makefiles的适度复杂的例子。 -Wstrict-prototypes -Wmissing-prototypes \
-DDEBUG -Wredundant-decls
#CC = cc -g
#XFLAGS =
UFLAGS =#在命令行上始终可覆盖

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 =<在sqlcmd-depend.mk中定义>
#INC2 =<在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 (在Microsoft创建一个同名命令之前,这个名称选择十年或更长时间)。我假设 make 程序有一个编译C代码的规则,例如:

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

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

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

正如您所见,通过运行脚本 esqlcver )默认派生的ESQLC_VERSION(使用的Informix ESQL / C的版本)的可设置宏,然后通过INC1将INCLUDE目录包含到INC5和INCFLAGS(可以有很多这些,取决于平台),优化器标志(OFLAGS),额外标志(CFLAGS),用户定义的标志(UFLAGS - 我用于大多数生成文件的习惯用法;它允许用户设置UFLAGS放在 make 命令行中,并为构建添加一个额外的标志)以及一堆与库相关的宏。这就是我的开发Makefile所需要的最小的麻烦,可以是我的开发平台,可以是Linux,Solaris或MacOS X.对于程序的用户,有一个 configure autoconf 生成的$ c>脚本,所以他们不必担心这些位正确。然而,这与代码具有很强的遗传相似性,包括UFLAGS选项。注意许多用于makefile构建的系统都有一个设置CFLAGS的机制,与此类似 - 只需分配给CFLAGS即可解除系统完成的良好工作。但是你必须理解你的makefile才能够理解它。

]


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

解决方案

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.

[Added:

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

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}

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

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}

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.

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天全站免登陆