0

If I have a header file List.h that contains the prototypes of the functions related to a list, the definitions of the functions are in a source file (c file) List.c. Both List.c file and the main.c file(or any source file representing the main program) include the List.h file. Now the main program has the prototypes of the list functions, but how did the definitions of the functions get included in the main program while there is no inclusion for the List.c file into main.c file? It is not about that the List.h and List.c files have the same name.

I am working on Windows and using MS Visual Studio.

Student
  • 805
  • 1
  • 8
  • 11
Ahmed Hesham
  • 43
  • 1
  • 6
  • 2
    Please post the [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that shows the problem, and how you compile it. – Weather Vane Aug 05 '18 at 20:49
  • 2
    @WeatherVane: The page you refer to begins “When asking a question about a problem caused by your code,…” The OP is asking a question about general principles, about how things work, not about a problem caused by their code. And their question is clear. Calling for an MCVE is inappropriate. – Eric Postpischil Aug 05 '18 at 21:46
  • @WeatherVane: I am just asking about a concept, I see that a code would not make sense. – Ahmed Hesham Aug 06 '18 at 00:42
  • @EricPostpischil I asked that because the actual code would be clearer than a description of the code (as has been commented before) and because there might have been something wrong with it. – Weather Vane Aug 06 '18 at 17:06

2 Answers2

2

For your scenario, you compile List.c to List.o (or maybe List.obj if you're working on Windows), and you compile main.c to main.o. Then you run the compiler again to link the two object files together, along with any other necessary libraries.

If you use GCC (the GNU C Compiler from the GNU Compiler Collection), then you might use:

gcc -Wall -Werror -std=c11 -c List.c
gcc -Wall -Werror -std=c11 -c main.c
gcc -Wall -Werror -std=c11 -o program main.o list.o

If you need to specify libraries, you'd add them after the object files.

You probably automate all this with a makefile, too.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thank you for your answer, but can you clarify the instructions like `gcc -Wall -Werror -std=c11 -c List.c` ? Where can I write them? You can post links to articles. – Ahmed Hesham Aug 05 '18 at 22:08
  • What platform are you working on? If it is a Unix-like system, you'd type them at a 'terminal' command line. If you're on Windows and using Cygwin or MinGW, you'd do the same in a 'cmd' or similar window. Or you could create a `makefile` (there are lots of questions about that on SO) and run `make` on the command line instead. If you're using an IDE (Code::Blocks, Eclipse, MS Visual Studio), you'll need to consult appropriate questions here on SO — or search via Google, Bing or any other search engine. (I have not yet spent enough time working out how to subdue any IDE so that it suits me.) – Jonathan Leffler Aug 05 '18 at 22:13
  • Are you using Visual Studio or some other IDE? I don't work on Windows very often, so there's a limit to how much more help I can provide. But there are many question on SO related to this topic. The basic ideas are the same regardless, but the details are very different. – Jonathan Leffler Aug 05 '18 at 22:36
  • I am using Visual Studio. – Ahmed Hesham Aug 05 '18 at 23:34
1

They are compiled separately. After compilation most compilers generate object files containing executable code, relocation, symbolic, debugging and some other information. Those object files are next "merged" together by linker program which uses the information from the object files to create the correct executable file.

This is of course a very simplified description and if you want to know more you should read more about it on internet.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • I already know this cycle, but I do not know how the function definitions get included into the main program, the main.c file only includes the file that has the prototypes in it. How does the compiler know that the function definitions must be included with the proper header file? and if this is related to an advanced subject can you please state its name is it "Compiler Design" ? – Ahmed Hesham Aug 05 '18 at 22:05
  • This all the other C file needs to know. What is the function nama and how to correctly call the function and what is the return value or what is the name and type of the external variable. The "body" of the function or variable is in the other object file and it is linked by the linker. – 0___________ Aug 05 '18 at 22:08
  • Do you mean that the compiler generates in the object file what means that these definitions are the definitions of the functions whose prototypes are included into the main program? and the linker uses this information in linking? – Ahmed Hesham Aug 05 '18 at 22:15
  • yes everything is in the object files. BTW details are not useful knowledge for the beginners as they do not write linkers or compilers. Just believe us. – 0___________ Aug 05 '18 at 23:02
  • I believe you. I thought that this is the time to know more deeply about this topic. – Ahmed Hesham Aug 06 '18 at 00:40