Linux makefile include directories

How to add gcc include directory to existing Makefile

I have a Makefile that I have copied and use in many small programs that I write in C for Linux. Sadly, I don’t understand every detail of how it works and I typically just comment out the name of the output files and insert the name I want and it compiles my programs successfully. I would like to use these instructions: Those with a command-line compiler will typically use options such as ‘/I%SQLAPIDIR%\include’ or ‘-I$/include’. The header files are in the include subdirectory of SQLAPI++ distributions so that my Makefile will add the libraries when it compiles. I checked this site and found the following links but they didn’t help: What are the GCC default include directories? how to add a new files to existing makefile project Adding an include directory to gcc *before* -I My attempt at including the directory.

OBJS = testql.o CC = g++ DEBUG = -g SQLAPI=/home/developer/Desktop/ARC_DEVELOPER/user123/testsql/SQLAPI CFLAGS = -I$/include -Wall -c $(DEBUG) LFLAGS = -Wall $(DEBUG) testql: $(OBJS) $(CC) $(LFLAGS) $(OBJS) -o testql clean: rm -f testql *.o *~ core 
[developer@localhost testql]$ make g++ -c -o testql.o testql.cpp testql.cpp:2:44: fatal error: SQLAPI.h: No such file or directory #include // main SQLAPI++ header 
[developer@localhost testql]$ ls -l total 12 -rw-rw-r--. 1 developer developer 286 Mar 3 12:47 Makefile drwxr-xr-x. 7 developer developer 4096 Oct 16 02:08 SQLAPI -rw-rw-r--. 1 developer developer 1169 Mar 3 11:43 testql.cpp 
[developer@localhost testql]$ ls SQLAPI/include/SQLAPI.h SQLAPI/include/SQLAPI.h 
 #include // for printf #include // main SQLAPI++ header int main(int argc, char* argv[]) < SAConnection con; // create connection object try < // connect to database // in this example it is Oracle, // but can also be Sybase, Informix, DB2 // SQLServer, InterBase, SQLBase and ODBC con.Connect( "test", // database name "tester", // user name "tester", // password SA_Oracle_Client); printf("We are connected!\n"); // Disconnect is optional // autodisconnect will ocur in destructor if needed con.Disconnect(); printf("We are disconnected!\n"); >catch(SAException &x) < // SAConnection::Rollback() // can also throw an exception // (if a network error for example), // we will be ready try < // on error rollback changes con.Rollback(); >catch(SAException &) < >// print error message printf("%s\n", (const char*)x.ErrText()); > return 0; > 

Источник

Linux makefile include directories

The include directive tells make to suspend reading the current makefile and read one or more other makefiles before continuing. The directive is a line in the makefile that looks like this:

filenames can contain shell file name patterns. If filenames is empty, nothing is included and no error is printed.

Extra spaces are allowed and ignored at the beginning of the line, but the first character must not be a tab (or the value of .RECIPEPREFIX )—if the line begins with a tab, it will be considered a recipe line. Whitespace is required between include and the file names, and between file names; extra whitespace is ignored there and at the end of the directive. A comment starting with ‘ # ’ is allowed at the end of the line. If the file names contain any variable or function references, they are expanded. See How to Use Variables.

Читайте также:  Полная русификация kali linux

For example, if you have three .mk files, a.mk , b.mk , and c.mk , and $(bar) expands to bish bash , then the following expression

include foo a.mk b.mk c.mk bish bash

When make processes an include directive, it suspends reading of the containing makefile and reads from each listed file in turn. When that is finished, make resumes reading the makefile in which the directive appears.

One occasion for using include directives is when several programs, handled by individual makefiles in various directories, need to use a common set of variable definitions (see Setting Variables) or pattern rules (see Defining and Redefining Pattern Rules).

Another such occasion is when you want to generate prerequisites from source files automatically; the prerequisites can be put in a file that is included by the main makefile. This practice is generally cleaner than that of somehow appending the prerequisites to the end of the main makefile as has been traditionally done with other versions of make . See Generating Prerequisites Automatically.

If the specified name does not start with a slash (or a drive letter and colon when GNU Make is compiled with MS-DOS / MS-Windows path support), and the file is not found in the current directory, several other directories are searched. First, any directories you have specified with the ‘ -I ’ or ‘ —include-dir ’ options are searched (see Summary of Options). Then the following directories (if they exist) are searched, in this order: prefix /include (normally /usr/local/include 1 ) /usr/gnu/include , /usr/local/include , /usr/include .

The .INCLUDE_DIRS variable will contain the current list of directories that make will search for included files. See Other Special Variables.

You can avoid searching in these default directories by adding the command line option -I with the special value — (e.g., -I- ) to the command line. This will cause make to forget any already-set include directories, including the default directories.

If an included makefile cannot be found in any of these directories it is not an immediately fatal error; processing of the makefile containing the include continues. Once it has finished reading makefiles, make will try to remake any that are out of date or don’t exist. See How Makefiles Are Remade. Only after it has failed to find a rule to remake the makefile, or it found a rule but the recipe failed, will make diagnose the missing makefile as a fatal error.

If you want make to simply ignore a makefile which does not exist or cannot be remade, with no error message, use the -include directive instead of include , like this:

Читайте также:  Просмотр системных логов linux

This acts like include in every way except that there is no error (not even a warning) if any of the filenames (or any prerequisites of any of the filenames ) do not exist or cannot be remade.

For compatibility with some other make implementations, sinclude is another name for -include .

Footnotes

(1)

GNU Make compiled for MS-DOS and MS-Windows behaves as if prefix has been defined to be the root of the DJGPP tree hierarchy.

Источник

How To Include Files From Multiple Directories In C on Linux?

Now I am trying to run main.c which contains #include directive to include header files from include directory and function calls to .c files in both common and src directories. I am using -I option but it is useful only for one directory path indication. How does the compiler will look in all src, common and include directories to resolve the calls. Kindly suggest me a command or make file to provide path of multiple directories while compiling with gcc.

1 Answer 1

Multiple -I options are permitted. The description of the -I option from Options for Directory Search states:

Add the directory dir to the head of the list of directories to be searched for header files. This can be used to override a system header file, substituting your own version, since these directories are searched before the system header file directories. However, you should not use this option to add directories that contain vendor-supplied system header files (use -isystem for that). If you use more than one -I option, the directories are scanned in left-to-right order; the standard system directories come after.

gcc main.c -o main -Iinclude -Isrc/include -Icommon/include

Note that if main.c is using functions implemented in another .c file(s) then the other .c files will also need compiled and linked into the final program binary. For example:

gcc main.c src/another.c -o main -Iinclude -Isrc/include -Icommon/include

Источник

How to add include and lib paths to configure/make cycle?

I need a place to install libraries in a linux box I have no su access to. I’m using ~/local[/bin,/lib,/include], but I don’t know how can I tell ./configure to look for libraries there (particularly, I’m trying to compile emacs, which needs libgif, which doesn’t come in my distro). I tried adding

export PATH=$PATH:~/local/bin export LD_LIBRARY_PATH=~/local/lib export C_INCLUDE_PATH=~/local/include export CPLUS_INCLUDE_PATH=~/local/include 

Note that excepted for the PATH, you overwrite your system default ones. If you make something like export C_INCLUDE_PATH=~/local/include:$C_INCLUDE_PATH , your compiler will search firstly in ~/local/include , and in $C_INCLUDE_PATH only if it didn’t found the include in the first directory.

6 Answers 6

You want a config.site file. Try:

$ mkdir -p ~/local/share $ cat ~/local/share/config.site CPPFLAGS=-I$HOME/local/include LDFLAGS=-L$HOME/local/lib . EOF

Whenever you invoke an autoconf generated configure script with —prefix=$HOME/local, the config.site will be read and all the assignments will be made for you. CPPFLAGS and LDFLAGS should be all you need, but you can make any other desired assignments as well (hence the . in the sample above). Note that -I flags belong in CPPFLAGS and not in CFLAGS, as -I is intended for the pre-processor and not the compiler.

Читайте также:  Linux host allow all

William; A thanks, that was bang on advice about CPPFLAGS for building on a shared host. Saved much thrashing about with failed ./configure runs today, because of your note.

Set LDFLAGS and CFLAGS when you run make:

$ LDFLAGS="-L/home/me/local/lib" CFLAGS="-I/home/me/local/include" make 

If you don’t want to do that a gazillion times, export these in your .bashrc (or your shell equivalent). Also set LD_LIBRARY_PATH to include /home/me/local/lib:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/me/local/lib 

when I add LDFLAGS and CFLAGS to .bashrc ./configure fails with a configure: error: C compiler cannot create executables.

They are probably overriding the configure test code compiler parameters. Just use the make env prefetching. Also try and see if configure doesn’t already have an option for overriding libgif. If the autotools scripts are properly made it will alow you to spec a path where libgif is installed (e.g. override the default library location). That would be the cleaner solution.

same difference.. tneme@ws16lab07:~/Descargas/emacs-23.3$ LDFLAGS=»-L/home/tneme/local/lib» CFLAGS=»-l/home/tneme/local/include» ./configure —prefix=»/home/tneme/local» checking build system type. i686-pc-linux-gnu checking host system type. i686-pc-linux-gnu checking for gcc. gcc checking whether the C compiler works. no configure: error: in /home/tneme/Descargas/emacs-23.3′: configure: error: C compiler cannot create executables See config.log’ for more details t

just tried configuring emacs 23 with these and I didn’t get that problem: LDFLAGS=»-L/home/me/local/lib» CFLAGS=»-I/home/me/local/include» ./configure checking build system type. x86_64-unknown-linux-gnu checking host system type. x86_64-unknown-linux-gnu checking for gcc. gcc checking for C compiler default output file name. a.out checking whether the C compiler works. yes checking whether we are cross compiling. no checking for suffix of executables. checking for suffix of object files. o checking whether we are using the GNU C compiler. yes

This took a while to get right. I had this issue when cross-compiling in Ubuntu for an ARM target. I solved it with:

PATH=$PATH:/ccpath/bin CC=ccname-gcc AR=ccname-ar LD=ccname-ld CPPFLAGS="-nostdinc -I/ccrootfs/usr/include . " LDFLAGS=-L/ccrootfs/usr/lib ./autogen.sh --build=`config.guess` --host=armv5tejl-unknown-linux-gnueabihf 

Notice CFLAGS is not used with autogen.sh/configure, using it gave me the error: «configure: error: C compiler cannot create executables». In the build environment I was using an autogen.sh script was provided, if you don’t have an autogen.sh script substitute ./autogen.sh with ./configure in the command above. I ran config.guess on the target system to get the —host parameter.

After successfully running autogen.sh/configure, compile with:

PATH=$PATH:/ccpath/bin CC=ccname-gcc AR=ccname-ar LD=ccname-ld CPPFLAGS="-nostdinc -I/ccrootfs/usr/include . " LDFLAGS=-L/ccrootfs/usr/lib CFLAGS="-march=. -mcpu=. etc." make 

The CFLAGS I chose to use were: «-march=armv5te -fno-tree-vectorize -mthumb-interwork -mcpu=arm926ej-s». It will take a while to get all of the include directories set up correctly: you might want some includes pointing to your cross-compiler and some pointing to your root file system includes, and there will likely be some conflicts.

I’m sure this is not the perfect answer. And I am still seeing some include directories pointing to / and not /ccrootfs in the Makefiles. Would love to know how to correct this. Hope this helps someone.

Источник

Оцените статью
Adblock
detector