Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update include path in linux

Tags:

linux

I have few header files in /my/path/to/file folder. I know how to include these files in new C program but everytime I need to type full path to header file before including it. Can I set some path variable in linux such that it automatically looks for header files ?

like image 459
username_4567 Avatar asked Dec 10 '25 21:12

username_4567


2 Answers

You could create a makefile. A minimal example would be:

INC_PATH=/my/path/to/file
CFLAGS=-I$(INC_PATH)

all:
    gcc $(CFLAGS) -o prog src1.c src2.c

From here you could improve this makefile in many ways. The most important, probably, would be to state compilation dependencies (so only modified files are recompiled).

As a reference, here you have a link to the GNU make documentation.

If you do not want to use makefiles, you can always set an environment variable to make it easier to type the compilation command:

export MY_INC_PATH=/my/path/to/file

Then you could compile your program like:

gcc -I${MY_INC_PATH} -o prog src1.c src2.c ...

You may want to define MY_INC_PATH variable in the file .bashrc, or probably better, create a file in a handy place containing the variable definition. Then, you could use source to set that variable in the current shell:

source env.sh

I think, however, that using a makefile is a much preferable approach.

like image 73
betabandido Avatar answered Dec 13 '25 08:12

betabandido


there is a similar question and likely better solved (if you are interested in a permanent solution): https://stackoverflow.com/a/558819/1408096

Try setting C_INCLUDE_PATH (for C header files) or CPLUS_INCLUDE_PATH (for C++ header files).

Kudos:jcrossley3

like image 24
xhudik Avatar answered Dec 13 '25 08:12

xhudik