-1

I am working in a MFC project and i have a header and cpp file of my library, i include header file in different files but include cpp in only "main" file. i have a global initialization of a char array in the header file because all the files need it. But when i compile it, understandably, it tells me that i have initialized the char array twice.

I Tried using pragma once and the defining trick used in header files

#ifndef MY_HEADER_FILE
#define MY_HEADER_FILE

//Initialization here

#endif

But the linker still gives error, how do we solve this issue?

  • there is more than one link error, please tell us which one you get and please take care that what you post is sufficient to reproduce the problem ([mcve]) – 463035818_is_not_an_ai Jun 17 '20 at 12:21
  • do you include a cpp in another source? Don't do that – 463035818_is_not_an_ai Jun 17 '20 at 12:22
  • 1
    You solve the issue by taking a [tour] of stackoverflow.com, reading the [help], learning [ask] questions here, and then asking a question that includes a [mre]. This will result in your question having sufficient information for others to help you. Your question does not, and the most that you can hope for, here, are guesses as to what your problem might be. – Sam Varshavchik Jun 17 '20 at 12:24
  • I'm quite sure there was a canonical duplicate to creating global objects in headers, but I can't find it. This one might be okay: [Correct way to declare/define custom cout-like object](https://stackoverflow.com/questions/17774383/correct-way-to-declare-define-custom-cout-like-object) – Yksisarvinen Jun 17 '20 at 12:25
  • Anyway, you shouldn't define global objects in header files, this will break sooner or later. You should only declare the variable with external linkage in header file (`extern int myGlobalVar;`) and define it in one .cpp (`int myGlobalVar;`). And don't ever `include` cpp files,they are not meant to be used like that. – Yksisarvinen Jun 17 '20 at 12:27

1 Answers1

2

i include header file in different files but include cpp in only "main" file

If this means that you have something like

#include "MyFile.cpp" // <- Remove this

int main() { ... }

then this file will contain all of the definitions of "MyFile.cpp", thus 2 compilation units contain the same definitions.

You should never #include any .cpp files. Using include guards in the header file wont work, since both compilation units are seperate from each other. You can't use macros to control what's happening in another compilation unit.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • Thanks for the answer but i removed the cpp file but that was not the issue, the issue was redefination error when linking even when using pragma once – The Computer Genius Jun 17 '20 at 12:36
  • Then please make a [mre] and be add more information about those errors. – Lukas-T Jun 17 '20 at 12:37
  • ***the issue was redefination error when linking even when using pragma once*** That is an expected problem when including a `.cpp` file. However a second way to get this type of error is defining variables and functions in a header that is used in more than 1 cpp file. #pragma once or include guards don't help with that. – drescherjm Jun 17 '20 at 13:20