Adding a directory for the headers in a Makefile

2019-01-17 07:27发布

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

2条回答
▲ chillily
2楼-- · 2019-01-17 07:38

At least for GNU make, try the implicit variable CFLAGS, as in:

CFLAGS=-I/directory/to/add
查看更多
聊天终结者
3楼-- · 2019-01-17 07:47

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.

]

查看更多
登录 后发表回答