#include" libwithmain.h" int WINAPI wWinMain(HINSTANCE例,HINSTANCE pInstance,PWSTR cmdLine, int cmdShow){ $     foo();    返回0; } 构建libwithmain: cl%compiler_options %/ c libwithmain.cpp / Fo lib libwithmain.obj someuser.cpp #include" libwithmain.h" #ifdef CRT_OK #include < cstdio> #endif //使用.lib入口点 #ifdef MAIN_NEEDED extern"C" int main(){ #ifdef CRT_OK     printf(" \ nHello from main"); #endif    返回0; } $ //不要使用.lib入口点 #else $ int foo(){    返回0; } int main(){     printf("Hello from main()");     foo();    返回0; } #endif 以及构建一些用户的各种方法: 1)工作但不要求使用CRT cl%编译器%/ D MAIN_NEEDED someuser.cpp / link Kernel32 .lib libwithmain.lib / ENTRY:wWinMain 2)工作但使用libwithmain.obj而不是libwithmain.lib。  CRT可以使用。 $ cl%编译器%/ D MAIN_NEEDED / D CRT_OK someuser.cpp / link libwithmain.obj 3)我想要但无法与wWinMain合作的案例。 如果libwithmain.cpp使用WinMain()而不是wWinMain(),这可以正常工作。 如果使用wWinMain(),则链接器会给出 错误LNK2019未解析的外部符号WinMain在函数"int __cdecl __scrt_common_main_seg(void)"中引用(?__ scrt_common_main_seh @@ YAHXZ)zh-b $ b $ 链接某些用户时。 cl%编译器%/ DMAIN_NEEDED / D CRT_OK someuser.cpp / link libwithmain.lib / SUBSYSTEM:WINDOWS 这给我留下两个问题。 1)我的理解是这个场景中的.lib只是将一堆.obj文件合并为一个。 如果这是正确的,那么为什么构建一些链接到libwithmain.obj的用户工作但链接到libwithmain.lib不起作用。 2)我可以有crt main()调用wWinMain而不是WinMain来解决链接错误,如果是这样,我怎么告诉crt这样做?I am trying to set up a situation like the SDLs SDLMain2.lib, where a user can link to a .lib that contains WinMain() however, I would like to use wWinMain() instead of WinMain().  Here is what I have:libwithmain.h#ifdef MAIN_NEEDED#define main foo#endif#ifdef __cplusplusextern "C" {#endifint foo();#ifdef __cplusplus}#endiflibwithmain.cpp#include "libwithmain.h"int WINAPIwWinMain(HINSTANCE instance, HINSTANCE pInstance, PWSTR cmdLine, int cmdShow) {    foo();    return 0;}build libwithmain:cl %compiler_options% /c libwithmain.cpp /Folib libwithmain.objsomeuser.cpp#include "libwithmain.h"#ifdef CRT_OK#include <cstdio>#endif// Use .lib entry point#ifdef MAIN_NEEDEDextern "C" int main(){#ifdef CRT_OK    printf("\nHello from main");#endif    return 0;}// Do not use .lib entry point#elseint foo(){    return 0;}int main(){    printf("Hello from main()");    foo();    return 0;}#endifAnd a variety of ways to build someuser:1) Works but requires not using CRTcl %compiler% /D MAIN_NEEDED someuser.cpp /link Kernel32.lib libwithmain.lib /ENTRY:wWinMain2) Works but uses libwithmain.obj instead of libwithmain.lib.  CRT is okay to use.cl %compiler% /D MAIN_NEEDED /D CRT_OK someuser.cpp /link libwithmain.obj3) The case that I want but can't get to work with wWinMain.  If libwithmain.cpp uses WinMain() instead of wWinMain() this works just fine.  If wWinMain() is used the linker giveserror LNK2019 unresolved external symbol WinMain referenced in function "int __cdecl __scrt_common_main_seg(void)" (?__scrt_common_main_seh@@YAHXZ)when linking someuser.cl %compiler% /DMAIN_NEEDED /D CRT_OK someuser.cpp /link libwithmain.lib /SUBSYSTEM:WINDOWSThis leaves me with two questions.1) My understanding is that a .lib in this scenario is simply combining a bunch of .obj files into one.  If this is correct then why does building someuser with linking to libwithmain.obj work but linking to libwithmain.lib not work.2) Can I have crt main() call wWinMain instead of WinMain to resolve the link error and if so, how do I tell crt to do that?推荐答案 3)我想要但无法与wWinMain合作的案例。 如果libwithmain.cpp使用WinMain()而不是wWinMain(),这可以正常工作。 如果使用wWinMain(),则链接器会给出 错误LNK2019未解析的外部符号WinMain在函数"int __cdecl __scrt_common_main_seg(void)"中引用(?__ scrt_common_main_seh @@ YAHXZ)zh-b $ b $ 链接某些用户时。 cl%编译器%/ DMAIN_NEEDED / D CRT_OK someuser.cpp / link libwithmain.lib / SUBSYSTEM:WINDOWS3) The case that I want but can't get to work with wWinMain.  If libwithmain.cpp uses WinMain() instead of wWinMain() this works just fine.  If wWinMain() is used the linker giveserror LNK2019 unresolved external symbol WinMain referenced in function "int __cdecl __scrt_common_main_seg(void)" (?__scrt_common_main_seh@@YAHXZ)when linking someuser.cl %compiler% /DMAIN_NEEDED /D CRT_OK someuser.cpp /link libwithmain.lib /SUBSYSTEM:WINDOWS 尝试添加/ ENTRY:wWinMainCRTStartupTry adding /ENTRY:wWinMainCRTStartup 这篇关于有没有办法让CRT调用wWinMain而不是WinMain?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 05:09