Skip to content

Instantly share code, notes, and snippets.

@michaeljclark
Last active September 9, 2024 01:32
Show Gist options
  • Save michaeljclark/d17c2a85f51b30e96db6b12fda4a5581 to your computer and use it in GitHub Desktop.
Save michaeljclark/d17c2a85f51b30e96db6b12fda4a5581 to your computer and use it in GitHub Desktop.
C++ modules using clang and make
#include <cstdio>
import bar;
int main()
{
printf("%d\n", add(2, 3));
}
export module bar;
export int add(int x, int y) { return x + y; }
#
# clang modules-ts example
#
source = src
modules = build/modules
objects = build/objects
output = build/output
CXX = clang++
CXX_FLAGS = -std=c++2a -O2 -fmodules -fmodules-ts -fprebuilt-module-path=$(modules)
#
# To view commands use: make V=1
#
ifdef V
cmd = @mkdir -p $2 ; echo "$3"; $3
else
cmd = @echo "$1"; mkdir -p $2 ; $3
endif
#
# make rules
#
all: modules $(output)/app
modules: $(patsubst $(source)/%.ccm,$(modules)/%.pcm,$(wildcard $(source)/*.ccm))
clean: ; rm -fr build
$(modules)/%.pcm: $(source)/%.ccm
$(call cmd,CCM $@, $(@D),$(CXX) $(CXX_FLAGS) --precompile $^ -o $@)
$(objects)/%.o : $(source)/%.cc
$(call cmd,CC $@, $(@D),$(CXX) $(CXX_FLAGS) -c $^ -o $@)
$(output)/%: $(objects)/%.o
$(call cmd,LD $@, $(@D),$(CXX) $(CXX_FLAGS) $^ -o $@)
@Fighter19
Copy link

Hey there, thanks for the example!
I was wondering which license you consider the Makefile?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment