linker error

closed account (SECMoG1T)
I have a large project that I am working on and when compiling I got a weird linker error and I can't pin the cause, I have managed to reproduce the error in a smaller example and would be really grateful if anyone can help pin the cause of the error.

basically, the project has a few files similar to:

test1.h
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
  
#ifndef _TEST1_H_
#define _TEST1_H_

struct bar
{
    static int op_counter;

    template<typename T>
    T add(T x, T y);
    template<typename T>
    T minus(T x,T y);
};
int bar::op_counter(0);

template<typename T>
T bar::add(T x,T y)
{
    op_counter++;
    return x+y;
}

template<typename T>
T bar::minus(T x,T y)
{
    op_counter++;
    return x-y;
}

#endif // _TEST1_H_



test2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  
#ifndef _TEST2_H_
#define _TEST2_H_

#include "test1.h" ///it seems this is causing my bug

struct foo
{
    int x;
    int y;
    int operator()();
};

//use code from test1.h ...

#endif // _TEST2_H_


test2.cpp <here: if i remove this file a define everything in test2.h the error disappers>
1
2
3
4
5
6
  #include "test2.h" 

int foo::operator()()
{return x+y;}

//some more definitions 


main.h
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "test2.h"

int main()
{
    bar x;
    foo y{34,78};
    std::cout<<y()<<"\n";
    std::cout<<x.add(43,89)<<"\n";
    return 0;
}



even though this example does nothing it reproduces my error perfectly.

any insight is greatly appreciated.
Last edited on
Define the static member variable in a .cpp file.
(It has external linkage; so it should not be defined in the header file.)

say, in test1.cpp
1
2
3
#include "test1.h"

int bar::op_counter = 0 ;
It would really help if you posted the ACTUAL error message, which would probably be enough of a clue to say what's going on.

1
2
3
4
$ g++ -std=c++11 main.cpp test2.cpp 
/tmp/ccSjAMXo.o:(.bss+0x0): multiple definition of `bar::op_counter'
/tmp/ccfuTVgl.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status 


Here, "multiple definition" is the key point of observation.

You get this when you put definitions inside header files, because you end up with multiple definitions (hence the error message) whenever you include such a header file.

The answer is to put the bar::op_counter thing in exactly ONE .cpp file.
closed account (SECMoG1T)
@JLBorges and @salem c thank you very much, it's solved now.

I can't believe the static member caused me all that headache wow, what am I missing here.

this was the error i was getting:
||error: ld returned 1 exit status|
Last edited on
this was the error i was getting:
This is just the last most irrelevant part of the error.

So is your problem solved now?
closed account (SECMoG1T)
@coder777 that was in response to @salem c request for the actual error:
It would really help if you posted the ACTUAL error message
I somehow forgot to provide it with the code and yes the one and only error I got was just that statement
error: ld returned 1 exit status
and it is the reason I came here for help, couldn't figure it out.

https://ibb.co/wzmQb6N
Last edited on
Topic archived. No new replies allowed.