Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a directory for the headers in a 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
like image 915
Eduardo Avatar asked Dec 04 '08 17:12

Eduardo


People also ask

How do I add a header in makefile?

The only way to include the header file is to treat the filename in the same way you treat a string. Makefiles are a UNIX thing, not a programming language thing Makefiles contain UNIX commands and will run them in a specified sequence.

Can makefile target be a directory?

Yes, a Makefile can have a directory as target. Your problem could be that the cd doesn't do what you want: it does cd and the git clone is carried out in the original directory (the one you cd ed from, not the one you cd ed to). This is because for every command in the Makefile an extra shell is created.

How do I add .a files to makefile?

a , in which case you also need to add -lmine to the linker line (after the object files that reference the library). You have a file libmine that is a static archive, in which case you simply list it as a file ./libmine with no -L in front. You have a file libmine. a in the current directory that you want to pick up.

What does $< mean in makefile?

The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.


2 Answers

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

CFLAGS=-I/directory/to/add 
like image 53
unwind Avatar answered Sep 25 '22 13:09

unwind


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.

]

like image 44
Jonathan Leffler Avatar answered Sep 23 '22 13:09

Jonathan Leffler