0

I am not understanding how I should compile with gcc the following .c and library.

example:

Files: "commons.h" and "commons.c" (the .c defines function etc...)

Files: "level1.h" and "level1.c", level1.c includes "commons.h"

Files: "level2.h" and "level2.c", level2.c includes "commons.h" and "level1.h"

I tried this and got "undefined reference to x" (where x is a function inside level1):

gcc commons.c -c -o commons.o    (OK)
gcc level1.c -c -o level1.o      (OK)
gcc level2.c -o level2.o   (error, undefined reference to x)

I expect to have level2 to be execute, I will only execute this.

How should I compile? Which order? (an example with command line will help me understand)

user4789408
  • 1,196
  • 3
  • 14
  • 31
  • @uneven_mark Should I create a library everytime? ar rvs--- with commons, then do it again with level1 and again with level2? – user4789408 Aug 28 '19 at 01:45
  • @uneven_mark if you could post the shell command to compile these files correctly I will understand and mark the solution. I am a little bit lost – user4789408 Aug 28 '19 at 01:46
  • Are `commons`, `level1` and `level2` supposed to be compiled into one static/dynamic library, one executable, or multiple libraries? – walnut Aug 28 '19 at 01:46
  • @uneven_mark all the files will be used for just one executable, which is level2 – user4789408 Aug 28 '19 at 01:48
  • `gcc commons.c level1.c level2.c -o my_executable` does not work? – walnut Aug 28 '19 at 01:51
  • @uneven_mark maybe I or you missunderstood, I edited the post – user4789408 Aug 28 '19 at 01:57
  • Your last line of the edit has a typo, `-c` is missing. Afterwards you call `gcc common.o level1.o level2.o -o my_executable` to link the object files to an executable. – walnut Aug 28 '19 at 01:59
  • @uneven_mark this isn't a typo, level2 contains the main(). I have error when I try to compile level2 which is the source code of the "real" executable – user4789408 Aug 28 '19 at 02:02
  • Presumably, `level1.c` includes `level1.h` to ensure that the features declared in `level1.h` match those defined in `level1.c`, and similarly with `level2.c` and `level2.h`. If those implementation files do not include the corresponding headers, your naming conventions or your coding conventions (or both) leave much to be desired. See also [Should I use `#include` in headers?](https://stackoverflow.com/a/1804719/15168) – Jonathan Leffler Aug 28 '19 at 06:46

1 Answers1

2

Your last line should also use the -c flag in order to compile level2.c to an object file level2.o.

Afterwards the object files should be linked to create an executable, which is done via

gcc common.o level1.o level2.o -o my_executable

Alternatively you can directly supply all the source files to gcc without the -c flag, in which case it will perform all of the compilation and linking steps at once:

gcc common.c level1.c level2.c -o my_executable

Your error is currently caused, because you are instructing gcc without the -c option to compile and link the source file level2.c alone, but of course it can not be linked alone as it is missing symbols from the other source files.

The fact that level2.c contains main does not change anything about this. main is handled like any other function, only that it has a special meaning in the final executable as entry point.

walnut
  • 21,629
  • 4
  • 23
  • 59