函数已在.obj中定义 [英] function already defined in .obj

查看:1659
本文介绍了函数已在.obj中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的理解,这个错误是由于没有正确使用头守卫,当你有多个文件,包括同一个文件。在我的情况下,这是导致错误的包含树:

From what I understand, this error is caused by not properly using header guards when you have multiple files including the same file. In my case, this is the include tree that's causing the error:

文件A包含Z,其中包含我需要的功能。文件B包括A,文件C包括A.

没有任何 #pragma once

blahblah.obj: error LNK2005: class some::namespace::ObjectType Object already 
    defined in dialogDlg.obj

我尝试使用 #pragma once 在文件Z,但是没有工作。我也试过#pragma一次在文件A,这也没有工作。最后,我试过在A和Z,也没有工作。

I tried using #pragma once on file Z, but that didn't work. I also tried #pragma once on file A, which also didn't work. Finally I tried it on both A and Z, also didn't work.

推荐答案

定义头文件中的变量。如果该头文件包含在多个源文件中,它将在每个源文件中定义,从而给您错误。

It seems you are trying to define a variable in a header file. If that header file is included in several source file it will be define in each source file thereby giving you the error.

而不是声明 extern ,然后在其中一个源文件中定义。

Instead declare it as extern and then define in one of your source files.

所以在头文件中:

extern ObjectType Object;

在源文件中:

ObjectType Object;

这篇关于函数已在.obj中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆