About .c file and .h file in C programming?

in foo.c

there is a function f

, I put f Prototypes

in the header file...

and then 3 questions arise:

  • header file must have a name foo.h

    ?
  • foo.c

    and foo.h

    must be in the same directory?
  • If the answer to both questions is no , then the header file may be named f.h

    , foo.c

    and f.h

    may be in another directory. see an example:

~ / CFile / foo.c

#include "~/hfile/f.h"
int f(void){
     ...
}

      

~ / hfile / fh

int f(void);

      

~ / Main / cmain.c

#include "~/hfile/f.h"
int main(void){
    f(); 
    ...  
}

      

Then, when I call f

a function in cmain.c , cmain.c can find directive fh on #include

but cmain.c find foo.c by fh , because cmain.c includes only fh not include foo.c ? or how does the compiler or linker find foo.c using fh ?

+3


source to share


7 replies


1.the header file must be named foo.h?

This is not necessary, but it can be convenient and most of the time it is an unwritten rule.

2. foo.c and foo.h must be in the same directory?

It's not obligatory. You can specify include directories. Or you can refer to a directory for example.



#include "inc/foo.h"

      

3. If the answer to both questions is missing, that is, the header file may be named fh, foo.c and fh may be in a different directory. Then, when I call the f function, it can find the file fh with the #include directive, but how does the compiler or linker find foo.c and call f from foo.c?

See answer 2. For example, see gcc manual with -I option ( https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html )

Update: Since the question has been expanded, perhaps you can also look at the older post C / C ++ implementation header and files: How do they work? ...

+3


source


should the header file be named foo.h?

No, but using names that tie the relevant parts of the program together (even if it's a structural binding, not a logical link) is a very good thing. Remember that code / source trees are for humans, not machines.

foo.c and foo.h must be in the same directory

Not. But the included paths must be configured correctly in order foo.c

to be built.

but how does the compiler or linker find foo.c and call f from foo.c?



You have to tell these things to the compiler / linker. Check out their documentation. Since doing this for large projects can be a bit tricky, there are even tools that manage assemblies for you based on rules you define. One classic example is make

.


As for your added code samples. Merely including headers is not enough to create a program. The header contains only declarations. You should direct you to the linker to take the appropriate object files and link them to a single executable.

Think like a puzzle. Each declaration is placed in an object file with a specific shape. Each function definition is a bump in an object file with a specific shape. It is up to the linker to match the dent bump like you when you put the puzzle together.

+6


source


  • no, you can specify the header file you like
  • no, you can put the header file anywhere.
  • If the header file is not placed in the same location as the source file, you must pass the location of the header file to the compiler. How to do this for gcc is described in the answer to this question: How do I include header files in the GCC search path?
+2


source


You can name your files what you want. You can put your files almost anywhere you want, as long as you tell the compiler and linker correctly where those files are. You don't even need file names to end with .c

and .h

- these are mostly just conventions, but good ones. I really recommend that you go to .c

, and .h

completion of the files!

Let's say you call one file foo.c

and put it in a directory ~/my-sources

. Also say that you are calling another file bar.header

and putting it in a directory ~/my-headers

. Then your directive #include

should look something like this:

#include "../my-headers/bar.header"

      

Let's assume you are on a UNIX-like system. You will need to apply your knowledge of the operating system you are using for the directive to work properly #include

.

Since the header file is included in the source file when compiled, it is actually pretty much the same as the entire content if the header file is inside the source file. Therefore, as long as you tell the compiler where to find your files (on the command line when compiling and by using the correct path in #include directives), you can put the files anywhere.

+2


source


  • Not
  • Not
  • When you want to compile the output (could be a process or a static / dynamic library) you give the compiler the source files (in your case foo.c

    ) and then it will look for all those source files to define the function.
+2


source


  • should the header file be named foo.h?

NOT.

  1. foo.c and foo.h must be in the same directory?

NOT.

  1. If the answer to both questions is no, then the header file might be named fh, foo.c, and fh might be in a different directory. Then when I call the function f it can find the file fh with the #include directive, but how does the compiler or linker find foo.c and call f from foo.c?

It depends on you. If you put the header f.h

and source filefoo.c

in the same directory , just add the header file to the file foo.c

, for example:

#include "f.h"

      

But if you put the header f.h

and source file foo.c

in a different directory , add the header file path in foo.c

, for example:

#include "Your_header_file_Path/f.h"

      

+2


source


#include

literally inserts specified file in which it was used.

Take a look Have a intermediate preprocessor if you want to know what is ultimately provided to the compiler.

The component works differently, it just has a list of all objects. If there is an object, he can find it.
If you didn't link to it correctly, it will warn you with help implicit declaration

.

+1


source







All Articles