3

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

How to solve this issue?

2 Answers 2

14
extern IMediaSample *pSave = NULL;

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.

You should just rewrite it as

extern IMediaSample *pSave;

in the .h, then add IMediaSample *pSave = NULL; in exactly one of the .cpps.

Sign up to request clarification or add additional context in comments.

Comments

0

try using ifndef statement. define a variable unique to each header file you create then while including use something like:

#ifndef commonh
include common.h
#endif 

1 Comment

Include guards protect you from including a header multiply in the same source file, not in different ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.