簡體   English   中英

Windows.h 在 Clang c++20 模塊中

[英]Windows.h in Clang c++20 modules

我開始使用 c++20 模塊開發我的第一個項目,我選擇 LLVM Clang 作為構建工具集。 問題是,當我嘗試包含 windows.h 文件時,clangs 就像我沒有包含一樣。 (只有當我在同一個翻譯單元中導入時才會發生這種情況)

module;

#include <windows.h>

export module SomeModule;

import Whatever;

export HWND Something(){...}

在這種情況下,無論使用 Windows.h 功能,都會拋出錯誤,即符號不存在。 例子:

error: missing '#include <windef.h>'; 'HWND' must be declared before it is used
    HWND hwnd;
    ^
C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared\windef.h:39:28: note: declaration here is not visible
DECLARE_HANDLE            (HWND);

為什么? 如何讓它發揮作用? 請幫忙。 提前致謝!

我最近能夠為 Visual Studio 2022 17.5(我沒有檢查任何其他編譯器)構建一個Windows C++20 模塊,它可以導入到我們公司的代碼庫中。 在所有需要Windows API的C++個源文件中,我再import Windows; 而不是傳統的#include <Windows.h> 請注意,此方法需要使用 API 函數的全名,如CreateFileW ,而不是傳統的宏版本CreateFile ,因為在模塊中永遠不會導出宏。 我認為這是一個優勢,不會用有時會產生奇怪的編譯錯誤的隱藏宏定義污染客戶端代碼。

模塊定義有點亂,但似乎有效。 這是一個縮寫版本,但你明白了:

module;

#include <string.h>
#include <stdio.h>
#define static // I know it is UB, but it works...

export module Windows;
#pragma warning(disable: 5244)
export
{
#define MICROSOFT_WINDOWS_WINBASE_H_DEFINE_INTERLOCKED_CPLUSPLUS_OVERLOADS 0
    // Windows declarations here
#include <winsock2.h>
#include <windows.h>
}
#include "Windows.macro.h"

未定義的行為#define static是必要的,因為在 Windows API 的某些地方,一些 C 函數被導出為__inline static ,這與export不兼容。

Windows.macro.h header 是從一個簡單的宏名稱列表生成的文件,用於導出到文本文件中。 我沒有導出所有的宏名稱(有很多),但只導出了我們實際使用的名稱。 header 看起來像這樣:

const auto TEMP_MB_ABORTRETRYIGNORE = MB_ABORTRETRYIGNORE;
#undef MB_ABORTRETRYIGNORE
export const auto MB_ABORTRETRYIGNORE = TEMP_MB_ABORTRETRYIGNORE;

const auto TEMP_MB_ICONSTOP = MB_ICONSTOP;
#undef MB_ICONSTOP
export const auto MB_ICONSTOP = TEMP_MB_ICONSTOP;

const auto TEMP_DATE_SHORTDATE = DATE_SHORTDATE;
#undef DATE_SHORTDATE
export const auto DATE_SHORTDATE = TEMP_DATE_SHORTDATE;
...

我最近了關於如何將 C 宏常量導出到 C++ 可導出宏常量的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM