<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Topics tagged with static]]></title><description><![CDATA[A list of topics that have been tagged with static]]></description><link>http://forum.d2learn.org/tags/static</link><generator>RSS for Node</generator><lastBuildDate>Tue, 11 Aug 2026 11:34:59 GMT</lastBuildDate><atom:link href="http://forum.d2learn.org/tags/static.rss" rel="self" type="application/rss+xml"/><pubDate>Invalid Date</pubDate><ttl>60</ttl><item><title><![CDATA[C++ 模块中静态全局变量的消失问题]]></title><description><![CDATA[<p dir="auto">错误例子（我写的）</p>
module;

#include "httplib.h"

export module mcp.compat.httplib;

export namespace mcp {
    using HttpClient = httplib::Client;
    using DataSink = httplib::DataSink;
    using Headers = httplib::Headers;
    using HttpRequest = httplib::Request;
    using HttpResponse = httplib::Response;
    using HttpServer = httplib::Server;

    #ifdef MCP_SSL
    using SslServer = httplib::SSLServer;
    #endif
}


<p dir="auto">正确例子（官方库提供的）</p>
module;

/*
 * Headers
 */

#ifdef _WIN32
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif //_CRT_SECURE_NO_WARNINGS

#ifndef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#endif //_CRT_NONSTDC_NO_DEPRECATE

#if defined(_MSC_VER)
#if _MSC_VER &lt; 1900
#error Sorry, Visual Studio versions prior to 2015 are not supported
#endif

#pragma comment(lib, "ws2_32.lib")

#ifndef _SSIZE_T_DEFINED
#define _SSIZE_T_DEFINED
#endif
#endif // _MSC_VER

#ifndef S_ISREG
#define S_ISREG(m) (((m) &amp; S_IFREG) == S_IFREG)
#endif // S_ISREG

#ifndef S_ISDIR
#define S_ISDIR(m) (((m) &amp; S_IFDIR) == S_IFDIR)
#endif // S_ISDIR

#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX

#include &lt;io.h&gt;
#include &lt;winsock2.h&gt;
#include &lt;ws2tcpip.h&gt;

#if defined(__has_include)
#if __has_include(&lt;afunix.h&gt;)
// afunix.h uses types declared in winsock2.h, so has to be included after it.
#include &lt;afunix.h&gt;
#define CPPHTTPLIB_HAVE_AFUNIX_H 1
#endif
#endif

#ifndef WSA_FLAG_NO_HANDLE_INHERIT
#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
#endif


#else // not _WIN32

#include &lt;arpa/inet.h&gt;
#if !defined(_AIX) &amp;&amp; !defined(__MVS__)
#include &lt;ifaddrs.h&gt;
#endif
#ifdef __MVS__
#include &lt;strings.h&gt;
#ifndef NI_MAXHOST
#define NI_MAXHOST 1025
#endif
#endif
#include &lt;net/if.h&gt;
#include &lt;netdb.h&gt;
#include &lt;netinet/in.h&gt;
#ifdef __linux__
#include &lt;resolv.h&gt;
#undef _res // Undefine _res macro to avoid conflicts with user code (#2278)
#endif
#include &lt;csignal&gt;
#include &lt;netinet/tcp.h&gt;
#include &lt;poll.h&gt;
#include &lt;pthread.h&gt;
#include &lt;sys/mman.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/un.h&gt;
#include &lt;unistd.h&gt;

#ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1)
#endif
#endif //_WIN32

#if defined(__APPLE__)
#include &lt;TargetConditionals.h&gt;
#endif

#include &lt;algorithm&gt;
#include &lt;array&gt;
#include &lt;atomic&gt;
#include &lt;cassert&gt;
#include &lt;cctype&gt;
#include &lt;chrono&gt;
#include &lt;climits&gt;
#include &lt;condition_variable&gt;
#include &lt;cstdlib&gt;
#include &lt;cstring&gt;
#include &lt;errno.h&gt;
#include &lt;exception&gt;
#include &lt;fcntl.h&gt;
#include &lt;functional&gt;
#include &lt;iomanip&gt;
#include &lt;iostream&gt;
#include &lt;list&gt;
#include &lt;map&gt;
#include &lt;memory&gt;
#include &lt;mutex&gt;
#include &lt;random&gt;
#include &lt;regex&gt;
#include &lt;set&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;sys/stat.h&gt;
#include &lt;system_error&gt;
#include &lt;thread&gt;
#include &lt;unordered_map&gt;
#include &lt;unordered_set&gt;
#include &lt;utility&gt;

#if defined(CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO) ||                        \
    defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
#if TARGET_OS_MAC
#include &lt;CFNetwork/CFHost.h&gt;
#include &lt;CoreFoundation/CoreFoundation.h&gt;
#endif
#endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO or
       // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
#ifdef _WIN32
#include &lt;wincrypt.h&gt;

// these are defined in wincrypt.h and it breaks compilation if BoringSSL is
// used
#undef X509_NAME
#undef X509_CERT_PAIR
#undef X509_EXTENSIONS
#undef PKCS7_SIGNER_INFO

#ifdef _MSC_VER
#pragma comment(lib, "crypt32.lib")
#endif
#endif // _WIN32

#if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
#if TARGET_OS_MAC
#include &lt;Security/Security.h&gt;
#endif
#endif // CPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO

#include &lt;openssl/err.h&gt;
#include &lt;openssl/evp.h&gt;
#include &lt;openssl/ssl.h&gt;
#include &lt;openssl/x509v3.h&gt;

#if defined(_WIN32) &amp;&amp; defined(OPENSSL_USE_APPLINK)
#include &lt;openssl/applink.c&gt;
#endif

#include &lt;iostream&gt;
#include &lt;sstream&gt;

#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
#if OPENSSL_VERSION_NUMBER &lt; 0x1010107f
#error Please use OpenSSL or a current version of BoringSSL
#endif
#define SSL_get1_peer_certificate SSL_get_peer_certificate
#elif OPENSSL_VERSION_NUMBER &lt; 0x30000000L
#error Sorry, OpenSSL versions prior to 3.0.0 are not supported
#endif

#endif // CPPHTTPLIB_OPENSSL_SUPPORT

#ifdef CPPHTTPLIB_MBEDTLS_SUPPORT
#include &lt;mbedtls/ctr_drbg.h&gt;
#include &lt;mbedtls/entropy.h&gt;
#include &lt;mbedtls/error.h&gt;
#include &lt;mbedtls/md5.h&gt;
#include &lt;mbedtls/net_sockets.h&gt;
#include &lt;mbedtls/oid.h&gt;
#include &lt;mbedtls/pk.h&gt;
#include &lt;mbedtls/sha1.h&gt;
#include &lt;mbedtls/sha256.h&gt;
#include &lt;mbedtls/sha512.h&gt;
#include &lt;mbedtls/ssl.h&gt;
#include &lt;mbedtls/x509_crt.h&gt;
#ifdef _WIN32
#include &lt;wincrypt.h&gt;
#ifdef _MSC_VER
#pragma comment(lib, "crypt32.lib")
#endif
#endif // _WIN32
#if defined(CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN)
#if TARGET_OS_MAC
#include &lt;Security/Security.h&gt;
#endif
#endif // CPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN

// Mbed TLS 3.x API compatibility
#if MBEDTLS_VERSION_MAJOR &gt;= 3
#define CPPHTTPLIB_MBEDTLS_V3
#endif

#endif // CPPHTTPLIB_MBEDTLS_SUPPORT

// Define CPPHTTPLIB_SSL_ENABLED if any SSL backend is available
// This simplifies conditional compilation when adding new backends (e.g.,
// wolfSSL)
#if defined(CPPHTTPLIB_OPENSSL_SUPPORT) || defined(CPPHTTPLIB_MBEDTLS_SUPPORT)
#define CPPHTTPLIB_SSL_ENABLED
#endif

#ifdef CPPHTTPLIB_ZLIB_SUPPORT
#include &lt;zlib.h&gt;
#endif

#ifdef CPPHTTPLIB_BROTLI_SUPPORT
#include &lt;brotli/decode.h&gt;
#include &lt;brotli/encode.h&gt;
#endif

#ifdef CPPHTTPLIB_ZSTD_SUPPORT
#include &lt;zstd.h&gt;
#endif


export module httplib;

export extern "C++" {
    #include "httplib.h"
}



<p dir="auto">当你试图将一个非模块化的第三方库（尤其是像 httplib 这种带全局状态或初始化逻辑的库）封装进模块时：</p>
<p dir="auto">不要尝试只导出部分类型：除非你非常确定该库没有全局初始化逻辑（Constructor static guards）。</p>
<p dir="auto">使用 export extern "C++"：这是将传统头文件“模块化”的最标准、最稳妥做法。</p>
<p dir="auto">环境对齐：在 module; 之后，务必把该库依赖的所有系统宏（如 _WIN32, WIN32_LEAN_AND_MEAN 等）都写清楚。</p>
]]></description><link>http://forum.d2learn.org/topic/171/c-模块中静态全局变量的消失问题</link><guid isPermaLink="true">http://forum.d2learn.org/topic/171/c-模块中静态全局变量的消失问题</guid><dc:creator><![CDATA[woshinideba1425]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>