在Visual studio-LNK2001中的同一解决方案中包含来自单独项目的文件

Including files from a seperate project in the same solution in Visual studio - LNK2001?

本文关键字:单独 文件 包含 项目 Visual studio-LNK2001 解决方案      更新时间:2023-10-16

我有一个名为fun.sln的解决方案,该解决方案的项目名为fun.vcxproj.

我创建了一大堆可以使用的名称空间。

我做了另一个项目,名为no_more_fun.vcxproj.

我将fun.vcxproj的includes目录添加到no_more_fun.vcxproj.的配置中

我将此添加到no_more_fun.cpp

#include "candy.h"
void main(void)
{
candy::get();
return;
}

candy.h位于fun.vcxproj(已添加到配置中)的默认目录中

但我明白。。。

LNK2001 unresolved external symbol "int __cdecl candy::get(unsigned long)" (?get@candy@@YAHK@Z) .....

Visual Studio在编译之前没有显示任何错误。

"candy"命名空间在"fun"项目中运行良好,因此idn。。。

有没有一个指南或其他东西可以让我了解如何在一个解决方案中的不同项目之间有效地共享代码?

这是一个链接器错误。您在编译时没有得到任何错误,因为编译器在candy.h头中找到了candy::get()方法,但链接器没有找到实现(我想它在candy.cpp文件中)。只需将candy.cpp也添加到no_more_fun.vcxproj.

ps。我没有观察到,但在错误消息中,您可以看到函数正在等待一个参数。这样称呼它:

unsigned long foo = 0;
candy::get(foo);

这听起来很愚蠢,但。。。我只是把文件拖到visualstudio中,这样no.morefun项目的"目录"中也有"文件"。

哇。。。我不应该那样做。。。我错了吗?(讽刺)。