c ++导出和使用dll函数 [英] c++ exporting and using dll function

查看:125
本文介绍了c ++导出和使用dll函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不清楚哪里有错误。我正在创建一个DLL,然后在C ++控制台程序(Windows 7,VS2008)中使用它。但是当尝试使用DLL函数时,我得到 LNK2019未解析的外部符号



首先导出:

  #ifndef __MyFuncWin32Header_h 
#define __MyFuncWin32Header_h

#ifdef MyFuncLib_EXPORTS
#define MyFuncLib_EXPORT __declspec (dllexport)
#else
#define MyFuncLib_EXPORT __declspec(dllimport)
#endif

#endif

这是我之前使用的一个头文件:

  ifndef __cfd_MyFuncLibInterface_h__ 
#define __cfd_MyFuncLibInterface_h__

#includeMyFuncWin32Header.h

#include ... //其他一些进口

类MyFuncLib_EXPORT MyFuncLibInterface {

public:

MyFuncLibInterface();
〜MyFuncLibInterface();

void myFunc(std :: string param);

};

#endif

然后在控制台程序中有dllimport,有链接器中包含的DLL - >常规 - >附加库目录:

  #include< stdio.h> 
#include< stdlib.h>
#include< iostream>


__declspec(dllimport)void myFunc(std :: string param);


int main(int argc,const char * argv [])
{
std :: string inputPar =bla;
myFunc(inputPar); //这行生成链接器错误
}

我不知道发生了什么这里错了它必须是一个非常简单和根本的东西。

解决方案

您正在导出类成员函数 void MyFuncLibInterface :: myFunc(std :: string param ); 但尝试导入一个免费的函数 void myFunc(std :: string param);



确保您在DLL项目中 #define MyFuncLib_EXPORTS 。确保您在 #includeMyFuncLibInterface.h在控制台应用程序中没有定义 MyFuncLib_EXPORTS



DLL项目将显示:

  class __declspec(dllexport) MyFuncLibInterface {
...
}:

控制台项目将会看到:

  class __declspec(dllimport)MyFuncLibInterface {
...
}:

这允许您的控制台项目使用dll中的类。



编辑:回应评论

  #ifndef FooH 
#define FooH

#ifdef BUILDING_THE_DLL
#define EXPORTED __declspec(dllexport)
#else
#define EXPORTED __declspec(dllimport)
#endif

class EXPORTED Foo {
public:
void bar();
};


#endif

在实际实现 Foo :: bar() BUILDING_THE_DLL 必须定义。在尝试使用 Foo 的项目中, BUILDING_THE_DLL 应该被定义。 项目必须 #includeFoo.h,但只有DLL项目应包含Foo.cpp / code>



当您构建DLL时,Foo类及其所有成员将被标记为从此DLL导出。当您构建任何其他项目时,Foo类及其所有成员都将标记为从DLL导入


I can't quite figure out where there is a mistake. I am creating a DLL and then using it in a C++ console program (Windows 7, VS2008). But I get LNK2019 unresolved external symbol when trying to use the DLL functions.

First the export:

#ifndef __MyFuncWin32Header_h
#define __MyFuncWin32Header_h

#ifdef MyFuncLib_EXPORTS
#  define MyFuncLib_EXPORT __declspec(dllexport)
# else
#  define MyFuncLib_EXPORT __declspec(dllimport)
# endif  

#endif

This is one header file I then use in:

#ifndef __cfd_MyFuncLibInterface_h__
#define __cfd_MyFuncLibInterface_h__

#include "MyFuncWin32Header.h"

#include ... //some other imports here

class  MyFuncLib_EXPORT MyFuncLibInterface {

public:

MyFuncLibInterface();
~MyFuncLibInterface();

void myFunc(std::string param);

};

#endif

Then there is the dllimport in the console program, which has the DLL included in the Linker->General->Additional Library Directories:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>


__declspec( dllimport ) void myFunc(std::string param);


int main(int argc, const char* argv[])
{
    std::string inputPar = "bla";
    myFunc(inputPar); //this line produces the linker error
}

I can't figure out what's going wrong here; it must be something really simple and fundamental.

解决方案

You're exporting a class member function void MyFuncLibInterface::myFunc(std::string param); but trying to import a free function void myFunc(std::string param);

Make sure you #define MyFuncLib_EXPORTS in the DLL project. Make sure you #include "MyFuncLibInterface.h" in the console app without defining MyFuncLib_EXPORTS.

The DLL project will see:

class  __declspec(dllexport) MyFuncLibInterface {
...
}:

And the console project will see:

class  __declspec(dllimport) MyFuncLibInterface {
...
}:

This allows your console project to use the class from the dll.

EDIT: In response to comment

#ifndef FooH
#define FooH

#ifdef BUILDING_THE_DLL
#define EXPORTED __declspec(dllexport)
#else
#define EXPORTED __declspec(dllimport)
#endif

class EXPORTED Foo {
public:
  void bar();
};


#endif

In the project which actually implements Foo::bar() BUILDING_THE_DLL must be defined. In the project which tries to use Foo, BUILDING_THE_DLL should not be defined. Both projects must #include "Foo.h", but only the DLL project should contain "Foo.cpp"

When you then build the DLL, the class Foo and all its members are marked as "exported from this DLL". When you build any other project, the class Foo and all its members are marked as "imported from a DLL"

这篇关于c ++导出和使用dll函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆