link error: undefined reference to reference

Please tell me were this error is coming from:
obj/l_Key_Layered.o:l_Key_Layered.cpp:(.text+0xa): undefined reference to `l_Key_Layered::refLayerManager'
obj/l_Key_Layered.o:l_Key_Layered.cpp:(.text+0x11): undefined reference to `l_LayerManager::getActiveLayer()'

I am stumped.

Here is the code:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "l_LayerManager.h"
#include "l_Key_Layered.h"

//instantiate layerManager
l_LayerManager layerManager(0);	

//initialize static variable
l_LayerManager& l_Key_Layered::refLayerManager = layerManager;

int main()
{
	
}


l_LayerManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef L_LAYERMANAGER_H
#define L_LAYERMANAGER_H

#include <inttypes.h>

class l_LayerManager
{
	private:
		uint8_t activeLayer;
	public:
		l_LayerManager(uint8_t al): activeLayer(al) {}
		uint8_t getActiveLayer();
};
#endif 


l_LayerManager.cpp
1
2
3
4
5
6
#include "l_LayerManager.h"

uint8_t l_LayerManager::getActiveLayer()
{
	return activeLayer;
}


l_Key_Layered.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef L_KEY_LAYERED_H
#define L_KEY_LAYERED_H

#include "l_LayerManager.h"

class l_Key_Layered
{
	private:
		static l_LayerManager& refLayerManager;
		uint8_t layerPressed;

	public:
		void press();
};
#endif 


l_Key_Layered.cpp
1
2
3
4
5
6
#include "l_Key_Layered.h"

void l_Key_Layered::press()
{
	layerPressed = refLayerManager.getActiveLayer();
}


Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
CFLAGS += -Wall -std=c++11
OBJ = obj/l_Key_Layered obj/l_LayerManager.o
OUT = a

#default target
all: $(OUT)

#link main
a: obj/main.o $(OBJ)
	g++ $^ -o $@

#compile
obj/main.o: main.cpp $(OBJ)
	$(CXX) $(CFLAGS) -c $< -o $@

obj/l_Key_Layered.o: l_Key_Layered.cpp l_Key_Layered.h
	$(CXX) $(CFLAGS) -c $< -o $@

obj/l_LayerManager.o: l_LayerManager.cpp l_LayerManager.h
	$(CXX) $(CFLAGS) -c $< -o $@

clean:
	rm obj/*.o */


output:
C:\Users\wolf\Documents\l_Key_Layered>make
g++ -Wall -std=c++11 -c l_Key_Layered.cpp -o obj/l_Key_Layered.o
cc   obj/l_Key_Layered.o   -o obj/l_Key_Layered
obj/l_Key_Layered.o:l_Key_Layered.cpp:(.text+0xa): undefined reference to `l_Key
_Layered::refLayerManager'
obj/l_Key_Layered.o:l_Key_Layered.cpp:(.text+0x11): undefined reference to `l_La
yerManager::getActiveLayer()'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: obj/l_Key_
Layered.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link
 failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
make: *** [obj/l_Key_Layered] Error 1

Thank you.
g++ -Wall -std=c++11 -c l_Key_Layered.cpp -o obj/l_Key_Layered.o
cc   obj/l_Key_Layered.o   -o obj/l_Key_Layered
note the differences between those lines, although they are both supposed to compile a source.
The second one is not stopping at compilation, but trying to link too, because you've got a typo in your makefile

change line 2 to OBJ = obj/l_Key_Layered.o obj/l_LayerManager.o


You could also let make to make all the work http://stackoverflow.com/questions/2908057/makefiles-compile-all-cpp-files-in-src-to-os-in-obj-then-link-to-binary
Last edited on
Thank you ne555! That fixed it. And I thought I checked everything.

The second line doesn't mean anything to me:
g++ -Wall -std=c++11 -c l_Key_Layered.cpp -o obj/l_Key_Layered.o
cc   obj/l_Key_Layered.o   -o obj/l_Key_Layered


How did you learn to understand C++ compile and linker error messages?
That would be a useful skill and I want to learn it.
`cc' is the C compiler. In my case is simply a link to gcc.

First of all, I read it incorrectly.
cc   obj/l_Key_Layered.o   -o obj/l_Key_Layered
this is a linker command, it's got the object l_Key_Layered.o and would try to build an executable with it.
This is an implicit rule, here's the process that you can see with make -d (fat removed)
Considering target file 'obj/l_Key_Layered'.
       File 'obj/l_Key_Layered' does not exist.
       Looking for an implicit rule for 'obj/l_Key_Layered'.
       Trying pattern rule with stem 'l_Key_Layered'.
       Trying implicit prerequisite 'obj/l_Key_Layered.o'.
       Found an implicit rule for 'obj/l_Key_Layered'.
        Considering target file 'obj/l_Key_Layered.o'.
          Considering target file 'l_Key_Layered.cpp'.
          No need to remake target 'l_Key_Layered.cpp'.
          Considering target file 'l_Key_Layered.h'.
          No need to remake target 'l_Key_Layered.h'.
         Finished prerequisites of target file 'obj/l_Key_Layered.o'.
        Must remake target 'obj/l_Key_Layered.o'.
g++ -Wall -std=c++11 -c l_Key_Layered.cpp -o obj/l_Key_Layered.o
        Successfully remade target file 'obj/l_Key_Layered.o'.
       Finished prerequisites of target file 'obj/l_Key_Layered'.
      Must remake target 'obj/l_Key_Layered'.
cc   obj/l_Key_Layered.o   -o obj/l_Key_Layered



About the linker error http://www.cplusplus.com/forum/general/113904/
Looking at the command that caused the complain, you can see that there is no mention of main.o or of l_Layer_Manager.o


By the way obj/main.o: main.cpp $(OBJ)
$(OBJ) should not be a dependency there, or you would have to recompile main.cpp if you changed another source.
Thanks again ne555.

I bookmarked http://www.cplusplus.com/forum/general/113904/ as "common link errors". It has already help me solve a link error.

Topic archived. No new replies allowed.