如何构建一个在C++中具有函数声明但没有定义的DLL?

huangapple go评论69阅读模式
英文:

How can I build a DLL that has a declaration but no definition of a function, in C++

问题

我想在一个DLL中声明一个函数,然后在客户项目中定义该函数,以便DLL可以调用客户端中实现的这个函数。

我开始关注一位名为"The Cherno"的YouTuber的游戏引擎教程系列,在其中的第一个视频入口点 中,我创建了一个名为Entrypoint.h的文件,内容如下:

extern Hazel::Application* Hazel::CreateApplication();

int main(int argc, char** argv)
{
    Hazel::Application* app = Hazel::CreateApplication();
    app->Run();
    delete app;
}

这个文件属于应该编译成DLL的引擎项目。

然后,在同一个解决方案下的不同项目中,我有CreateApplication函数的定义:

Hazel::Application* Hazel::CreateApplication()
{
    return new Sandbox();
}

当我尝试构建这些项目时,我会遇到一个"未解析的外部符号"链接错误。在花了很长时间的研究后,我在StackOverflow上找到了一些答案,说这是不可能的,就像这个一样。但如果这不可能,那我就不明白为什么视频中可以,而我的代码不行。

英文:

I want to declare a function in a DLL and then define it in a client project so that the dll can call this function implemented in the client.

I started following a game engine tutorial series from the youtuber "The Cherno" and in one of the first episodes called Entry Point, I created a file called Entrypoint.h with the content:

extern Hazel::Application* Hazel::CreateApplication();

int main(int argc, char** argv)
{
	Hazel::Application* app = Hazel::CreateApplication();
	app->Run();
	delete app;
}

This file belongs to the engine project which should be compiled into a DLL.

Then in a different project under the same solution I have the definition of the CreateApplication function.

Hazel::Application* Hazel::CreateApplication()
{
	return new Sandbox();
}

When I attempt to build these projects I get an unresolved external symbol linker error and after spending a long time researching I've found some answers on stackoverflow saying that this is not possible, like this, but if it isn't supposed to be possible then I don't understand why it works in the video and doesn't work with my code.

答案1

得分: 0

我终于找到了为什么它不起作用。

当我创建引擎中的一个头文件时,我不小心将其添加为.cpp文件,然后将扩展名更改为.h,显然这在某种程度上破坏了程序。我通过删除头文件并重新创建它来修复了错误。

只有通过重新制作项目并按照视频的步骤进行比较两个项目的设置文件,才能找到两者之间的区别。

英文:

I finally found out why it wasn't working.

When I created one of the header files in the engine I accidentaly added it as a .cpp file and then changed the extension to .h, apparently this broke the program somehow. I fixed the error by deleting the header file and creating it again.

Only managed to find this out by remaking the project following the video and then comparing the settings files of both projects to find what was different between the two.

huangapple
  • 本文由 发表于 2023年2月24日 03:43:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定