In all the code I see online, programs are always broken up into many smaller files. For all of my projects for school though, I\'ve gotten by by just having one gigantic C sour
Well, that's exactly what you want : split your code in several libraries !
Let's take an example, in one file you have :
#include
int something() {
return 42;
}
int bar() {
return something();
}
void foo(int i) {
printf("do something with %d\n", i);
}
int main() {
foo(bar());
return 0;
}
you can split this up to :
mylib.h:
#ifndef __MYLIB_H__
#define __MYLIB_H__
#include
int bar();
void foo();
#endif
N.B.: the preprocessor code above is called a "guard" which is used to not run twice this header file, so you can call the same include at several places, and have no compilation error
mylib.c:
#include
int something() {
return 42;
}
int bar() {
return something();
}
void foo(int i) {
printf("do something with %d\n", i);
}
myprog.c:
#include
int main() {
foo(bar());
return 0;
}
to compile it you do :
gcc -c mylib.c -I./
gcc -o myprog myprog.c -I./ mylib.o
now the advantages ?