├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md ├── hello.cpp ├── hello.ixx ├── magic.cmake └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | project(HelloWorld) 3 | 4 | # Include the magic 5 | include(magic.cmake) 6 | 7 | # Create a project 8 | magic_add_executable(hello_world 9 | main.cpp 10 | hello.ixx 11 | hello.cpp) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Rick de Water 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ModuleHelloWorld 2 | This is a very basic example of how to get C++20 modules to work with CMake and the latest Visual Studio Preview. 3 | 4 | It'll allow you to start experimenting with modules right now, but it does come with many limitations. Do not use this in any production environment, and instead wait for compilers and build tools to be updated with proper support. 5 | 6 | # Requirements and limitations 7 | - Visual Studio 16.8.0 Preview 3 or higher for basic module support. 8 | - Visual Studio 16.8.0 Preview 4 or higher for module partition support. 9 | - Only the Visual Studio generator in CMake will work. You cannot for example use Ninja with MSVC. MSBuild has to do most of the work since CMake does not actually support modules (yet). 10 | - Since we rely on MSBuild's default behaviour, the module interface file extensions have to be either `.ixx` or `.cppm`. Extensions like `.mpp` will not work. 11 | -------------------------------------------------------------------------------- /hello.cpp: -------------------------------------------------------------------------------- 1 | module; 2 | #include 3 | module hello; 4 | 5 | void hello_world() 6 | { 7 | std::cout << "Hello World!"; 8 | } -------------------------------------------------------------------------------- /hello.ixx: -------------------------------------------------------------------------------- 1 | export module hello; 2 | 3 | export void hello_world(); -------------------------------------------------------------------------------- /magic.cmake: -------------------------------------------------------------------------------- 1 | # This function demonstrates how to trick CMake into configuring module interface files correctly. 2 | function(magic_add_executable TARGET) 3 | 4 | # Create the executable as normal 5 | add_executable(${ARGV}) 6 | 7 | # Also set the C++ version of the target to C++20 8 | target_compile_features("${TARGET}" PUBLIC cxx_std_20) 9 | 10 | # Get the sources that were added to the target 11 | get_target_property(TARGET_SOURCES "${TARGET}" SOURCES) 12 | 13 | # Filter out the module interface units 14 | list(FILTER TARGET_SOURCES INCLUDE REGEX "(.ixx|.cppm)$") 15 | message("Interface units: ${TARGET_SOURCES}") 16 | 17 | # Override CMake's behaviour of explicitly setting the compilation tools, 18 | # and instead set it to the default and let MSBuild decide. 19 | set_source_files_properties(${TARGET_SOURCES} PROPERTIES VS_TOOL_OVERRIDE "ClCompile") 20 | 21 | endfunction() -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | import hello; 2 | 3 | int main(int argc, char* argv[]) 4 | { 5 | hello_world(); 6 | } --------------------------------------------------------------------------------