I use #pragma once often and it seems to work fine when dealing with headers and but for some reason, the following creates the linker error of multiple definitions:
#pragma once
int someVariable=5;
Shouldn’t the pragma prevent this too?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, in this case multiple definitions of someVariable will be created if this header file is included in multiple places. If B.h and C.h both include your head file, then two someVariable will be created.
Better way is to define variables in only one .cpp file and use
externin other places.