Separate Compilation
- We often just want compiler to compile particular portions of our code
- e.g. confirm code you just wrote is correct
- Frequent compilation can eliminate the issue of fixing all errors at end after finishing implementation problem
- How would we compile separate pieces of source code?
- If you try to compile without a main function, you get a linking error
- To compile something without linking, we can specify
-c
option, e.g. clang -c <file>.c
- generates object files
- object file - contains binary code for compiled code and additional information for linker, e.g. identifiers defined file and what identifiers are required
- to link object files together, just include all of them in final command for compilation, e.g.
clang file1.o file2.o file3.o -o output
Build Automation
- Build process - take source code, produce deployable product
- Build automation - reduction/elimination of manual developer input in favor of automating steps that must be performed
- ensures consistency on how software is built, allows for quality control to be embedded within the process
- developers don’t need to remember how to build something
- Modern build automation software can be used to perform tasks such as:
- downloading and importing new versions of libraries being used
- automatically retrieving the latest version of the source code e.g. from a git repository
- running a code analysis tool against the source code to check for suspicious code, formatting etc.
- running a documentation tool to generate revised documentation
- building a directory structure containing images, fonts and other resources for the executable to use
- compiling the code, to one or more targets e.g. iOS and Android
- running and reporting on automated tests
- creating an installer so that the program can be easily deployed
- Popular build automation software:
- Make - very old, for C/C++
- Ant - implemented in Java
- Maven - also for Java
- Gradle - can support C/C++, Scala, Kotlin, offically used for Android
- Bazel - build automation tool released by Google, part of their internal build tool
Make
- Makefile - textual representation detailing what dependencies exist in our project, how make should compile the program
- Makefiles concist of rules:
target: prerequisites
recipe
myprogram: main.o vec.o linalg.o
clang main.o vec.o linalg.o /u2/cs136l/pub/common/cs136.o -o myprogram
vec.o: vec.c vec.h
clang -c vec.c
linalg.o: linalg.c vec.h linalg.h
clang -c linalg.c
main.o: main.c vec.h linalg.h /u2/cs136l/pub/common/cs136.h
clang -c main.c -I/u2/cs136l/pub/common
.PHONY: clean
clean:
rm *.o myprogram
- Corresponding dependency graph:
