I'm using Visual Studio 2019. I create a new C++ console application. I have a file Main.cpp in my Source Files folder with the following code:
#include <iostream>
int i = 10;
int main()
{
std::cout << i << std::endl;
std::cin.get();
}
I create a second file in my Source Files folder called File2.cpp with only the following line:
int i = 2;
If I try to compile this code I get an error "one or more multiply defined symbols found". Why am I getting this error even though I have not explicitly done #include "File2.cpp"
in Main.cpp?
UPDATE: HOWEVER, if I remove File2.cpp and replace it instead with a file in my Header Files folder called File2.h (containing only one line: int i = 2
), and all other things being equal, it compiles fine. Similar to the previous example, the only difference is that instead of a second file being a .cpp Source file, it is now a .h Header file.