Skip to content

Instantly share code, notes, and snippets.

@rofirrim
Last active May 22, 2023 19:45
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rofirrim/cefb7b2a730abd825c0326c96a0ef648 to your computer and use it in GitHub Desktop.
Example with modules
#include <iostream>
import myfancymodule;
int main(int argc, char *argv[])
{
int x = add(3, 4);
std::cout << "add(3, 4) = " << x << "\n";
int y = print_add(3, 4);
std::cout << " = " << y << "\n";
return 0;
}
CXX=$(HOME)/soft/llvm/install/bin/clang++ -std=c++20
all: main
main: main.o mymod.o mymod_impl_basic.o mymod_impl_print.o
$(CXX) -o $@ $+
main.o: main.cpp mymod.pcm
$(CXX) -fmodule-file=myfancymodule=mymod.pcm -c -o $@ $<
# mymod
mymod.o mymod.pcm &: mymod.cppm mymod_basic.pcm mymod_print.pcm
$(CXX) -c \
-fmodule-output \
-fmodule-file=myfancymodule:basic=mymod_basic.pcm \
-fmodule-file=myfancymodule:print=mymod_print.pcm $<
# mymod:basic
mymod_basic.o mymod_basic.pcm &: mymod_basic.cppm
$(CXX) -c \
-fmodule-output $<
mymod_impl_basic.o: mymod_impl_basic.cpp mymod.pcm
$(CXX) -c $<
# mymod:print
mymod_print.o mymod_print.pcm &: mymod_print.cppm
$(CXX) -c \
-fmodule-output $<
mymod_impl_print.o: mymod_impl_print.cpp mymod_print.pcm mymod_basic.pcm
$(CXX) -c \
-fmodule-file=myfancymodule:basic=mymod_basic.pcm $<
clean:
rm -vf *.o *.pcm *.o.tmp main
CXX=$(HOME)/soft/llvm/install/bin/clang++ -std=c++20
all: main
main: main.o mymod.o mymod_impl_basic.o mymod_impl_print.o
$(CXX) -o $@ $+
main.o: main.cpp mymod.pcm
$(CXX) -fmodule-file=myfancymodule=mymod.pcm -c -o $@ $<
# mymod
mymod.pcm: mymod.cppm mymod_basic.pcm mymod_print.pcm
$(CXX) --precompile \
-fmodule-file=myfancymodule:basic=mymod_basic.pcm \
-fmodule-file=myfancymodule:print=mymod_print.pcm $<
mymod.o: mymod.pcm
$(CXX) -c $<
# mymod:basic
mymod_basic.pcm: mymod_basic.cppm
$(CXX) --precompile $<
mymod_basic.o: mymod_basic.pcm
$(CXX) -c $<
mymod_impl_basic.o: mymod_impl_basic.cpp mymod.pcm
$(CXX) -c $<
# mymod:print
mymod_print.pcm: mymod_print.cppm
$(CXX) --precompile $<
mymod_print.o: mymod_print.pcm
$(CXX) -c $<
mymod_impl_print.o: mymod_impl_print.cpp mymod_print.pcm mymod_basic.pcm
$(CXX) -c \
-fmodule-file=myfancymodule:basic=mymod_basic.pcm $<
clean:
rm -vf *.o *.pcm *.o.tmp main
/* vim: syntax=C++ */
export module myfancymodule;
export import :basic;
export import :print;
/* vim: syntax=C++ */
export module myfancymodule:basic;
export int add(int a, int b);
export int sub(int a, int b);
module myfancymodule:basic;
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a + b;
}
module;
// Global module part.
#include <iostream>
module myfancymodule:print;
import :basic;
int print_add(int a, int b) {
std::cout << "add(" << a << ", " << b << ")";
return add(a, b);
}
int print_sub(int a, int b) {
std::cout << "sub(" << a << ", " << b << ")";
return sub(a, b);
}
/* vim: syntax=C++ */
export module myfancymodule:print;
export int print_add(int a, int b);
export int print_sub(int a, int b);
$ make -f Makefile.double-step
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 --precompile mymod_basic.cppm
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 --precompile mymod_print.cppm
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 --precompile \
-fmodule-file=myfancymodule:basic=mymod_basic.pcm \
-fmodule-file=myfancymodule:print=mymod_print.pcm mymod.cppm
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -fmodule-file=myfancymodule=mymod.pcm -c -o main.o main.cpp
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -c mymod.pcm
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -c mymod_impl_basic.cpp
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -c \
-fmodule-file=myfancymodule:basic=mymod_basic.pcm mymod_impl_print.cpp
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -o main main.o mymod.o mymod_impl_basic.o mymod_impl_print.o
$ ./main
add(3, 4) = 7
add(3, 4) = 7
$ make
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -c -fmodule-output mymod_basic.cppm
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -c -fmodule-output mymod_print.cppm
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -c \
-fmodule-output \
-fmodule-file=myfancymodule:basic=mymod_basic.pcm \
-fmodule-file=myfancymodule:print=mymod_print.pcm mymod.cppm
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -fmodule-file=myfancymodule=mymod.pcm -c -o main.o main.cpp
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -c mymod_impl_basic.cpp
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -c \
-fmodule-file=myfancymodule:basic=mymod_basic.pcm mymod_impl_print.cpp
/home/roger/soft/llvm/install/bin/clang++ -std=c++20 -o main main.o mymod.o mymod_impl_basic.o mymod_impl_print.o
$ ./main
add(3, 4) = 7
add(3, 4) = 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment