Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a C program in gcc which has header files?

I want to compile a C program in gcc which has my 2 header files.

I am using the command:

gcc UDP_Receive.c -o UDP_Receive -lm

to compile it but I get an error stating "UDP_Data.h: No such file or directory"

How can I tell the compiler to include these header files?

Header Files:

#include "UDP_Data.h"

#include "Crypt.h"

Thanks, Ritesh

like image 980
Ritesh Banka Avatar asked Mar 02 '11 09:03

Ritesh Banka


People also ask

Can you compile header files?

Only source files are passed to the compiler (to preprocess and compile it). Header files aren't passed to the compiler. Instead, they are included from source files.

Do you compile header files in C?

c' files call the pre-assembly of include files "compiling header files". However, it is an optimization technique that is not necessary for actual C development. Such a technique basically computed the include statements and kept a cache of the flattened includes.

Does GCC need header files?

GCC needs to install corrected versions of some system header files. This is because most target systems have some header files that won't work with GCC unless they are changed. Some have bugs, some are incompatible with ISO C, and some depend on special features of other compilers.


2 Answers

Use -Idirectory to add include paths, or make your #include statement use relative paths.

EDIT: Also be aware that #include filenames are case sensitive on many platforms.

EDIT2: Use #include "UDP_Data.h" not #include <UDP_Data.h>

like image 81
Erik Avatar answered Oct 27 '22 17:10

Erik


You have told the compiler to include that file, with a line like this:

#include "UDP_Data.h"

the problem is that the compiler can't find that file, and don't forget that some platforms are case sensitive when it comes to filenames so "UDP_data.h" is not the same file as "UDP_Data.h". The compiler will serach in a few places by default, but you will need to add extra directories to its search by using command line options. The exact option will depend on the compiler, for gcc it's:

-I<directory>
like image 20
Skizz Avatar answered Oct 27 '22 17:10

Skizz