Friday, November 19, 2021

[SOLVED] How to use Clang with C++ Modules in Windows 10

Issue

I wanted to start testing and learn modules and move out of the days of header files and see if the experience differs. I installed everything required (Visual Studio module option in the installer), but Clang doesn't seem to be able to resolve the imports. When I switch over to MSVC on the other hand, it works fine. I'm currently working with VS 2019 Insider preview. So, how so I get clang (10, the latest release) to work?

EDIT: The error in question is module not found with Clang. MSVC works fine. What flags do I need to pass to Clang so that it can find the modules installed my VS-Installer? For example, I pass CLI argument to MSVC to enable modules.

EDIT: C++ File (main.cpp):

import std.core;
int main() {
    std::cout << "Hello CMake from C++20!\n" << std::endl;
    return 0;
}

CMake file

cmake_minimum_required (VERSION 3.16)  

project ("cpp20" LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

add_compile_options(
    "$<$<CXX_COMPILER_ID:MSVC>:/experimental:module>"
)

add_executable (cpp20 "main.cpp" )

add_compile_options(
    "$<$<CXX_COMPILER_ID:MSVC>:/experimental:module>"
#   something here to let Clang use modules. I tried -fmodules, 
#  -fmopodules-ts, -fbuiltin-module-map, -fimplicit-module-maps CLI, but none work.
)

So, how do I get Clang to find the modules install by VS, or modules that it might have installed.

Here is a zip of the sample "Hello World" project.

Thank you.


Solution

CMake does not support C++ modules. Once it is officially supported, you can expect it to work. Until than, the compiler or msbuild might do some magic, but you cannot rely on it.



Answered By - usr1234567