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

  • Thread starter GregA
  • Start date
  • Tags
    files
In summary: 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.)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.
  • #1
GregA
210
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
  • #2
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
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?
 
  • #4
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
Where you put the include file search directories depends on which compiler you're using, which you have not told us.

- Warren
 
  • #6
... and on which operating system you are using, and you haven't told us that, either.
 
  • #7
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)
 
  • #8
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:
 
  • #9
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:

1. What are header files in programming?

Header files are files that contain declarations of functions, variables, and other data types that are used in a program. They are typically included at the beginning of a source code file and are used to provide information to the compiler about the code that will be used in the program.

2. Why are header files important in programming?

Header files are important because they allow for the separation of interface and implementation in a program. This means that the code that is used to declare functions, variables, and data types is kept separate from the code that actually implements those functions. This makes the code more organized and easier to maintain.

3. How do I include a header file in my program?

To include a header file in your program, you can use the preprocessor directive #include followed by the name of the header file in angle brackets or double quotes. For example, #include or #include "myheader.h". This will essentially copy and paste the contents of the header file into your source code file.

4. Can I create my own header files?

Yes, you can create your own header files to use in your programs. It is common to create a header file for each source code file in your program. This helps to keep the code organized and makes it easier to make changes or additions to the program in the future.

5. Are there any best practices for using header files?

Yes, there are some best practices for using header files in programming. These include using include guards to prevent multiple inclusions of the same header file, avoiding including unnecessary header files, and using descriptive and consistent naming conventions for header files. It is also important to properly document the contents of your header files for other programmers to understand and use.

Similar threads

  • Programming and Computer Science
Replies
2
Views
620
  • Programming and Computer Science
Replies
2
Views
338
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
6
Views
887
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
9
Views
845
Back
Top