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

48,740

Solution 1

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>

Solution 2

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>
Share:
48,740
Ritesh Banka
Author by

Ritesh Banka

Updated on July 29, 2022

Comments

  • Ritesh Banka
    Ritesh Banka almost 2 years

    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

  • Ritesh Banka
    Ritesh Banka about 13 years
    My header files are in the same location as my source code files.
  • Ritesh Banka
    Ritesh Banka about 13 years
    My header files are in the same location as my source code files. Could you please post a detailed example to walk me through the compilation process. Thanks
  • Erik
    Erik about 13 years
    Is the filename exactly UDP_Data.h? Not e.g. UDP_data.h?
  • Ritesh Banka
    Ritesh Banka about 13 years
    Yes, the file name is "UDP_Data.h"
  • Skizz
    Skizz about 13 years
    @Jim: How would you get that error if that line wasn't in the source file? (Apart from extra command line options of course!)
  • Erik
    Erik about 13 years
    Are you using #include "" and not #include <>?
  • Ritesh Banka
    Ritesh Banka about 13 years
    Yes, I am including the headers in ""
  • Erik
    Erik about 13 years
    Please edit your post and copy-paste any #include statements you have.
  • Ritesh Banka
    Ritesh Banka about 13 years
    #include "UDP_Data.h" #include "Crypt.h"
  • Erik
    Erik about 13 years
    According to the information you have given, this should work. If UDP_Data.h is in the same directory as UDP_Receive.c, and the filename is correct, this works. Doublecheck the filenames (look for hidden "weird" characters) both in the #include and the actual filename.