LNK2019关于dll项目的解决方案

LNK2019 on a solution with a dll project

本文关键字:解决方案 项目 dll 关于 LNK2019      更新时间:2023-10-16

我正试图创建一个解决方案,其中一个项目是.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:UsersusteinfeldDesktopPrivateStudentsYanaConsoleApplication1ConsoleApplication1ConsoleApplication1中引用。obj ConsoleApplication1

这个函数被定义为一个自由函数

void printHello()

它属于hello_world类,所以你应该将它的作用域设置为

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