DllMain在Cygwin上使用g++时表现异常

DllMain在Cygwin上使用g++时表现异常,dll,g++,cygwin,Dll,G++,Cygwin,我在Cygwin上使用gcc v4.8.3。我不确定德勒曼在这种情况下应该怎么做 当我使用mingw编译相同的代码时,它会按预期工作,并得到以下输出: Hello from DllMain Hello from main I'm a _WIN32 I'm a __MINGW32__ I'm a __cplusplus Calling workhorse Hello from workhorse ret 1 Hello from DllMain 当我在Cygwin中用g++编译它

我在Cygwin上使用gcc v4.8.3。我不确定德勒曼在这种情况下应该怎么做

当我使用mingw编译相同的代码时,它会按预期工作,并得到以下输出:

Hello from DllMain 
Hello from main 
I'm a _WIN32 
I'm a __MINGW32__ 
I'm a __cplusplus 
Calling workhorse 
Hello from workhorse 
ret 1 
Hello from DllMain
当我在Cygwin中用g++编译它时,我得到

Hello from DllMain
没别的了。帮助我想在g++版本中没有DllMain我也能活下去,但我似乎离它只有一小步之遥

以下是DLL代码dds.cpp:

 #include "dll.h"

 // #ifdef USE_DLLMAIN
 extern "C" BOOL APIENTRY DllMain(
   HMODULE               hModule,
   DWORD                 ul_reason_for_call,
   LPVOID                lpReserved)
 {
   printf("Hello from DllMain\n");
   return TRUE;
 }
 // #endif


 int STDCALL workhorse()
 {
   printf("Hello from workhorse\n");
   return TRUE;
 }
以下是头文件dll.h:

 #include <windows.h>
 #include <stdio.h>

 #if defined(_WIN32) || defined(__CYGWIN__)
 #    define DLLEXPORT __declspec(dllexport)
 #    define STDCALL __stdcall
 #else
 #    define DLLEXPORT
 #    define STDCALL
 #endif

 #ifdef __cplusplus
 #    define EXTERN_C extern "C"
 #else
 #    define EXTERN_C
 #endif

 EXTERN_C DLLEXPORT int STDCALL workhorse();

请不要在发布代码时包含行号。即使在编译可执行文件时,也将workhorse定义为dllexport。这是错误的。您应该只在编译DLL时使用dllexport。我已经编辑了行号,谢谢。我还尝试从dtest.cpp中删除include dll.h,而是引用了extern C int\uu stdcall工作区。不过,这没什么区别。请注意,在dtest.cpp的开头,我甚至没有从ifdef获取输出。有时,根据我不理解的小更改,我根本没有得到任何输出。您可能在运行时初始化代码的某个地方遇到了基于浮点的崩溃。我知道我得到了一个。我不太擅长用cygwin工具构建windows DLL,所以我不知道从这里继续。感谢您的关注。浮点错误听起来有点奇怪。如果有人在Cygwin上使用DllMain和g++编写了DLL代码,我也可以从中进行反向工作。
 #include "dll.h"

 int main()
 {
   printf("Hello from main\n");

 #ifdef _WIN32
   printf("I'm a _WIN32\n");
 #endif
 #ifdef __CYGWIN__
   printf("I'm a __CYGWIN__\n");
 #endif
 #ifdef __MINGW32__
   printf("I'm a __MINGW32__\n");
 #endif
 #ifdef __cplusplus
   printf("I'm a __cplusplus\n");
 #endif

   printf("Calling workhorse\n");
   printf("ret %d\n", workhorse());

 }