Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当使用MinGW和wclang交叉编译DLL时,我真的需要u declspec(dllexport)吗?_C++_Dll_Mingw - Fatal编程技术网

C++ 当使用MinGW和wclang交叉编译DLL时,我真的需要u declspec(dllexport)吗?

C++ 当使用MinGW和wclang交叉编译DLL时,我真的需要u declspec(dllexport)吗?,c++,dll,mingw,C++,Dll,Mingw,这里也有类似的问题,但它们不能完全回答我的问题: 当我使用MinGW和从Mac OS X交叉编译DLL时,为什么我的DLL没有使用\uu declspec? 在函数声明之前使用\uu declspec(dllexport),以及我看到的每一个这样做的引用。然而,我的9000行库中没有一个代码使用它,而且DLL工作得很好 例如,这里有一个以相同方式构建的人工示例库: #include <stdio.h> extern "C" { int hello(const char*

这里也有类似的问题,但它们不能完全回答我的问题:

当我使用MinGW和从Mac OS X交叉编译DLL时,为什么我的DLL没有使用
\uu declspec

在函数声明之前使用
\uu declspec(dllexport)
,以及我看到的每一个这样做的引用。然而,我的9000行库中没有一个代码使用它,而且DLL工作得很好

例如,这里有一个以相同方式构建的人工示例库:

#include <stdio.h>

extern "C" {

int hello(const char* name) {
  printf("Hello, %s!\n", name);
  return 0;
}

}
生成外观良好的DLL:

和我的Windows应用程序:

#include "stdafx.h"
#include <Windows.h>
#include <iostream>

typedef int(*hellofn)(const char*);

int _tmain(int argc, _TCHAR* argv[])
{
  DWORD err;

  HINSTANCE dll = LoadLibrary(L"E:\\test.dll");
  if (!dll) {
    err = GetLastError();
    std::cout << "Can't load library: " << err << std::endl;
    return 1;
  }

  hellofn hello = (hellofn)GetProcAddress(dll, "hello");
  if (!hello) {
    err = GetLastError();
    std::cout << "Could not load the function: " << err << std::endl;
    return 2;
  }

  int ret = hello("nerd");
  std::cout << "hello() returned " << ret << std::endl;

  return 0;
}
#包括“stdafx.h”
#包括
#包括
typedef int(*hellofn)(常量字符*);
int _tmain(int argc,_TCHAR*argv[]
{
德沃德·厄尔;
HINSTANCE dll=LoadLibrary(L“E:\\test.dll”);
如果(!dll){
err=GetLastError();

std::cout不,在使用MinGW构建DLL时,您不需要
\uu declspec(dllexport)
(事实上,我自己经常忽略它)。需要注意的是,如果DLL中只包含一个符号是如此修饰的,那么您希望导出的所有其他符号都必须同样修饰,(除非在构建DLL时将
--导出所有符号
选项传递到链接器)

如果仅包含未装饰的符号,则将导出所有全局符号,就像默认情况下指定了
--导出所有符号一样


但是,这与
\uu stdcall
\uu cdecl
调用约定或名称损坏无关;它只是DLL导出表中符号可见性的决定因素。如果您不将函数声明为
\uu stdcall
\uu cdecl
,则它们将是
\uu cdecl
默认情况下,这是没有问题的,只要提供DLL和调用方在该约定上达成一致。同样,如果双方都同意任何命名规则的约定(通常意味着,在C++的情况下,它们在编译时使用相同的编译器),那么就不会有链接问题。
#include "stdafx.h"
#include <Windows.h>
#include <iostream>

typedef int(*hellofn)(const char*);

int _tmain(int argc, _TCHAR* argv[])
{
  DWORD err;

  HINSTANCE dll = LoadLibrary(L"E:\\test.dll");
  if (!dll) {
    err = GetLastError();
    std::cout << "Can't load library: " << err << std::endl;
    return 1;
  }

  hellofn hello = (hellofn)GetProcAddress(dll, "hello");
  if (!hello) {
    err = GetLastError();
    std::cout << "Could not load the function: " << err << std::endl;
    return 2;
  }

  int ret = hello("nerd");
  std::cout << "hello() returned " << ret << std::endl;

  return 0;
}