#pragma once is not working in visual studio - c++

I am using visual studio and #pragma once is not working. This is the following error i get:
1>namespaces.obj : error LNK2005: "int a" (?a##3HA) already defined in another.obj
1>fatal error LNK1169: one or more multiply defined symbols found
These are the files
main.cpp
#include <iostream>
#include "Header.h"
int main()
{
std::cout << a;
}
another.cpp
#include <iostream>
#include "Header.h"
void hi() {
std::cout << a;
}
header.h
#pragma once
int a = 5;

#pragma once does not mean "include me in only one source file", it means "only include me once in a single source file.

You should not define a global variable in a header file for exactly this reason. #pragma once does not protect you from this kind of error. In fact, nothing can protect you from this kind of error, because each source file is compiled separately. What happens in one compilation unit has no effect on what happens in another compilation unit.
The solution is to move int a = 5; to main.cpp, and change header.h to say extern int a;.

Related

C++ Conditional compilation directives: multiple files

I have 2 files,main.cpp and head.h
//main.cpp
#define DEBUG2019 1
#include 'head.h'
int main{
A A1;
return 0;
}
//head.h
class A{
#ifdef DEBUG2019
int p;
#endif
int q;
};
Look, I have defined DEBUG2019 in main.cpp. But in my visual studio 2019, the int p is still greyed out in head.h. Why is that? Why head.h does not know that DEBUG2019 has been defined? You may suggest me to define DEBUG2019 in the header file directly. But I have to define it in main.cpp.
As far as I'm concerned, you should use #include "head.h" instead of #include 'head.h'
Here is the code of main.cpp:
#include <iostream>
#define DEBUG2019 1
#include "head.h"
int main()
{
A A1;
return 0;
}
To define a macro in project level, you can define it as a preprocessor directive: right click and select properties then in C/C++ ->Preprocessor-> Preprocessor Definitions define your macro.
or
Define the macro in a common header file and use it in the related other files (you already did this, so you can ignore the grey area).

Linker error: _main already defined in *.obj

The following code structure:
ArrayStack.h
#ifndef ARRAY_STACK_H
#define ARRAY_STACK_H
#include "Array.h"
// class ArrayStack
#endif
ArrayStack.cpp
#include "ArrayStack.h"
// ArrayStack's methods
Array.h
#ifndef ARRAY_HEADER
#define ARRAY_HEADER
#include <iostream>
// class Array
#endif
Array.cpp
#include "Array.h"
// Array's methods
main.cpp
#include "ArrayStack.h"
int main() {
return 0;
}
generates these errors:
LNK1169 one or more multiply defined symbols found
LNK2005 _main already defined in Array.obj
What's the problem here? Please do note that Array.cpp did have int main() defined in itself when it was included in the project for the first time, but no longer has it (neither does the ArrayStack.cpp). Also, the code compiles just fine when the int main() in main.cpp is omitted...
The error message means that in all the compiled code, the *.obj files, the linker finds more than one main() function. One is obviously in main.cpp.
The first solution that comes to mind, as mentioned in comments, is to (enforce) re-compile by somehow deleting the *.obj files.
When this doesn't change anything try to rebuild your solution separately from scratch. Start with main.cpp without the include. Then successively add files where you are confident that you won't get errors. Maybe you have to comment out some lines in some cases to make compilation possible.

How to use global variables in multiple .cpp files?

I have this simple program which tries to print my global variable in a separate file. I'm using the Visual Studio 2013 professional IDE.
print.h
#ifndef PRINT_H_
#define PRINT_H_
void Print();
#endif
print.cpp
#include "print.h"
#include <iostream>
void Print()
{
std::cout << g_x << '\n';
}
source.cpp
#include <iostream>
#include <limits>
#include "print.h"
extern int g_x = 5;
int main()
{
Print();
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
return 0;
}
I get a compiler error error C2065: 'g_x' : undeclared identifier.
I've searched through this forum and was unable to find anyone else having my problem. I've tried re-declaring my global variable in the separate .cpp file with no success. As you can see, I've included the necessary header guards and assigned my global variable the extern keyword. This is my first time testing global variables in multiple files. Obviously I'm missing something simple. What do I need to change or add to make my program work?
EDIT: I found this topic useful in understanding the difference between extern and the definition of a global variable.
The compiler is compiling print.cpp. It knows nothing about source.cpp while it is compiling print.cpp. Therefore that g_x that you placed in source.cpp does you absolutely no good when print.cpp is being compiled, that's why you get the error.
What you probably want to do is
1) place extern int g_x; inside of print.h. Then the compiler will see g_x when compiling print.cpp.
2) in source.cpp, remove the extern from the declaration of g_x:
int g_x = 5;
Move your global declaration to a common header, like common.h:
#ifndef COMMON_H_
#define COMMON_H_
extern int g_x; //tells the compiler that g_x exists somewhere
#endif
print.cpp:
#include <iostream>
#include "print.h"
#include "common.h"
void Print()
{
std::cout << g_x << '\n';
}
source.cpp:
#include <iostream>
#include <limits>
#include "print.h"
#include "common.h"
int g_x;
int main()
{
g_x = 5; //initialize global var
Print();
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
return 0;
}
In other .cpp files, you can access g_x including the common.h header.
extern int g_x;
belongs to .h, and you need to add
int g_x =5;
to some of .cpp.

How do I use the same object across two classes?

I have a total of three classes...
1. Source.cpp (Where the main function is.)
2. Variables.h (Where I declared all my variables in, Variables.cpp is pretty much irrelevant)
3. Functions.cpp & .h (Where I make the functions to run in the main function in Source.cpp)
In main i have this
#include <iostream>
#include <cstdlib>
#include "Variables.h"
#include "Functions.h"
#include <ctime>
using namespace std;
Variables vari;
Functions func;
int main(){
cout << "\n\n>>> ";
cin >> vari.answer;
func.choiceChecker();
}
In Functions.cpp I have this
Variables vari;
void Functions::choiceChecker(){
if (vari.answer == 1){
scenario1();
}
else{
cout << "Failed";
}
}
I always get the output failed, instead of running the scenario1 function. I also get two errors.
1.Error 1 error LNK2005: "class Variables vari" (?vari##3VVariables##A) already defined in Source.obj C:\Users...\Desktop\Projects\ConsoleApplication1\ConsoleApplication1\Functions.obj ConsoleApplication1
2.Error 2 error LNK1169: one or more multiply defined symbols found C:\Users...\Desktop\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe ConsoleApplication1
I've tried using a new object in Functions.cpp, i get no errors but it doesn't get the same value from
cin >> vari.answer;
To use one global variable, you have to declare it once, then define it once (WhozCraig linked this question with its explanation).
The following should work in your situation:
Add extern Variables vari; in Variables.h (below the declaration of the Variables class or struct).
At the bottom of Variables.cpp add Variables vari;
In Functions.h you add #include Variables.h
In Source.cpp you remove #include Variables.h (because Functions.h already includes this)
Now you should be able to use vari in both Functions.cpp and Source.cpp.

Order of including the header file

I am getting linking error in the following program.
//FILE: CDummyMessage.h
#idndef DUMMY_FILE
#define DUMMY_FILE
#include "stdafx.h"
class CDummyMessage(){
static int objCount1;
std::string;
};
#endif //DUMMY_FILE
// CDummyMessage.cpp
all the necessary definition
//main cpp file: SmartPointerExample.cpp
#include "stdafx.h"
#include "CDummyMessage.h"
int main(){
CDummyMessage* OBJ1= new CDummyMessage();
}
I am getting this linking error:-
Error 1 error LNK2005: "private: static int dummy::CDummyMessage::ObjCount1" (?ObjCount1#CDummyMessage#dummy##0HA) already defined in DummyMessage.obj C:\Users\Veil\Documents\Visual Studio 2012\Projects\SmartPointerExample\SmartPointerExample\SmartPointerExample.obj
I am not able to figure out the reason of this linking error. I have made use of #ifndef directive that prevents duplicate inclusion of header file.
I think you have another file called CDummyMessage.cpp in your project ? and Visual studio Compiled that one too.