1. Jun 28, 2008

### GregA

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?

2. Jun 28, 2008

### Staff: Mentor

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.

3. Jun 28, 2008

### GregA

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?

4. Jun 28, 2008

### D H

Staff Emeritus
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).

5. Jun 28, 2008

### chroot

Staff Emeritus
Where you put the include file search directories depends on which compiler you're using, which you have not told us.

- Warren

6. Jun 28, 2008

### D H

Staff Emeritus
... and on which operating system you are using, and you haven't told us that, either.

7. Jun 28, 2008

### Staff: Mentor

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 (Text):$ CPATH=/path/to/directory
$export PATH In the C shell and its derivatives (usually a % prompt) you use Code (Text): % setenv CPATH /path/to/directory To display what the variable contains (to make sure you did it OK): Code (Text): % echo$CPATH

(showing the C-shell prompt)

8. Jun 29, 2008

### GregA

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

9. Jun 29, 2008

### D H

Staff Emeritus
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. Jun 29, 2008

### GregA

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

11. Jun 29, 2008

### Staff: Mentor

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! Actually the formal name is "GNU C++" but everybody calls it "g++". 12. Jun 29, 2008 ### chroot Staff Emeritus 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. Jun 29, 2008 ### GregA 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

14. Jun 29, 2008

### GregA

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

15. Jun 29, 2008

### shoehorn

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

Code (Text):
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.

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 (Text):
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 (Text):
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 here and http://www.hsrl.rutgers.edu/ug/make_help.html [Broken].

Last edited by a moderator: May 3, 2017