C++20 使用 Visual Studio 编译模块:不编译或导入 ixx 文件
C++20 compiling modules with Visual Studio: doesn't compile or import ixx files
>Visual Studio 2019不会尝试编译我的.cxx或.ixx文件。这是我的 .cxx 文件:
export module greetings;
import std.core;
export std::string get_greeting_text()
{
return "Hello, World!";
}
这是主要的:
import std.core;
import greetings;
int main()
{
std::cout << get_greeting_text() << 'n';
}
我确实设置了这些标志:/std:c++latest
,/experimental:module
。错误消息是
C:...main.cpp(2,17):error C2230: could not find module 'greetings'
C:...main.cpp(6,2): error C3861: 'get_greeting_text': identifier not found
。但我没有看到任何关于尝试编译 greetings.cxx 的行,所以这一定是问题所在。将其更改为 .ixx 不起作用。有什么解决办法?
解决方案:
- 将问候语.ixx添加到头文件。(将其添加到源文件不起作用。
- 右键单击 greeting.ixx 上的属性,然后
- 将项类型设置为 C/C++ 编译器
- 将"从内部版本排除"设置为"否"。
- 救
- 建
似乎有点片状。除非我先进行清理,否则重建失败。
模块声明export module greetings;
尚未在Visual Studio 2019上运行。
您可以尝试为您的greetings.cxx文件添加以下编译器意见:
/module:export /module:name greetings /module:wrapper greetings.h /module:output greetings.ifc -c greetings.cxx
另一种解决方案,将greetings.cxx重命名为greetings.ixx。.ixx 扩展名是 Visual Studio 中的模块接口文件所必需的。
为了在MSVC中使用不同的文件扩展名,以便您可以在Visual Studio Code中使用智能感知,实际上有一个解决方法。
使用cl.exe
命令,有一个"/interface"选项。 您可以这样做:
在我的 cl.exe 命令文件中:
/interface /Tp UI/ApplicationHost.cpp
/reference ApplicationHost=BuildDebugApplicationHost.ifc
UI/WorldEngine.cpp
/link /SUBSYSTEM:WINDOWS
Build/Debug/pch.obj
/接口- 让编译器知道有一个 ifc 模块/标头即将出现。/Tp- 强制编译器将其识别为C++文件,如果重命名为
.cppm
很有用。/reference- 告知编译器从何处获取以下源代码的依赖项信息。