本文介绍了如何在头文件中声明要在两个 .cpp 中使用的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..我的目标模块是一个由 X.cpp
和 Y.cpp
构建的可执行文件,这两个文件都需要一个共同的 .h
文件:
My target module is an executable to be built from X.cpp
and Y.cpp
, both these two files need a common .h
file:
extern HANDLE hPipe;
extern IMediaSample *pSave = NULL;
但是当我构建模块时,我收到一条错误消息:
But when I build the module, I got an error saying :
Y.obj : error LNK2005: "struct IMediaSample * pSave" (?pSave@@3PAUIMediaSample@@A) already defined in X.obj
如何解决这个问题?
推荐答案
extern IMediaSample *pSave = NULL;
这不仅仅是一个声明.这会将 pSave
定义为 NULL
.由于.cpp
都包含.h
,这个变量会在2个翻译单元中定义,导致冲突.
This is not just a declaration. This will define pSave
to NULL
. Since both .cpp
include the .h
, this variable will be defined in 2 translation units, which causes the conflict.
你应该把它改写为
extern IMediaSample *pSave;
在 .h
中,然后在 .cpp
s 之一中添加 IMediaSample *pSave = NULL;
.
in the .h
, then add IMediaSample *pSave = NULL;
in exactly one of the .cpp
s.
这篇关于如何在头文件中声明要在两个 .cpp 中使用的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论