LNK2019在带有dll项目的解决方案上

时间:2014-07-01 12:42:22

标签: c++ dll linker-errors

我正在尝试创建一个解决方案,其中一个项目是.exe,另一个项目是简单的dll。我想学的是如何在两个项目之间建立联系。我已经搜索了堆栈溢出,并找到了我所遵循的非常好的答案,例如声明正确的标题浴:

  

Properties - > Configuration Properties - > C/C++ - > General - > Additional Include Directories

然后设置.lib:

  

Properties - > Configuration Properties - > Linker - > Input - > Additional Dependencies

我还使用宏来生成.lib文件。这是我的简化代码:

.exe: CPP:

#include "stdafx.h"
#include "../ConsoleApplication2/HelloWorld.h"

int _tmain(int argc, _TCHAR* argv[])
{
    hello_world hw;
    hw.printHello();
    getchar();
    return 0;
}

dll: 标题:

#pragma once
#ifdef is_hello_world_dll
#define hello_world_exp  __declspec(dllexport)
#else
#define hello_world_exp  __declspec(dllimport)
#endif


class hello_world_exp hello_world
{
public:
    hello_world();
    ~hello_world();
    void printHello();
};

CPP:

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

hello_world::hello_world()
{
}

hello_world::~hello_world()
{
}

void printHello()
{
    std::cout << "Hello World" << std::endl;
}

注意:当我不调用hw.printHello();时解决方案编译正常但是当我调用它时,链接器生成:

  

错误1错误LNK2019:未解析的外部符号“__declspec(dllimport)public:void __thiscall hello_world :: printHello(void)”(__ imp_?printHello @ hello_world @@ QAEXXZ)在函数_wmain中引用C:\ Users \ usteinfeld \ Desktop \ Private \ Students \ Yana \ ConsoleApplication1 \ ConsoleApplication1 \ ConsoleApplication1.obj ConsoleApplication1

1 个答案:

答案 0 :(得分:3)

此功能根据您编写的方式定义为自由功能

void printHello()

它属于类hello_world,因此您应该将其作为范围

void hello_world::printHello()
{
    std::cout << "Hello World" << std::endl;
}