Created
March 28, 2024 04:01
-
-
Save chen3feng/dd0c898385fff2a2ec7ce0fbe2da3fb2 to your computer and use it in GitHub Desktop.
Weak symbol in VC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
extern const char * pWeakValue; | |
static const char * pDefaultWeakValue = "nullptr"; | |
// #pragma comment(linker, "/alternatename:_pWeakValue=_pDefaultWeakValue") | |
#define DEFINE_WEAK(Type, Name) \ | |
extern Type Name; \ | |
static Type Name##Default; \ | |
__pragma(warning(push)) \ | |
__pragma(comment(linker, "/alternatename:_" #Name "=_" #Name "Default")) | |
#define PRAGMA_ENABLE_SHADOW_VARIABLE_WARNINGS \ | |
__pragma(warning(pop)) | |
PRAGMA_ENABLE_SHADOW_VARIABLE_WARNINGS | |
DEFINE_WEAK(int, N); | |
int main() { | |
extern int X; | |
printf("pWeakValue=%p\n", &X); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment