Created
September 16, 2021 11:21
-
-
Save boki1/183f5cdb1d84dfbbda944ce3afd825cc to your computer and use it in GitHub Desktop.
C++20 Modules tryout
This file contains hidden or 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
cmake_minimum_required(VERSION 3.19) # Lower versions should also be supported | |
project(cpp20-modules) | |
# Add target to build iostream module | |
add_custom_target(std_modules ALL | |
COMMAND ${CMAKE_COMMAND} -E echo "Building standard library modules" | |
COMMAND g++ -fmodules-ts -std=c++20 -c -x c++-system-header iostream | |
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} | |
) | |
# Function to set up modules in GCC | |
function (prepare_for_module TGT) | |
target_compile_options(${TGT} PUBLIC -fmodules-ts) | |
set_property(TARGET ${TGT} PROPERTY CXX_STANDARD 20) | |
set_property(TARGET ${TGT} PROPERTY CXX_EXTENSIONS OFF) | |
add_dependencies(${TGT} std_modules) | |
endfunction() | |
# Program name and sources | |
set (TARGET prog) | |
set (SOURCES main.cpp) | |
set (MODULES mod.cpp) | |
# Setup program modules object library | |
set (MODULE_TARGET prog-modules) | |
add_library(${MODULE_TARGET} OBJECT ${MODULES}) | |
prepare_for_module(${MODULE_TARGET}) | |
# Setup executable | |
add_executable(${TARGET} ${SOURCES}) | |
prepare_for_module(${TARGET}) | |
# Add modules to application using object library | |
target_link_libraries(${TARGET} PRIVATE ${MODULE_TARGET}) |
This file contains hidden or 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
import math; | |
import <iostream>; | |
int main (int, char *argv[]) { | |
std::cout << add(2000, 20) << '\n'; | |
return 0; | |
} |
This file contains hidden or 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 math; | |
export int add(int fir, int sec){ | |
return fir + sec; | |
} |
This file contains hidden or 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
cmake . -Bout/ && make -C out/ && ./out/prog |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment