3

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 ?

3
  • you can add the path to your makefile ? if you use one that is Commented Jul 17, 2012 at 9:57
  • Do you use gcc? Do you use it directly or do you use a makefile? Commented Jul 17, 2012 at 9:57
  • 1
    If using gcc directly (which usually is a bad idea; it is preferable to use some builder like make or omake or some script), just pass it the appropriate -I your/include/dir flags. Don't forget to pass -Wall to gcc. Commented Jul 17, 2012 at 10:06

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but is there any way without writing makefile?
@username_4567 I was just updating my question to include an alternative solution. But writing a 5 lines makefile seems to me like an easy and better solution.
Thanks actually I thought there is be default path where all header files are looked for..
@username_4567 There are some default paths (such as /usr/include). Options -I and -L let you use custom include and library paths. Of course, you do not want to type the long path all the time, so you need some kind of solution like the ones I mentioned :)
3

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

Comments

0

I'm not in Linux right now and I can't be bothered to reboot to check if everything's right, but have you tried making symbolic links? For example, if you are on Ubuntu:

$ cd /usr/include
$ sudo ln -s /my/path/to/file mystuff

So then when you want to include stuf, you can use:

#include <mystuff/SpamFlavours.h>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.