CMake is mostly useful if you want to have cross-platform support. If that is not needed, I'd suggest to just use what your compiler provides.
I don't know about homebrew, but for Linx (gnu g++) you use -L and -l options. The former specifies a search path for the library(ies), the latter the library to link to.
If you have a “/some/where/libXYZ.so” library file, it's something like “g++ -o myexe program.cpp -L/some/where -lXYZ”. Likely you want to set some optimization flags etc too, eg -O2 for g++
- Order of mentioning libraries (the -l options) matters. Some libraries use other libraries. The latter should be mentioned after the former so all references are resolved.
- Standard library paths are known by the compiler, so if the library is located there, no need for -L.
- Usually,there are several libXYZ….so files, with version numbers. These files are usually all soft-linked to the same library file. It gives you the option to specify how precise you want the version to match.
Simplest is to stick the entire compile process in a shell script, eg
#!/bin/sh
set -e -u -x
CCFLAGS="-Wall -g"
FLEXFLAGS=
bison --defines=tokens.h --output=parser.cpp parser.y
flex $FLEXFLAGS --outfile=scanner.cpp scanner.l
g++ $CCFLAGS -c -o parser.o parser.cpp
g++ $CCFLAGS -c -o scanner.o scanner.cpp
g++ $CCFLAGS -c -o encode.o encode.cpp
g++ $CCFLAGS -c -o ast.o ast.cpp
g++ $CCFLAGS -c -o image.o image.cpp
g++ $CCFLAGS -c -o output.o output.cpp
g++ $CCFLAGS -o encoder parser.o scanner.o encode.o ast.o image.o output.o -lpng
bison and flex are two code generators so they run before compiling. Then it compiles each cpp file to an .o file, and links it all at the last line with libpng.so I use “mk” as name for such a script, and make it executable, so I can type “./mk” to get a build.
Alternatively, you can do everything in one g++ command line. It's just like the last line, except with “.cpp” instead of ".o" (and no need for “g++ -c” lines then).
Such a script is simple but it compiles every cpp file each time, and runs only one g++ at a time, while you can compile .cpp to .o in parallel. As your program grows, this may become a nuisance. This is where a Makefile can help, but that is a different topic 🙂
EDIT: Some libraries come with a pkg-config setup, which saves you the trouble of figuring out how to compile and link exactly., You may run into these. See also
https://stackoverflow.com/questions/20416956/what-is-the-significance-of-pkg-config-in-gcc