int already defined in .obj vs 2017 LNK2005 ERROR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
a.h 
pink();
int a;
string doe;
int pri;

.............


a.cpp
#include "a.h"
pink()
{
 a = 20;
doe = "dodd";
pri =24;
}


..........

main.cpp
#include "a.h"
int main()
{
 pink();
}








This is just the basic thinks inside the header and the cpp yet am get this error, am help pls
Last edited on
Your .h file should be
1
2
3
4
5
a.h 
pink();
extern int a;
extern string doe;
extern int pri;


Exactly ONE of your .cpp files needs these globals declared.
1
2
3
int a;
string doe;
int pri;
@salem have done just that but am now getting an unresolved external symbol error
What symbol? And what is the code that is now generating this error?

We're not telepaths. We can't read your mind. Show us a minimal, compilable codeset that exhibits the problem, and tell us exactly what the error(s) your getting are.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
a.h 
int pink();
extern int a;
extern string doe;
extern int pri;

.............


a.cpp
#include "a.h"
inline int pink()
{
 a = 20;
doe = "dodd";
pri =24;
}


..........

main.cpp
#include "a.h"
int main()
{
 pink();
}
salem c wrote:

Exactly ONE of your .cpp files needs these globals declared.
1
2
3
int a;
string doe;
int pri;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
a.h 
int pink();
extern int a;
extern string doe;
extern int pri;

.............


a.cpp
#include "a.h"
inline int pink()
{
 int a;
 string doe;
 int pri
 a = 20;
doe = "dodd";
pri =24;
}


..........

main.cpp
#include "a.h"
int main()
{
 pink();
}


This worked thanks guys
> This worked thanks guys
Only because you redeclared the variables inside your pink() function.

Try printing those variables inside main, after you called pink().
hello guys i cant really understand this header files stuff, everything works though but i have to declare all variable already inside the header file inside any cpp i want to use, why is it like this, and what is now the use of the header file.. thanks
everything works though but i have to declare all variable already inside the header file inside any cpp i want to use

That doesn't make any sense. Clearly you're doing something wrong, but until you show us a minimal, compilable codeset that demonstrates your problem, it's had for us to guess what that might be.

what is now the use of the header file


The header file literally includes its contents in every source file that #includes it. What happens is the preprocesser creates a new source file, constructed from the contents of the original source file, and the contents of the header files that it #includes.

So, the use of a header file is to contain code that, otherwise, would need to be repeated in several different files. Usually, they are organised so that one header file contains things that are related together, for ease if use. For example, the standard header file <string> contains all declarations and definitions that are needed to use std::string.
Last edited on
This is what you're trying to achieve, if you didn't have a header file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// This is a.cpp
int pink();
extern int a;
extern string doe;
extern int pri;

int pink()
{
 a = 20;
 doe = "dodd";
 pri =24;
}

// This is main.cpp
int pink();
extern int a;
extern string doe;
extern int pri;

// Now make those globals exist
int a;
string doe;
int pri;

int main()
{
 pink();
}

The first FOUR lines are common declarations in both files, so those are what you would put in a.h.

Leaving you with something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// This is a.h
int pink();
extern int a;
extern string doe;
extern int pri;

// This is a.cpp
#include "a.h"
int pink()
{
 a = 20;
 doe = "dodd";
 pri =24;
}

// This is main.cpp
#include "a.h"

// Now make those globals exist
int a;
string doe;
int pri;

int main()
{
 pink();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <opencv2/highgui.hpp>
#include <string>
#include "opencv2/opencv.hpp"
#include <opencv2/highgui.hpp>
using namespace std;
using namespace cv;

// This is the headerfile

       extern int Gaussianblur() ;
	 
		 
		 extern string imagedir;
		 extern Mat img;
		 extern  int imgh;
		 extern int imgw;
		 extern Mat des;
		 extern int Ksize1;
		 extern int Ksize2;
		 extern char Option;
		 extern int x;
		 extern int y;
		 extern int imgheight;
		 extern int imgwidth;
		 extern string ImageO;
		 extern string ImageB;


 // -----------------------------------------------------    
      gau.cpp file

#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include "Gaussianblur.h"
#include "Help.h"

using namespace std;
using namespace cv;

	  inline int Gaussianblur()
{   
		 
		  Mat img;
		  Mat des;
		  int imgh;
		  int imgw;
		  int Ksize1;
		  int Ksize2;
		  char Option;
		  int x;
		  int y;
		  int imgheight;
		  int imgwidth;
		  string imagedir;
		  string ImageO;
		  string ImageB;
	
	cout << "Enter your Image directory " << endl;
	getline(cin, imagedir);
	cout << endl;
	img = imread(imagedir);
	if (img.empty())
	{
cout << "Error!!!, Invalid image or wrong Directory, check your input and try again, see you later" << endl;
		system("pause");
		return -1;
	}
     imgh = img.cols;
	 imgw = img.rows;

//------------------------------------------------------------
    main.cpp

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <string>
#include "Gaussianblur.h"
#include "Help.h"
#include "Blur.h"

using namespace std;
using namespace cv;

int main()
{
	
	string Option;
	Help();
	cin >> Option; cin.ignore();
	if (Option == '1')
	{
		Normalblur();

	}
	else if(Option =='2')
	{
		Gaussianblur();
	}
	cout << endl;

	system("pause");
	return 0;
}



if i want to use any of those variable i have to declare them in the cpp file before i can use it, i first learnt separating my code in codeblock but visual studio seems to be different
Last edited on
This is what salem c wrote earlier:
salem c wrote:

Exactly ONE of your .cpp files needs these globals declared.
1
2
3
int a;
string doe;
int pri;

emphasis mine.

Do you know the difference between a global and a local variable?

If you have extern int a; in your header, you need to declare int a; on global scope in one of the .cpp files.

Also, you should really be avoiding the use of global variables here in the first place. See how much trouble they're already causing? Wrap the variables you need in class or struct, and give input parameters to your blur functions (or make them class members).

I think your function GaussianBlur is trying to do too many things at once. I would pass it a pre-existing image, and the parameters it needs to do the blur. Do the actual file loading/saving in other functions.

But, for now, just do what salem c said: They need to global variables, not local ones.

_____________________

PS: Last thoughts: There's no point in declaring the function as extern, just remove the inline. And you should be using proper header guards.
https://www.learncpp.com/cpp-tutorial/1-10a-header-guards/
Last edited on
Topic archived. No new replies allowed.