Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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. 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.

What I have tried:

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

C++
#ifndef MY_HEADER_FILE
#define MY_HEADER_FILE
//Initialization here

#endif


But the linker still gives error, how do we solve this issue?
Posted
Updated 17-Jun-20 6:52am
v2

Don't put initialisation in the header, put it in the implementation (i.e. the .cpp file). And using the #ifndef or #pragma statements merely prevent the header file from being included more than once in each compilation unit (again the .cpp file). It does not make it unique in the entire project. The above is something of a guess since you have not shown any of the relevant code.
 
Share this answer
 
v2
Comments
Harasees_Singh 17-Jun-20 9:08am    
Thanks for the answer, found the solution
My recommendation is the usage of the modern #pragma once compiler directive instead of macros which also are a common source of strange bugs.

The latest development are modules but I am not using the actual Visual Studio and its toolchain.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900