Let's put the source code of a library we need to build for different platforms into the src
directory. The Makefile script should start as follows:
CFLAGS = -O2 -I src
This line defines a variable CFLAGS
with a list of compiler command-line parameters. In our case, we instruct the compiler to search the src
directory for header files. If the library source code spans across multiple directories, we need to add the –I
switch for each of the directories. The -O2
switch tells the compiler to enable level 2 optimizations. Next, we add the following lines for each source file:
<SourceFileName>.o: gcc $(CFLAGS) –c <SourceFile>.cpp –o <SourceFile>.o
The string <SourceFileName>
should be replaced with the actual name of the .cpp
source file, and these lines should be written for each of the source files.
Now, we add the list of object files:
ObjectFiles = <SourceFile1>.o <SourceFile2>.o
Finally, we will write the target for...