How to link multiple implementation files in C

匆匆过客 提交于 2019-11-26 05:37:15

问题


I have a number of .c files, i.e. the implementation files say

  • main.c
  • A.c
  • B.c

Where functions from any of the files can call any function from a different files. My question being, do I need a .h i.e. header file for each of A and B\'s implementation where each header file has the definition of ALL the functions in A or B.

Also, main.c will have both A.h and B.h #included in it?

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

Thanks.


回答1:


Header contents

The header A.h for A.c should only contain the information that is necessary for external code that uses the facilities defined in A.c. It should not declare static functions; it should not declare static variables; it should not declare internal types (types used only in A.c). It should ensure that a file can use just #include "A.h" and then make full use of the facilities published by A.c. It should be self-contained, idempotent (so you can include it twice without any compilation errors) and minimal. You can simply check that the header is self-contained by writing #include "A.h" as the first #include line in A.c; you can check that it is idempotent by including it twice (but that's better done as a separate test). If it doesn't compile, it is not self-contained. Similarly for B.h and B.c.

For more information on headers and standards, see 'Should I use #include in headers?', which references a NASA coding standard, and 'Linking against a static library', which includes a script chkhdr that I use for testing self-containment and idempotency.

Linking

Note that main.o depends on main.c, A.h and B.h, but main.c itself does not depend on the headers.

When it comes to compilation, you can use:

gcc -o program main.c A.c B.c

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

gcc -c main.c
gcc -c A.c
gcc -c B.c
gcc -o program main.o A.o B.o



回答2:


You must provide an header file just if what is declared in a .c file is required in another .c file.

Generally speaking you can have a header file for every source file in which you export all the functions declared or extern symbols.

In practice you won't alway need to export every function or every variable, just the one that are required by another source file, and you will need to include it just in the required file (and in the source paired with the specific header file).

When trying to understand how it works just think about the fact that every source file is compiled on its own, so if it's going to use something that is not declared directly in its source file, then it must be declared through an header file. In this way the compiler can know that everything exists and it is correctly typed.




回答3:


It would depend on the compiler, but assuming you are using gcc, you could use something like this:

gcc -Wall main.c A.c B.c -o myoutput

Look at http://www.network-theory.co.uk/docs/gccintro/gccintro_11.html (first google answer) for more details. You could compile it into multiple object files/ libraries:

gcc -c main.c
gcc -c A.c
gcc -c B.c
gcc -o mybin main.o A.o B.o


来源:https://stackoverflow.com/questions/15622409/how-to-link-multiple-implementation-files-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!