|
|
This page demonstrates how to perform the most common
C/C++ jobs, such as building libraries and binaries.
Here we see 3 different ways to build C/C++ binaries (and there are actually more ways to do it):
########################################################################
# Method #1: more or less conventional approach. We rely on implicit
# targets and we can use per-target rules to modify compile/link flags:
ifeq (1,1)
foo.o: CXXFLAGS+='-DBOGODEF="build method 1"'
foo: foo.o
foo: LDFLAGS+=-L/some/path/lib
foo: LDFLAGS+=-L/another/path/lib
all: foo
package.clean_files += foo
endif
###
# Method #2: use toc2.call.rules.c-bin, passing it .o file(s):
ifeq (1,1)
foo2.cpp: foo.cpp
cp foo.cpp foo2.cpp
package.clean_files += foo2.cpp
foo2.BIN.OBJECTS := foo2.o
foo2.o: CPPFLAGS+= '-DBOGODEF="build method 2"'
all: foo2
$(call toc2.call.rules.c-bin,foo2)
# The disadvantage to this approach is that the dependencies for
# foo2.BIN cannot be 100% automatically generated, but they can
# be mostly inferred.
endif
###
# Method #3: use toc2.call.rules.c-bin, passing it .cpp file(s):
ifeq (1,1)
foo3.cpp: foo.cpp
cp foo.cpp foo3.cpp
package.clean_files += foo3.cpp
foo3.BIN.SOURCES := foo3.cpp
foo3.BIN.CPPFLAGS := '-DBOGODEF="build method 3"'
foo3.BIN.LDFLAGS = -lstdc++
all: foo3.BIN
$(call toc2.call.rules.c-bin,foo3)
endif
Note that most dependencies tracking is handled automatically. That is,
when the dependencies can be figured out simply by reading the source
files, they are handled automatically. In some cases, such as in the
foo2.cpp and foo3.cpp examples above, we help out by providing
explicit dependency information.
As with binaries, there are multiple approaches to building libraries, but here are the simplest approaches: ######################################################################## # Build a static lib... ifeq (1,1) mylib.LIB.OBJECTS = libstuff.o $(call toc2.call.rules.c-lib,mylib) all: mylib.LIB endif # end static lib ######################################################################## ######################################################################## # Build a shared lib: # Method #1: from object file(s) ifeq (1,1) mylib.DLL.OBJECTS = libstuff.o $(call toc2.call.rules.c-dll,mylib) all: $(mylib.DLL) endif # Method #2: directly from source file(s) ifeq (1,1) mylib2.DLL.SOURCES = libstuff2.cpp libstuff2.cpp: ; cp libstuff.cpp $@ package.clean_files += libstuff2.cpp $(call toc2.call.rules.c-dll,mylib2) all: mylib2.DLL endif # end shared lib ########################################################################The toc2 manual goes into more detail about the other approaches to building C/C++ code. |
| toc is developed in conjunction with: s11n.net, SpiderApe, & wanderinghorse.net. | Site design adapted from a design by Sumanasa.com |