You look confused. Read first the wikipage on linkers and on compilers. You don't link source files, but only object files and libraries.
(I am guessing and supposing and hoping for you that you are using Linux)
You also compile translation units into object files.
Header files are for the preprocessor (the first "phase" of the compilation). The preprocessing is a textual operation. See this answer for some hint.
So you probably want to compile your main.c
into main.o
with
gcc -Wall -Wextra -g -c main.c -o main.o
(the -Wall
asks for all warnings, so never forget that; the -Wextra
asks for more of them; the -g
asks for debugging information; -c
asks to compile some source into some object file; order of program arguments to gcc
matters a big lot).
Likewise, you want to compile your sortfile.c
into sortfile.o
. I leave as an exercise to get the right command doing that.
Finally, you want to get an executable program myprogsort
, by linking both object files. Do that with
gcc -g main.o sortfile.o -o myprogsort
But you really want to use some build automation tool. Learn about GNU make. Write your Makefile
(beware, tabs are important in it). See this example. Don't forget to try make -p
to understand (and take advantage of) all the builtin rules make
is knowing.
Learn to use some version control software, e.g. git, for your source code and tests.
Consider also using code generators (like GNU bison for parsing tasks) if and when relevant. Repetitive code could be generated by preprocessors (e.g. GPP).
Also would like to know what information usually goes in a header file.
Conventionally you want only declarations in your common header file (which you would #include
in every source file composing a translation unit). You can also add definitions of static inline
functions. Read more about inline functions (you probably don't need them at first).
Don't forget to learn how to use the gdb
debugger. You probably will run
gdb ./myprogsort
more than once. Don't forget to rebuild your thing after changes to source code.
Once you are confident with your code and have debugged simple bugs, you would compile it with optimization flags like -O2
(in addition, or in place of -g
). This is needed to benchmark your executable. Learn about profiling.
Some programs are designed to accept plugins (extending their features). On Linux or POSIX systems, this uses dlopen(3) and dlsym(3). Other programs cooperate with other executables and communicate with them (e.g. carefully using popen(3), sockets, RPC/XDR, JSONRPC, etc....)
Look also inside the source code of some medium sized free software project coded in C on github. You'll learn a big lot.
NB. On serious software projects senior developers would decide on some coding guidelines (e.g. GNU standards), rules and conventions, and others would review the code, perhaps even developing some GCC plugin to validate some of these coding rules. Be however aware of Rice's theorem.