Skip to content

Instantly share code, notes, and snippets.

@brianonn
Last active May 27, 2025 03:14
Show Gist options
  • Save brianonn/f10eaf000a4358478afb5b3cc8925b1d to your computer and use it in GitHub Desktop.
Save brianonn/f10eaf000a4358478afb5b3cc8925b1d to your computer and use it in GitHub Desktop.
a single Makefile bootstrap for a simple C++ project with cmake

a pre-make for CMake based projects

This is my simple pre-make script for bootstrapping CMake for a small project

Usage

Use the [RAW] button on the Makefileto find the raw file location, then copy it to your clipboard. Download this file to your local project workspace via curl with the URL or the raw file.

$ mkdir new-project
$ cd new-project
$ curl -sSL https://gist.github.io/raw-path-to-this-gist  > Makefile
$ make

See also

premake5 - A more comprehensive and cross-platform/cross-architecture pre-make system.

PROJECT = main
MAINCPP = main.cpp
CMAKELISTS = CMakeLists.txt
.PHONY: clean run bootstrap all
all: build/$(PROJECT)
build/$(PROJECT): $(MAINCPP) build
$(MAKE) -C build
build: $(CMAKELISTS)
@ rm -rf build
cmake -B build
run: build/$(PROJECT)
build/$(PROJECT)
clean:
rm -rf build
$(MAINCPP) $(CMAKELISTS):
rm -f $(CMAKELISTS) $(MAINCPP)
@ echo "cmake_minimum_required(VERSION 3.14)" >> $(CMAKELISTS)
@ echo "project(main)" >> $(CMAKELISTS)
@ echo "set(CMAKE_CXX_STANDARD 17)" >> $(CMAKELISTS)
@ echo "add_executable(main main.cpp)" >> $(CMAKELISTS)
@ echo "#include <iostream>" >> $(MAINCPP)
@ echo "" >> $(MAINCPP)
@ echo "int main(void) {" >> $(MAINCPP)
@ echo " // write new code here" >> $(MAINCPP)
@ echo " std::cout << \"main() done!\" << std::endl;" >> $(MAINCPP)
@ echo " return 0;" >> $(MAINCPP)
@ echo "}" >> $(MAINCPP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment