简体   繁体   中英

C++20 import modules with dot notation from main file in Clang

In a C++20 project built with Clang (stdlib=libc++), I have the next structure:

testing_core.cppm

export module testing.core;

export namespace testing {

    class TestSuite {
        public:
            static constexpr const char* var [] = { "Hi, constexpr!" };
    };

    void say_hello();
}

clang++ -c --std=c++20 -stdlib=libc++ -fmodules -fimplicit-modules -fbuiltin-module-map -fimplicit-module-maps -Xclang -emit-module-interface --precompile -o ./out/modules/interfaces/testing_core.pcm ./zero/ifc/testing/testing_core.cppm

testing_core.cpp

module testing.core;

import <iostream>;

namespace testing {
    void say_hello() {
        std::cout << "Hi, from the testing module!" << std::endl;
    }
}

clang++ -c --std=c++20 -stdlib=libc++ -fmodules -fimplicit-modules -fbuiltin-module-map -fimplicit-module-maps zero/src/testing_core.cpp -o ./out/modules/implementations/testing_core.o -fmodule-file=./out/modules/interfaces/testing_core.pcm

// Driver code main.cpp

import testing.core;
import <iostream>;

using namespace zero;

int main() {
    testing::say_hello();

    // Prints const char* first element memory address
    std::cout << testingTestSuite::var << std::endl;
    // Prints the deferenced value of the const char* constexpr
    std::cout << *testingTestSuite::var << std::endl;

    return 0;
}

clang++ --std=c++20 -stdlib=libc++ -fmodules -fimplicit-modules -fbuiltin-module-map -fimplicit-module-maps -o ./out/zero.exe main.cpp ./out/modules/interfaces/testing_core.pcm ./out/modules/interfaces/zero.pcm ./out/modules/interfaces/testing.pcm ./out/modules/implementations/testing.o ./out/modules/implementations/testing_core.o -fprebuilt-module-path=./out/modules/interfaces

The error:

main.cpp:3:8: fatal error: module 'testing.core' not found
import testing.core;
~~~~~~~^~~~~~~
1 error generated.

I am not being able of import modules from the main file if they have a dot in its identifier. I am able to import them without having a dot (import testing), when the module name is testing , not testing.core .

Also, I am also able to export import testing.core from another module interface file (for ex: testing.cppm), but I can only import it on the main.cpp file as testing . This last ones makes perfect sense to me, but I got trapped thinking in why I can export import in a module interface unit a module with dots in its identifier, but I can't on a regular cpp source file.

What I am missing with Clang here?

Thanks.

Note: Code it's compiled with Clang 14.0.4 , under a Manjaro 5.13 .

Solution it's pretty straightforward. In Clang , (at least in versions up to 14.0.4), you must explicitly include the -fmodule-file=<some_interface> for the module files that includes the dot notation in it's identifier.

So, for the main.cpp file build process, it must include -fmodule-file=./out/modules/interfaces/testing_core.pcm in the command line arguments.

Note: When an interface or implementation file depends on another module interface unit, you also must include the -fmodule-file indicating the dependency, even if the module identifier does not contains any dot in it's export module module_name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM