从C+导入函数时遇到问题+;dll错误LNK 2019 我试图编写和测试C++中的DLL文件,每当我想文件系统级访问时,我都可以调用它。当我试图访问C++中的DLL中的方法时,我现在感到头疼。奇怪的是,我能够在一个单独的C程序中调用代码,但没有什么麻烦,但是我想了解一下DLL交互在C++中是如何工作的。

从C+导入函数时遇到问题+;dll错误LNK 2019 我试图编写和测试C++中的DLL文件,每当我想文件系统级访问时,我都可以调用它。当我试图访问C++中的DLL中的方法时,我现在感到头疼。奇怪的是,我能够在一个单独的C程序中调用代码,但没有什么麻烦,但是我想了解一下DLL交互在C++中是如何工作的。,c++,dll,import,declspec,C++,Dll,Import,Declspec,这是我的虚拟可执行文件的.cpp,它应该只调用我的“newMain”测试方法 // dummy.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> //#pragma comment(lib,"visa32.lib") #pragma message("automatic link t

这是我的虚拟可执行文件的.cpp,它应该只调用我的“newMain”测试方法

// dummy.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
//#pragma comment(lib,"visa32.lib")
#pragma message("automatic link to adsInterface.dll")
#pragma message(lib, "adsInterface.lib"

extern "C" int __stdcall newMain();

int _tmain(int argc, _TCHAR* argv[])
{
 newMain();
 std::string i;
 std::cin >> i
 return 0;
}
以下是ADS接口的.h:

// adsInterface.h

#ifndef ADSINTERFACE_H
#define ADSINTERFACE_H

/* //save this for later i have no clue how this really works.
#ifdef ADSAPI_EXPORTS
#define ADSAPI __declspec(dllexport)
#else
#define ADSAPI __declspec(dllexport)
#endif
*/

namespace ADSInterface
{
  //test method. should print to console.
  __declspec(dllexport) int __stdcall newMain();

  void hello();
}
#endif
// adsInterface.cpp : Defines the exported functions for the DLL application.
//

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

namespace ADSInterface
{
  /* this is where the actual internal class and other methods will go */

  void hello()
  {
    std::cout << "hello from the DLL!" << std::endl;
  }


  __declspec(dllexport) int __stdcall newMain()
  {
    hello();
    return 0;
  }
}
下面是我的.cpp for adsInterface:

// adsInterface.h

#ifndef ADSINTERFACE_H
#define ADSINTERFACE_H

/* //save this for later i have no clue how this really works.
#ifdef ADSAPI_EXPORTS
#define ADSAPI __declspec(dllexport)
#else
#define ADSAPI __declspec(dllexport)
#endif
*/

namespace ADSInterface
{
  //test method. should print to console.
  __declspec(dllexport) int __stdcall newMain();

  void hello();
}
#endif
// adsInterface.cpp : Defines the exported functions for the DLL application.
//

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

namespace ADSInterface
{
  /* this is where the actual internal class and other methods will go */

  void hello()
  {
    std::cout << "hello from the DLL!" << std::endl;
  }


  __declspec(dllexport) int __stdcall newMain()
  {
    hello();
    return 0;
  }
}
奇怪的是,我能够用这一行在C#中导入方法(我也不必包含.lib文件):

当我正常调用它时,它运行:

newMain();

我已经阅读了很多关于如何导入dll函数的不同指南,我已经到了一个地步,我认为我只是把不同语言之间导入的不同方式弄乱了,把事情弄得一团糟。如果有人能够提供一些关于我应该如何导入C++中的DLL方法的见解,那将是非常值得赞赏的。< /P> < P>删除此声明:

extern "C" int __stdcall newMain();
并从_tmain调用ADSInterface::newMain()

在发布的代码中,您没有定义任何与该声明匹配的内容,是吗


或者让实现调用另一个,或者将该实现从名称空间拖到全局。

#在
dummy.cpp中包含“adsInterface.h”
,这很好,感谢所有帮助。我为这件事大发雷霆。只是一个小问题,有没有一种方法使我不必包含标题?就像这样,我只需要将dll作为资源提供?您可以使用正确的声明,因为它当前位于头中--在名称空间中。你不会有全部的麻烦,首先是避免使用名称空间:)。所以,如果我删除名称空间并使newMain()是全局的,我就不需要头了?还是仍然需要标题?标题没有真正的特殊性,其内容只是粘贴在#include上。如果一行声明直接出现在.cpp文本中或来自于.h,则没有区别。因此,标题从来都不是“必要的”,只是为了方便避免重复。
extern "C" int __stdcall newMain();