Skip to content

Instantly share code, notes, and snippets.

@paulbuis
Last active August 29, 2015 14:17
Show Gist options
  • Save paulbuis/742a8ec87321ca09ebc1 to your computer and use it in GitHub Desktop.
Save paulbuis/742a8ec87321ca09ebc1 to your computer and use it in GitHub Desktop.
Simple C language separate compilation example
#include "foo.h"
int fooSum(struct foo* pFoo) {
return pFoo->a + pFoo->b;
}
struct foo {
int a;
int b;
};
#include <stdio.h>
#include "foo.h"
int main(int argc, char* argv[]) {
struct foo f;
f.a = 1;
f.b = 2;
int s = fooSum(&f);
}
CC = gcc
CFLAGS = -g -Wall
TARGET = foo.exe
OFILES = main.o foo.o
LIBS = -lm
all: $(TARGET)
foo.o: foo.c foo.h
main.o: main.c foo.h
foo.exe: $(OFILES)
$(CC) $(CFLAGS) -o foo.exe $(OFILES) $(LIBS)
clean:
$(RM) foo.exe *.o
@paulbuis
Copy link
Author

Directions to create an executable

create foo.o

gcc -c foo.c

create main.o

gcc -c main.c

link foo.o and main.o into foo.exe

gcc -o foo.exe main.o foo.o

compile in link as single command:

gcc -o foo.exe main.c foo.c

or use "make" to do this all for you

make

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment