0

When I have a single C file for example, example.c and I need to use a method from a library, let's say math.h, at the top of my example.c I will have #include <math.h> and during the preprocessor stage it will take the function declarations out of math.h and place them into my math.c. The file will then be compiled and at this stage I will have an object file with an empty memory address pointing to the function I could be using inside math.h, let us says ceil. Only during the linking stage will the reference to the function ceil from the math.h library be corrected placed so that it can use the function correctly.

However, suppose I am writing the function ceil and I created my own math.c and math.h files, why does it need to be linked when I do not necessarily need to reference anything. Suppose inside my math.c I create a function add, that very simply, returns the addition of two given parameters, I don 't need to import anything, I don't reference anything, I have a very simple library with one function that adds two numbers, what exactly will happen during the linking stage when making my library?

  • "why does it need to be linked". Linked to what? If something uses your `math.c` functions then it needs to link in that object. If something doesn't need anything from your `math.c` then it doesn't need to link it in. It's not clear what you are really asking. – kaylum Oct 31 '20 at 06:53
  • @kaylum From what I have read, the three steps in the process to creating an executable is preprocessing -> compilation -> linking. However, if I made a `math.c` does that not reference anything outside of it's own file, however, many other files such as `example.c` will reference and use `math.c`, what happens or why does `math.c` need to be linked at all given that it includes one function that can perform it's tasks from the basic operations available in the language, i.e `x + y` – asyncisconfusing Oct 31 '20 at 07:04

0 Answers0