在多个文件中使用函数时出现 DLL 库错误

DLL library errors when using functions in multiple files

本文关键字:DLL 错误 函数 文件      更新时间:2023-10-16

我正在制作库,以便在多个游戏中使用,这样我就不必复制代码。在此示例中,我的主程序是main.cpp,另外两个文件是我的库。一切都正确链接。

当我在Common Functions Library.h中的所有函数都在前面static时,我得到错误:static function 'std::string common::joinAll(std::vector<std::string,std::allocator<_Ty>>)' declared but not defined (Error C2129)来自main.cpp(即使函数在Common Functions Library.h/.cpp中),它说行号比整个程序中的多一个,这很奇怪。

因此,为了解决此问题,我发现网上有人说我需要用inline替换static,所以我再次尝试并得到此错误:cannot open file 'Common Functions Library.lib' (Error LNK1104)

然后我尝试将所有函数设置回static,然后在main.cpp中注释掉#include "Cubes Library.h",这不再给我其他错误,而是与立方体库相关的东西(显然)。但是,这在第 65 行停止,之后导致错误的函数说它们没有定义。我不知道出了什么问题,但感谢您的任何帮助:)

Code:

主.cpp

#include "Common Functions Library.h"
#include "Cubes Library.h"
// Using functions from `Common Functions Library`

公共函数库.cpp(DLL 的一部分)

#include "Common Functions Library.h"

namespace common
{
// Functions
}

多维数据集库.cpp(DLL 的一部分)

#include "Cubes Library.h"
#include "Common Functions Library.h"

如果您将inline放在所有函数的前面并在标头中定义,则不需要.cpp文件,因此不需要.lib

我也在你的Common Functions Library.h中看到namespace common.因此,不要忘记在main.cpp中使用类之前,将using namespace common;放在您的main.cpp或前缀common::中。