Question about including header files

K

KIRAN

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
 
I

Ian Collins

KIRAN said:
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.
 
A

Antoninus Twink

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.
 
J

James Kuyper

KIRAN said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top