Yes, that is it. For those reading this, the complete command line will be:
g++ -Wall main.cpp ThinkingCap.cpp -o think
(Please don't name your executables "main". Name it something that explains what the program does...)
If you want to move to something a little more advanced, you can write a
makefile to compile things for you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
# The following are just convenience variables commonly used in makefiles.
# CC is the compiler name and any special flags needed to execute the compiler.
# CFLAGS are compile-flags: things like -Wall, -DDEBUG=1, etc.
# LFLAGS are link-flags: things like -lm and -lcurses and the like.
#
CC = g++
CFLAGS = -Wall
LFLAGS =
# Assuming Unix:
DELFILE = rm -f
# If you are on Windows, use the following instead:
#DELFILE = del /q
# A 'rule' is a target name, a colon, a list of dependencies, and then the
# instructions to compile the target. Each line of instructions must begin
# with a TAB character (not spaces!). A dependency is either a file name or
# another target in the makefile (or both).
#
# The very first rule names the default target. In other words, if you don't
# specify a target name when you run 'make', then the default target is
# built. (You can have more than one target in your makefiles. This makefile
# has three main targets: 'think', 'clean', and 'cleaner'.)
#
think: main.o ThinkingCap.o
$(CC) main.o ThinkingCap.o $(LFLAGS) -o think
main.o: main.cpp ThinkingCap.hpp
$(CC) -c main.cpp
ThinkingCap.o: ThinkingCap.cpp ThinkingCap.hpp
$(CC) -c ThinkingCap.cpp
clean:
$(DELFILE) *.o
# This also removes the executable file.
# Works for Unix and Windows... (but may complain about one of the files not
# existing to be deleted...)
cleaner: clean
$(DELFILE) think think.exe
|
Save the file as
Makefile in the same directory as your source (main.cpp, etc).
This is only a very basic Makefile. There are other things you can do with it, but don't worry too much about it. The GNU make program has a lot of nice extensions --I tend to use it over the usual make when I am using makefiles.
Makefiles have some significant problems, but are still pretty widely used. But the best part is, they make compiling your program very easy:
% make
% ./think
Compiles your program and executes it. If you are using GNU make, type
% gmake
instead. To get rid of those left-over object files (if you want to recompile
everything), make the 'clean' target instead of the default:
% make clean
Slick, isn't it?