User Tools

Site Tools


software_carpentary2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
software_carpentary2 [2011/06/20 04:15]
medhamsh
software_carpentary2 [2011/06/20 04:23]
medhamsh
Line 231: Line 231:
 There are several other switches to the make utility. For more info, man make. There are several other switches to the make utility. For more info, man make.
  
 +===== Build Process =====
  
 +
 +   1. Compiler takes the source files and outputs object files
 +   2. Linker takes the object files and creates an executable
 +
 +==== Compiling by hand ====
 +
 +
 +The trivial way to compile the files and obtain an executable, is by running the command:
 +<​code>​
 +g++ main.cpp hello.cpp factorial.cpp -o hello
 +</​code>​
 +
 +==== The basic Makefile ====
 +
 +
 +The basic makefile is composed of:
 +
 +<​code>​
 +target: dependencies
 +[tab] system command
 +</​code>​
 +
 +This syntax applied to our example would look like:
 +
 +<​code>​
 +all:
 + g++ main.cpp hello.cpp factorial.cpp -o hello
 +</​code>​
 +
 +==== Using variables and comments ====
 +
 +You can also use variables when writing Makefiles. It comes in handy in situations where you want to change the compiler, or the compiler options.
 +<​code>​
 +# I am a comment, and I want to say that the variable CC will be
 +# the compiler to use.
 +CC=g++
 +# Hey!, I am comment number 2. I want to say that CFLAGS will be the
 +# options I'll pass to the compiler.
 +CFLAGS=-c -Wall
 +
 +all: hello
 +
 +hello: main.o factorial.o hello.o
 + $(CC) main.o factorial.o hello.o -o hello
 +
 +main.o: main.cpp
 + $(CC) $(CFLAGS) main.cpp
 +
 +factorial.o:​ factorial.cpp
 + $(CC) $(CFLAGS) factorial.cpp
 +
 +hello.o: hello.cpp
 + $(CC) $(CFLAGS) hello.cpp
 +
 +clean:
 + rm -rf *o hello
 +</​code>​
software_carpentary2.txt ยท Last modified: 2018/03/24 11:13 (external edit)