简体   繁体   English

C++ 20 从 Clang 中的主文件导入带有点符号的模块

[英]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:在使用Clang (stdlib=libc++) 构建的C++20项目中,我有以下结构:

testing_core.cppm 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 clang++ -c --std=c++20 -stdlib=libc++ -fmodules -fimplicit-modules -fbuiltin-module-map -fimplicit-module-maps -Xclang -emit-module-interface --precompile -o ./out/模块/接口/testing_core.pcm ./zero/ifc/testing/testing_core.cppm

testing_core.cpp 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 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 // 驱动代码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 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 .当模块名称为testing而不是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 .此外,我还可以从另一个模块接口文件(例如:testing.cppm)中export import testing.core ,但我只能在main.cpp文件中将其作为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.最后一个对我来说非常有意义,但我陷入了思考为什么我可以在模块接口单元中export import一个带有点的模块,但我不能在常规的cpp源文件上。

What I am missing with Clang here?我在这里与 Clang 缺少什么?

Thanks.谢谢。

Note: Code it's compiled with Clang 14.0.4 , under a Manjaro 5.13 .注意:代码是用Clang 14.0.4在 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.Clang中(至少在 14.0.4 之前的版本中),您必须为在其标识符中包含点符号的模块文件显式包含-fmodule-file=<some_interface>

So, for the main.cpp file build process, it must include -fmodule-file=./out/modules/interfaces/testing_core.pcm in the command line arguments.因此,对于main.cpp文件构建过程,它必须在命令行参数中包含-fmodule-file=./out/modules/interfaces/testing_core.pcm

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 .注意:当一个接口或实现文件依赖于另一个模块接口单元时,您还必须包含指示依赖关系的-fmodule-file ,即使模块标识符在其export module module_name中不包含任何点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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