How to Add a Default Include Path For Gcc in Linux

How to add a default include path for GCC in Linux?

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

As Ciro mentioned, CPATH will set the path for both C and C++ (and any other language).

More details in GCC's documentation.

How to add default Include and Library path for gcc in Macos for bash?

Following the suggestions of Jonathan Leffler, I got a way around.

I created soft links and it worked.

sudo ln -s ~/Applications/cfitsio/lib/libcfitsio.a /usr/local/lib/libcfitsio.a

sudo ln -s ~/Applications/cfitsio/include/*.h /usr/local/include/

In fact, I copies all the header files from

~/Applications/cfitsio/include   

to

/usr/local/include/  

and it worked.

I assume soft links also should work.

This might look a very simple solution, but it took hours me to figure out.

How Can I Add An Include Path in GCC C++

Use the -I command-line argument:

gcc -Ipath

What are the GCC default include directories?

In order to figure out the default paths used by gcc/g++, as well as their priorities, you need to examine the output of the following commands:

  1. For C:
    gcc -xc -E -v -

  1. For C++:
    gcc -xc++ -E -v -

The credit goes to Qt Creator team.

Finding out what the GCC include path is

The command

echo | gcc -E -Wp,-v -

will show the include path in use.

Change gcc include path globally

Typically for C++ it should be:

CPLUS_INCLUDE_PATH=/opt/local/include 
export CPLUS_INCLUDE_PATH

You can also set that in your .bash_profile for future use.

Update include path in linux

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.



Related Topics



Leave a reply



Submit