C/C++ programming on Linux
g++ option:
- -o : specify output filename
- -c : compile code to object file
- -I[path] : (UPPER i) where is header files
- -l[library] : (lower L) link to library
- -L[path] : linker directions
Build a main.c to an executable program
g++ main.c -o program_name
-> program_name on Linux
-> program_name.exe on Windows
Build a method.c to an object method.o. The method.o can provide its methods for others program
g++ -c method.c
-> method.o
If method.c is split to method.h and method.c (they are located in same directory):
g++ -c -I. method.c
-> method.o
The main.c uses methods in method.c
-> build main.c to main.o
-> build method.c to method.o
g++ main.o method.o -o program_name
Create a Makefile for above step (before g++ is a tab not a space):
main: main.o method.o g++ main.o method.o -o program_name main.o: main.c g++ -c main.c method.o: method.c g++ -c method.c clean: rm -f *.o *~ main
Run Makefile
make
Run clean
make clean
Loading