Can Header Files Be Accessed from Any Location on My Computer?

  • Thread starter Thread starter GregA
  • Start date Start date
  • Tags Tags
    files
AI Thread Summary
Creating custom header files for C++ programs allows for better organization and reuse of code. These header files can be accessed from any location on the computer by specifying their paths to the compiler. In Unix-like systems, the g++ compiler can be directed to search specific directories using the -I option or by setting the CPATH environment variable. The CPATH variable can be configured in the shell, with different commands depending on whether the user is in the Bourne shell or C shell. It is important to avoid modifying system header files directly, as this can lead to complications. For efficiency, users are encouraged to learn about using makefiles to automate the build process, which can simplify compiling programs by reducing the need to repeatedly type long commands.
GregA
Messages
210
Reaction score
0
If I make my own header file with both function declarations and descriptions in it is there anyway to make it such that these header files can be accessed from any location on my computer or do they have to be stored in the same path as the program that will be using it?
 
Technology news on Phys.org
There should be a way to tell your compiler where to look for header files besides the standard locations. For example, if you're using gcc or g++ at the command line in a Unix-like system you can tell it to look in a specific directory or directories using the -I option:

g++ myprog.cpp -I /my/include-file/directory

Or you can set the CPATH environment variable.
 
jtbell said:
There should be a way to tell your compiler where to look for header files besides the standard locations. For example, if you're using gcc or g++ at the command line in a Unix-like system you can tell it to look in a specific directory or directories using the -I option:

g++ myprog.cpp -I /my/include-file/directory

Or you can set the CPATH environment variable.


cheers for that...I've done a bit of googling but am still unsure about CPATH...do I write it as a preprocessor directive at the top of my program?
 
You write in on the command line (or even better, in your .cshrc file if you are using csh/tcsh on a Linux/Unix machine).
 
Where you put the include file search directories depends on which compiler you're using, which you have not told us.

- Warren
 
... and on which operating system you are using, and you haven't told us that, either.
 
GregA said:
am still unsure about CPATH...do I write it as a preprocessor directive at the top of my program?

In a Unix or Unix-like environment (e.g. Linux or Mac OS X) how you set environment variables depends on which shell you're using. In the Bourne shell (usually a $ prompt) you use

Code:
$ CPATH=/path/to/directory
$ export PATH

In the C shell and its derivatives (usually a % prompt) you use

Code:
% setenv CPATH /path/to/directory

To display what the variable contains (to make sure you did it OK):

Code:
% echo $CPATH

(showing the C-shell prompt)
 
Thanks for the replies and help folks...sorry for not giving all the details necessary; my operating system is "Fedora 8"

I'm not sure what my compiler is but to compile a program I use g++ progName -o prog, and to run it I use ./prog

edit 1: might not be the best way to do it but yesterday I went into root and flung my own header files into the include folder where all the old system headers are located such as iostream.h etc... and it seems to be working. (though perhaps I might run into some problems later)

one more edit: I'm not very good with linux yet :redface:
 
Never, ever go into root and change the system header files. This is a very bad idea.

You are using the gnu C++ compiler. You can find out how to use this program using the command 'man g++' or 'info g++'.

With the gnu C++ compiler there are two ways to specify the search path:
  • As -I options on the command line: g++ progName -o prog -I /path/to/directory
    You can make the compiler search multiple directories by specifying multiple -I options.
  • With the CPATH environment variable. You can also make the compiler search multiple directories by separating the paths with semicolons.
You can also use a combination of the two.
 
  • #10
D H said:
Never, ever go into root and change the system header files. This is a very bad idea.

You are using the gnu C++ compiler. You can find out how to use this program using the command 'man g++' or 'info g++'.

With the gnu C++ compiler there are two ways to specify the search path:
  • As -I options on the command line: g++ progName -o prog -I /path/to/directory
    You can make the compiler search multiple directories by specifying multiple -I options.
  • With the CPATH environment variable. You can also make the compiler search multiple directories by separating the paths with semicolons.
You can also use a combination of the two.

Cheers for that D H...I'll make a short path for my header files and undo what I did yesterday inside the /include folder :smile:
 
  • #11
GregA said:
my operating system is "Fedora 8"

Which is a distribution of Linux, so the methods that I described should work. The big thing to watch out for is that how you set environment variables depends on which shell you're using. If your command-line prompt has a "$" at the end, it's probably Bourne shell. If it's a "%" it's the C shell or one of its derivatives.

It's possible that the CPATH variable may already be set. To check this, give the command "echo $CPATH". If there's something there, you probably don't want to replace it with your include-file directory, but rather, add the directory to the beginning of the list. To do this, give the following command (for C shell; make a similar change for the Bourne shell version)

setenv CPATH "/your/include/directory:$CPATH"

(In general, CPATH contains a list of directories, separated by colons.)

I'm not sure what my compiler is but to compile a program I use g++

Then that's your compiler! :smile: Actually the formal name is "GNU C++" but everybody calls it "g++".
 
  • #12
By the way, if you get tired of typing long commands every time you want to build your program, you might want to start learning how to use 'make.' There are many tutorials online that will show you how to automate the process of building your project.

- Warren
 
  • #13
jtbell said:
Which is a distribution of Linux, so the methods that I described should work. The big thing to watch out for is that how you set environment variables depends on which shell you're using. If your command-line prompt has a "$" at the end, it's probably Bourne shell. If it's a "%" it's the C shell or one of its derivatives.

It's possible that the CPATH variable may already be set. To check this, give the command "echo $CPATH". If there's something there, you probably don't want to replace it with your include-file directory, but rather, add the directory to the beginning of the list. To do this, give the following command (for C shell; make a similar change for the Bourne shell version)

setenv CPATH "/your/include/directory:$CPATH"

(In general, CPATH contains a list of directories, separated by colons.)



Then that's your compiler! :smile: Actually the formal name is "GNU C++" but everybody calls it "g++".

it ends with $ so I think it will be the bourne shell based off what you have said.
Thanks for all the advice, I will certainly be doing some messing later :smile:
 
  • #14
chroot said:
By the way, if you get tired of typing long commands every time you want to build your program, you might want to start learning how to use 'make.' There are many tutorials online that will show you how to automate the process of building your project.

- Warren

Thats pretty much my motivation for wanting to know how to do this (to save typing out long commands) :smile:...will look that up cheers!
 
  • #15
GregA said:
it ends with $ so I think it will be the bourne shell based off what you have said.
Thanks for all the advice, I will certainly be doing some messing later :smile:

A quick and dirty way of finding out which shell you're running is to grep the list of currently running processes:

Code:
ps axu | grep sh

This lists the currently running processes (using 'ps' with the options 'a', 'u', and 'x') and pipes them through grep to search for the string 'sh'. Typically, this will give you a list of the running shells - bash, tcsh, ksh, and so on.

GregA said:
Thats pretty much my motivation for wanting to know how to do this (to save typing out long commands) ...will look that up cheers!

The following is the sort of thing you're looking for. Suppose that you've got a source file called main.cpp and you want to build it with a makefile. I often use something like the following.

Code:
LIB1 = -leng -lmx -lut -licuuc -licuio
LIB2 = -lmat -licudata -licui18n -lMTwister
LIB3 = -I/usr/local/Matlab2008a/extern/include
LIB4 = -L/usr/local/Matlab2008a/bin/glnx86

CXX = g++
CFLAGS = -Werror -Wall -W -Wshadow -fno-common -g \
         -ansi -pedantic -Wconversion -Wpointer-arith \
         -Wcast-qual -Wcast-align -Wwrite-strings -fshort-enums

main: main.cpp
	$(CXX) $(CFLAGS) main.cpp -o main $(LIB1) $(LIB2) $(LIB3) $(LIB4)

clean:
	-rm -rf *.o *~ main

The first four lines define objects (LIB1...4) that I use to tell the compiler the location of my libraries and includes. The next line defines the variable CXX to be my compiler, and the line after that lists some of the common flags that I pass to the compiler, things like debugging symbols (-g) and extra compiler warnings (-Wall, -W, and so on).

The next line defines a target called 'main' which depends on the file 'main.cpp'. I can then tell make to carry out the instructions associated with this target by saving the above file as "Makefile" in the same directory as main.cpp and issuing

Code:
make main

at a prompt. The 'clean' target is based on the same idea and allows me to clean up the directory by removing all the object files and emacs backups when I'm finished.

A couple of decent tutorials are http://www.cs.umd.edu/class/spring2002/cmsc214/Tutorial/makefile.html and http://www.hsrl.rutgers.edu/ug/make_help.html .
 
Last edited by a moderator:
Back
Top