![](/img/trans.png)
[英]C++20 modules on clang (Windows): typeinfo error in simplest example
[英]C++20 import modules with dot notation from main file in Clang
在使用Clang
(stdlib=libc++) 構建的C++20
項目中,我有以下結構:
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/模塊/接口/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
// 驅動代碼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
錯誤:
main.cpp:3:8: fatal error: module 'testing.core' not found
import testing.core;
~~~~~~~^~~~~~~
1 error generated.
如果它們的標識符中有一個點,我將無法從主文件中導入模塊。 當模塊名稱為testing
而不是testing.core
時,我可以在沒有點的情況下導入它們(導入測試)。
此外,我還可以從另一個模塊接口文件(例如:testing.cppm)中export import testing.core
,但我只能在main.cpp
文件中將其作為testing
導入。 最后一個對我來說非常有意義,但我陷入了思考為什么我可以在模塊接口單元中export import
一個帶有點的模塊,但我不能在常規的cpp
源文件上。
我在這里與 Clang 缺少什么?
謝謝。
注意:代碼是用Clang 14.0.4
在 Manjaro 5.13
下編譯的。
解決方案非常簡單。 在Clang
中(至少在 14.0.4 之前的版本中),您必須為在其標識符中包含點符號的模塊文件顯式包含-fmodule-file=<some_interface>
。
因此,對於main.cpp
文件構建過程,它必須在命令行參數中包含-fmodule-file=./out/modules/interfaces/testing_core.pcm
。
注意:當一個接口或實現文件依賴於另一個模塊接口單元時,您還必須包含指示依賴關系的-fmodule-file
,即使模塊標識符在其export module module_name
中不包含任何點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.