Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about including header files

Reply
Thread Tools

Question about including header files

 
 
KIRAN
Guest
Posts: n/a
 
      10-10-2008
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-10-2008
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
 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      10-10-2008
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.

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-10-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
problem including perl header files shruti C Programming 2 05-15-2006 08:04 PM
UNIX header files to Windows header files anand.ba@gmail.com C Programming 3 05-01-2006 03:57 PM
Header files included in header files John Smith C Programming 18 07-24-2004 04:55 AM
Confusion over including header files...Can you give me a quick hand? Eternally C++ 7 07-17-2003 09:49 AM



Advertisments