Walker Walker - 1 year ago 90
C Question

c - compile main file with source files and header

I'm trying to compile a main file that uses a header. I have all of the files in my directory: card.c, deck.c header.h, and main.c
enter image description here

The issue is that when i do gcc main.c I get this: enter image description here

It seems like it's not recognizing the source and header files so my compiling commands are missing something. Thanks

Answer Source

You have source files card.c, deck.c, and main.c. It is reasonable to suppose that card.c and deck.c each define functions, at least one of which is called by something in main.c, else there would be no need for your header.h. Presumably, the functions named in your link errors are among those.

The command

gcc main.c

attempts to compile the code in main.c and link it to form a complete program, but it is not enough for a complete program because some of the needed functions are defined (implemented) in other source files. That's what the linker is complaining about.

If you want to compile main.c to an object file but not link it, then you need the -c option:

gcc -c main.c

If you want to build a complete program with one run of gcc then you must specify all the needed sources:

gcc main.c deck.c card.c

In the latter case, you might also want to use the -o option to specify a name different from a.out for the executable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download