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



然后将.lib设置为:



我也使用宏来生成该.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:
header :
#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();时,解决方案可以很好地编译,但是当我调用它时,链接器会生成:

最佳答案

该函数根据您的编写方式定义为自由函数

void printHello()

它属于hello_world类,因此应将其范围定为
void hello_world::printHello()
{
    std::cout << "Hello World" << std::endl;
}

关于c++ - 带有DLL项目的解决方案上的LNK2019,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24510606/

10-11 22:30
查看更多