473,395 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Question about including header files

Hello all,
My question is about the way of including header files(*.h) in source
files (*.c)
I have three folders,
-build ( for project makefiles)
-include ( for *.h files)
-src (for *.c files).

I know that there are two ways of specifying include path of header
files
1. using -I flag at the compilation ( for gcc)
2. including *.h files in source file with complete relative path
(Ex. to include test.h in test.c I would write #include "../include/
test.h" in test.c)

Which is the most preferred method of the above two?
Suggestions/help is apprecited. Thanks in advance.
Regards,
Kiran
Oct 10 '08 #1
3 2552
KIRAN wrote:
Hello all,
My question is about the way of including header files(*.h) in source
files (*.c)
I have three folders,
-build ( for project makefiles)
-include ( for *.h files)
-src (for *.c files).

I know that there are two ways of specifying include path of header
files
1. using -I flag at the compilation ( for gcc)
2. including *.h files in source file with complete relative path
(Ex. to include test.h in test.c I would write #include "../include/
test.h" in test.c)

Which is the most preferred method of the above two?
Which ever requires the lest work to setup and maintain.

I'd use compile options, then you only have to change the makefile(s) if
your layout changes.

--
Ian Collins
Oct 10 '08 #2
On 10 Oct 2008 at 2:55, KIRAN wrote:
I know that there are two ways of specifying include path of header
files
1. using -I flag at the compilation ( for gcc)
2. including *.h files in source file with complete relative path
(Ex. to include test.h in test.c I would write #include "../include/
test.h" in test.c)

Which is the most preferred method of the above two?
Usually the first is simpler, and makes life easier if you ever decide
to turn bits of the program into a library.

Just put -I../include in your CPPFLAGS in your Makefile - do it once and
you'll never have to think about it again.

Oct 10 '08 #3
KIRAN wrote:
Hello all,
My question is about the way of including header files(*.h) in source
files (*.c)
I have three folders,
-build ( for project makefiles)
-include ( for *.h files)
-src (for *.c files).

I know that there are two ways of specifying include path of header
files
1. using -I flag at the compilation ( for gcc)
This also works for most of the other C compilers I've used.
2. including *.h files in source file with complete relative path
(Ex. to include test.h in test.c I would write #include "../include/
test.h" in test.c)

Which is the most preferred method of the above two?
I definitely prefer the -I option. It gives you more freedom to
rearrange the structure of your project without having to rewrite the
source code. Moving the header file directory requires only the change
of a single line in my make file, rather than a change to every source
code file that #includes any file in that directory.
Oct 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: da Vinci | last post by:
Hello. First off, I am not sure of the exact jargon used, so I will ask a question regarding it. Then on to my other question. When you use things like cout and cin from the iostream header...
6
by: Johannes Bauer | last post by:
Hi group, I've got a question concerning inclusion of .hpp files. Currently I'm including all needed header files in the .cpp file. This means all dependencies of the package and all...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
2
by: Kevin Joplin | last post by:
Hi there, I've almost finished writing simple server application and i have one little doubt. Suppose we have main server code in server.c file. Rest of the code we divided into some pices and put...
36
by: Geiregat Jonas | last post by:
I've got this small app , from 2 .c files. One file contains the main and the other file contains functions used in the main. I compile this using gcc like this: gcc -c *c && gcc -o app *o. But I...
10
by: PCHOME | last post by:
Hi! Would someone please help me thess C error(in gcc on Linux)? The compiler continues to give me: readLP.o: In function `Input_Problem': readLP.o(.text+0x0): multiple definition of...
31
by: Joseph Wakeling | last post by:
Hello all, I'm writing some programs that will be using modules from the GNU Scientific Library (GSL). Include commands within the GSL modules have commands such as, #include...
1
by: Robby | last post by:
Hello: Can we pass a pointer to an object from one cpp file to another by using a function call. Assuming the object was created and declared along with its accessor methods in the first cpp...
11
by: mdh | last post by:
Well, K& R has finally gotten to header files!!! May I ask this. (Have checked out the FAQ) I am using "Xcode" to compile the program, so it may well be that this is doing something strange. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.