Last active
September 9, 2024 01:32
-
-
Save michaeljclark/d17c2a85f51b30e96db6b12fda4a5581 to your computer and use it in GitHub Desktop.
C++ modules using clang and make
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
import bar; | |
int main() | |
{ | |
printf("%d\n", add(2, 3)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export module bar; | |
export int add(int x, int y) { return x + y; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 $@) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey there, thanks for the example!
I was wondering which license you consider the Makefile?