How to link multiple implementation files in C

I have several .c files, i.e. implementation files say

  • main.c
  • Ac
  • Bc

If functions from any of the files can call any function from different files. My question is: do I need a .h ie header file for each implementation of A and B, where each header file has the definition of ALL functions in or B.

In addition, main.c will contain Ah and Bh #included ?

If someone can finally make it clear, also how can I later compile and run multiple files in the terminal.

Thanks.

+8
c compilation header implementation
source share
3 answers

Header content

The Ah header for Ac should contain only the information that is needed for external code using the tools defined in Ac . It should not declare static functions; It should not declare static variables; it should not declare internal types (types used only in Ac ). This should ensure that the file can only use #include "Ah" and then take full advantage of the facilities published by Ac . It should be autonomous, idempotent (so you can enable it twice without compilation errors) and minimal. You can simply verify that the header is self-sufficient by writing #include "Ah" as the first line of #include in Ac ; You can verify that it is idempotent by turning it on twice (but this is best done as a separate test). If it does not compile, it is not autonomous. Similarly for Bh and Bc .

For more information about headers and standards, see “ Should I use #include in headers? ” Which refers to the NASA coding standard, and “ Link to a static library ”, which includes the chkhdr script that I use to test self-sufficiency and idempotency .

compound

Note that main.o dependent on main.c , Ah and Bh , but main.c itself is independent of headers.

When it comes to compilation, you can use:

 gcc -o program main.c Ac Bc 

If you need other options, add them (most flags at the beginning; libraries at the end, after the source code). You can also compile each file into object code separately, and then link the object files together:

 gcc -c main.c gcc -c Ac gcc -c Bc gcc -o program main.o Ao Bo 
+21
source share

You should provide a header file only if what is declared in the .c file is required in another .c file.

Generally speaking, you can have a header file for each source file in which you export all declared functions or extern characters.

In practice, you do not have to export each function or each variable, only the one that is required by another source file, and you will only need to include it in the required file (and in the source connected to the specific header file).

When trying to understand how this works, just think that each source file is compiled on its own, so if it is going to use something that is not declared directly in its source file, then it should be declared via the file header. Thus, the compiler can know that everything exists and is correctly printed.

+3
source share

This will depend on the compiler, but if you use gcc, you can use something like this:

 gcc -Wall main.c Ac Bc -o myoutput 

See http://www.network-theory.co.uk/docs/gccintro/gccintro_11.html (Google's first answer) for more details. You can compile it into several object files / libraries:

 gcc -c main.c gcc -c Ac gcc -c Bc gcc -o mybin main.o Ao Bo 
+3
source share

All Articles