c++ exporting and using dll function

2019-01-19 08:31发布

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.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-19 09:09

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"

查看更多
\"骚年 ilove
3楼-- · 2019-01-19 09:15

You need to import the class not a function. After that, you can call the class member.

class  __declspec( dllimport ) MyFuncLibInterface {

public:

MyFuncLibInterface();
~MyFuncLibInterface();

void myFunc(std::string param);

};

int main(int argc, const char* argv[])
{
std::string inputPar = "bla";
MyFuncLibInterface intf;
intf.myFunc(inputPar); //this line produces the linker error
}
查看更多
登录 后发表回答