multiple definition, already defined here - c++

I have three simple files.
”banana.cc“
namespace ocr{
int a = 5;
}
"apple.cc"
#include "banana.cc"
namespace ocr{
int b = a;
}
"main.cc"
#include "apple.cc"
int main()
{
return 0;
}
/tmp/ccs6XmP2.o:(.data+0x0): multiple definition of `ocr::a'
/tmp/ccEkxDgJ.o:(.data+0x0): first defined here
/tmp/ccs6XmP2.o:(.bss+0x0): multiple definition of `ocr::b'
/tmp/ccEkxDgJ.o:(.bss+0x0): first defined here
/tmp/cco0dUCm.o:(.data+0x0): multiple definition of `ocr::a'
/tmp/ccEkxDgJ.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
after compiler insert all the #include, main.cc is like:
namespace ocr{
int a = 5;
}
namespace ocr{
int b = a;
}
int main()
{
return 0;
}
why this will cause redefinition?
Thank you.

Because you're compiling apple.cc and banana.cc and main.cc in your project.
So you're compiling this file:
namespace ocr{
int a = 5;
}
and this file:
namespace ocr{
int a = 5;
}
namespace ocr{
int b = a;
}
and this file:
namespace ocr{
int a = 5;
}
namespace ocr{
int b = a;
}
int main()
{
return 0;
}
Obviously ocr::a is defined in all three files, and ocr::b is defined in two of them.

Don't have enough reputation to comment but I just want to elaborate on this, just in case there is still confusion.
If you want to share some variables between files, create a header file and declare it there.
i.e.
// common.h
namespace ocr{ int a, b; }
// banana.cc
#include "common.h"
void initAppple(){
ocr::a = 4;
}
// apple.cc
#include "common.h
void initBanana(){
ocr::b = a;
}
// main.cc
#include "common.h"
int main(){ initApple(); initBanana(); }
And then when you compile main.cc, link it with apple.cc and banana.cc instead of "including" it.
g++ main.cc apple.cc banana.cc -o output
Note that you can't just declare and initialize separately in global scope, which is why you probably need to use a setter function such as the ones above (initApple() etc). Or use extern inside the header file and define it inside the source file.

Related

Why vscode shows me "undefined reference" when I call funciton around two cpp?

I'm new to C++, I tried an example in 《C++ Primer Plus》, review 9.6, which is composed of 2 .cpp, without a .h, the TERMINAL says
C:\Users\SPM\AppData\Local\Temp\ccRnfRBS.o:p1.cpp:(.text+0x80): undefined reference to `another()'
collect2.exe: error: ld returned 1 exit status
My question is, is this program correct? Can a .cpp call a function in another .cpp without a .h? Thank you for answering, this question make me pain for a few hours, I really don't know how to slove it.
//file1.cpp
#include <iostream>
using namespace std;
void other();
void another();
int x = 10;
int y;
int main()
{
cout<<x<<endl;
{
int x = 4;
cout<<x<<endl;
cout<<y<<endl;
}
other();
another();
return 0;
}
void other()
{
int y = 1;
cout<<"Other: "<<x<<", "<<y<<endl;
}
//file2.cpp
#include <iostream>
using namespace std;
extern int x;
namespace
{
int y = -4;
}
void another()
{
cout<<"another(): "<<x<<", "<<y<<endl;
}
Yes, there are no problem with your code.
For example, you can successfully build your program via g++ like this
g++ file1.cpp file2.cpp
So problem is in your build definition.

Why does "#define A" interfere with "namespace A{}"?

The following code can compile:
namespace A{
int i;
}
namespace B{
int i;
}
int main(){ return 0; }
But the following code cannot compile:
#define A
#define B
namespace A{
int i;
}
namespace B{
int i;
}
int main(){ return 0; }
The error info is
error: redefinition of 'int {anonymous}::i'
After I define A and B why do the names of namespaces become anonymous?
The used compiler: gcc-4.9.3.
In
#define A
#define B
namespace A{
int i;
}
namespace B{
int i;
}
You define A and B to be nothing. That means your code becomes
namespace {
int i;
}
namespace {
int i;
}
After the preprocessor runs. Since both namespaces become anonymous namespaces the compiler correctly complains that you are redeclaring i.
Remember that when you define something the preprocessor is going to do through your source code and replace all occurrences of that symbol with whatever you defined it to be. Had you done
#define A LONG_NAME_I_DO_NOT_WANT_TO_TYPE
#define B ANOTHER_LONG_NAME_THAT_I_ALSO_DO_NOT_WANT_TO_TYPE
namespace A{
int i;
}
namespace B{
int i;
}
Then the preprocessor would change the code to be
namespace LONG_NAME_I_DO_NOT_WANT_TO_TYPE{
int i;
}
namespace ANOTHER_LONG_NAME_THAT_I_ALSO_DO_NOT_WANT_TO_TYPE{
int i;
}
For more information on how the preprocessor works see: GCC - The C Preprocessor

How to use extern to link to a function in other cpp file?

Here is my main.cpp:
#include <iostream>
#include "function.cpp"
using namespace std;
extern int giveMain();
int main() {
int x = 4;
x = giveMain(x);
cout << x << endl;
}
And here is my function.cpp:
#include <iostream>
using namespace std;
int giveMain(int a) {
a = 3 + a;
return a;
}
But when I compile, it says that "Linker command failed". Can anyone helps me to solve this problem.
You declared the function int giveMain() in main.cpp but the function in function.cpp takes an int. Declare the correct function and it should work. Also extern is the default for functions so you don't need to include the keyword.
EDIT: Just noticed that you #include <function.cpp> in main.cpp. Never include .cpp files. The issue you were having was multiple definitions for int giveMain(int) because the contents of functions.cpp was being compiled twice.

Static variable link error, C++

Consider this code.
//header.h
int x;
//otherSource.cpp
#include "header.h"
//main.cpp
#include "header.h"
...
int main()
{
}
In this case compiler erred with the message. "fatal error LNK1169: one or more multiply defined symbols found"
but when I add static before x, it compiles without errors.
And here is the second case.
//header.h
class A
{
public:
void f(){}
static int a;
};
int A::a = 0;
/otherSource.cpp
#include "header.h"
//main.cpp
#include "header.h"
...
int main()
{
}
In this case compiler again erred with multiple declaration.
Can anybody explain me the behavior we static variables in classes and in global declarations?? Thanks in advance.
The issue with the static member variable is that you have the definition occur in the header file. If you #include the file in multiple source files, you have multiple definitions of the static member variable.
To fix this, the header file should consist only of this:
#ifndef HEADER_H
#define HEADER_H
// In the header file
class A
{
public:
void f(){}
static int a;
};
#endif
The definition of the static variable a should be in one and only one module. The obvious place for this is in your main.cpp.
#include "header.h"
int A::a = 0; // defined here
int main()
{
}
Declare x as extern in header.h to tell the compiler that x will be defined somewhere else:
extern int x;
Then define x once in the source file which you think is most fitting.
For example in otherSource.cpp:
int x = some_initial_value;

Error already defined

Hi i just created a sample class and using it in main but i am getting already defined error.
sample.h
#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;
int count = 10;
class sample
{
public:
sample();
int Get();
private:
int i;
};
#endif
sample.cpp
#include "sample.h"
sample::sample()
{
cout<<"hello two";
}
int sample::sample()
{
return 10;
}
main.cpp
#include <iostream>
#include "sample.h"
using namespace std;
int main(void)
{
int test = count;
return 0;
}
Link error:
main.obj : error LNK2005: "int count" (?count##3HA) already defined in sample.obj
if u see above class i am using #ifndef and #define, actually there things will declare data once thought we include in many places.could some one explain me clearly why its giving that link error.
Remember that #include literally means "add the contents of this file here".
Include guards only protects against a file's content being included more than once per file it's included in.
When the preprocessor has done its preprocessing, this is what your compiler sees:
sample.cpp
[iostream contents here...]
using namespace std;
int count = 10;
class sample
{
public:
sample();
int Get();
private:
int i;
};
sample::sample()
{
cout<<"hello two";
}
int sample::sample()
{
return 10;
}
main.cpp
[iostream contents here...]
using namespace std;
int count = 10;
class sample
{
public:
sample();
int Get();
private:
int i;
};
using namespace std;
int main(void)
{
int test = count;
return 0;
}
As you can see, there are two definitions of count, one in each file (formally, "translation unit").
The solution is to have a declaration of the variable in "sample.h"
extern int count;
and have the one and only definition in sample.cpp:
int count = 10;
(And you should not put using namespace std; in a header.)
To make a global variable like that visible everywhere:
blah.h
extern int count;
blah.cpp
int count(10);
Include guards only guard against including the same header file multiple times, not against multiple definitions. You should move your variable in a cpp file in order to not violate the ODR, or use internal linkage or declare it external and define it somewhere once. There are multiple solutions depending on the use of that variable.
Notice that I'm ignoring the fact that you probably meant int sample::Get() in the sample.cpp file
#include "sample.h"
sample::sample()
{
cout<<"hello two";
}
int sample::sample() // ??
{
return 10;
}
You have either to declare variable count as having internal linkage as for example
#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;
namespace
{
int count = 10;
}
//...
#endif
(the above internal declaration valid in C++ 2011) or
#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;
static int count = 10;
//...
#endif
Or to declare it as having external linkage but define it only once in some module. Fpr example
#ifndef __sample__
#define __sample__
#include<iostream>
using namespace std;
extern int count;
//...
#endif
#include "sample.h"
int count = 10;
sample::sample()
{
cout<<"hello two";
}
int sample::sample()
{
return 10;
}
Otherwise the compiler will issue an error that variable count is defined more than once that is that more than one compilation unit (in this case sample.cpp and main.cpp) contain the variable definition.