Undefined reference on GCC while in Visual C++ links normally.


EDITED: Because the code quotes seem messed up, I created a file on my github repo with the g++ output:
https://github.com/babaliaris/VampEngine/blob/master/forum_stuff


Hello!

You can see the project on my git-hub repo: https://github.com/babaliaris/VampEngine (It's really small).

I have two projects. VampEngine (A shared lib) and Application (the client).

The most important files are VampEngine/src/core.cpp where the external "C" functions are getting exported and Application/src/core.hpp where I'm using the external "C" functions.

On Windows visual c++ the code compile's, link's and run's normally but on Linux using g++ gives me undefined errors (but compiles).

You can use premake4 https://premake.github.io to generate the makefile by running:
premake4 gmake The output will be in ides/gmake/


This is the output of the make file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
==== Building VampEngine (debug64) ====
Creating ../../bin/Debug
Creating ../../bin-int/Debug/VampEngine/x64/Debug/VampEngine
core.cpp
window.cpp
stb_image.cpp
Linking VampEngine
==== Building Application (debug64) ====
Creating ../../bin-int/Debug/VampEngine/x64/Debug/Application
main.cpp
Linking Application
/usr/bin/ld: ../../bin-int/Debug/VampEngine/x64/Debug/Application/main.o: in function `VampEngine::Core::Core(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int, unsigned int)':
main.cpp:(.text._ZN10VampEngine4CoreC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjj[_ZN10VampEngine4CoreC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjj]+0x31): undefined reference to `Vamp_Core_Constructor'
/usr/bin/ld: ../../bin-int/Debug/VampEngine/x64/Debug/Application/main.o: in function `VampEngine::Core::~Core()':
main.cpp:(.text._ZN10VampEngine4CoreD2Ev[_ZN10VampEngine4CoreD5Ev]+0x17): undefined reference to `Vamp_Core_Deconstructor'
/usr/bin/ld: ../../bin-int/Debug/VampEngine/x64/Debug/Application/main.o: in function `VampEngine::Core::MainLoop()':
main.cpp:(.text._ZN10VampEngine4Core8MainLoopEv[_ZN10VampEngine4Core8MainLoopEv]+0x17): undefined reference to `Vamp_Core_MainLoop'
collect2: error: ld returned 1 exit status
make[1]: *** [Application.make:86: ../../bin/Debug/Application] Error 1
make: *** [Makefile:17: Application] Error 2


I really don't get it. I'm certain that g++ includes the appropriate paths for the linker, plus I'm positive I gave the same names in the core.cpp and core.hpp files when I declared the extern "C" functions.

This is more analytic output which shows you the actuall g++ calls:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

g++ -m64 -fPIC -I../../Depedencies/Cross-Plat/glm-0.9.9.5 -I/usr/include -I/usr/local/include -c /home/babaliaris/Deve/cpp/VampEngine/VampEngine/src/core.cpp -o ../../bin-int/Debug/VampEngine/x64/Debug/VampEngine/VampEngine/src/core.o
g++ -m64 -fPIC -I../../Depedencies/Cross-Plat/glm-0.9.9.5 -I/usr/include -I/usr/local/include -c /home/babaliaris/Deve/cpp/VampEngine/VampEngine/src/stb_image/stb_image.cpp -o ../../bin-int/Debug/VampEngine/x64/Debug/VampEngine/VampEngine/src/stb_image/stb_image.o
g++ -m64 -fPIC -I../../Depedencies/Cross-Plat/glm-0.9.9.5 -I/usr/include -I/usr/local/include -c /home/babaliaris/Deve/cpp/VampEngine/VampEngine/src/window.cpp -o ../../bin-int/Debug/VampEngine/x64/Debug/VampEngine/VampEngine/src/window.o
g++ -shared -L/usr/lib -L/usr/lib64 -L/usr/local/lib -L/usr/local/lib64 ../../bin-int/Debug/VampEngine/x64/Debug/VampEngine/VampEngine/src/core.o ../../bin-int/Debug/VampEngine/x64/Debug/VampEngine/VampEngine/src/stb_image/stb_image.o ../../bin-int/Debug/VampEngine/x64/Debug/VampEngine/VampEngine/src/window.o  -o ../../bin/Debug/libVampEngine.so -s -shared -m64 -L/usr/lib64  -lGL -lGLEW -lglfw
Output file is ../../bin/Debug/libVampEngine.so with size 138.57 KB

-------------- Build: Debug|x64 in Application (compiler: GNU GCC Compiler)---------------

g++ -m64 -I../../Depedencies/Cross-Plat/glm-0.9.9.5 -I../../VampEngine/src -I/usr/include -I/usr/local/include -c /home/babaliaris/Deve/cpp/VampEngine/Application/src/main.cpp -o ../../bin-int/Debug/VampEngine/x64/Debug/Application/Application/src/main.o
g++ -L/usr/lib -L/usr/lib64 -L/usr/local/lib -L/usr/local/lib64 -L../../bin/Debug -o ../../bin/Debug/Application ../../bin-int/Debug/VampEngine/x64/Debug/Application/Application/src/main.o  -s -m64 -L/usr/lib64  -lVampEngine
/usr/bin/ld: ../../bin-int/Debug/VampEngine/x64/Debug/Application/Application/src/main.o: in function `VampEngine::Core::Core(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int, unsigned int)':
main.cpp:(.text._ZN10VampEngine4CoreC2ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjj[_ZN10VampEngine4CoreC5ENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEjj]+0x31): undefined reference to `Vamp_Core_Constructor'
/usr/bin/ld: ../../bin-int/Debug/VampEngine/x64/Debug/Application/Application/src/main.o: in function `VampEngine::Core::~Core()':
main.cpp:(.text._ZN10VampEngine4CoreD2Ev[_ZN10VampEngine4CoreD5Ev]+0x17): undefined reference to `Vamp_Core_Deconstructor'
/usr/bin/ld: ../../bin-int/Debug/VampEngine/x64/Debug/Application/Application/src/main.o: in function `VampEngine::Core::MainLoop()':
main.cpp:(.text._ZN10VampEngine4Core8MainLoopEv[_ZN10VampEngine4Core8MainLoopEv]+0x17): undefined reference to `Vamp_Core_MainLoop'
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
4 error(s), 0 warning(s) (0 minute(s), 1 second(s))
 
Last edited on
Are you absolutely sure that your included files and link libraries are being found? It may be irrelevant but you are misspelling "Dependencies" as "Depedencies". Your paths also contain huge numbers of unlikely repeats. What is in your make file?
Last edited on
You're hiding quite a bit of information from us.

From what you've posted, it's not obvious where the VS build output is and how it compares with the GCC build output.

Focusing on GCC, you haven't shown the output of make -n

The link of main doesn't seem to use the same library built by the previous step, the directories are different.

I looked at the github project briefly, I didn't see a makefile.
Hello! Thanks for replying. Yes, I misspelled Depedencies but I use the same. Also, I don't get any "name.h" not found, so I'm positive it finds all header files. I don't provide a Makefile but a premake4 script (It's like cmake) which lets you generate one.

Both on Windows and on Linux binaries are being created at bin/Debug and objects at bin-int/Debug/VampEngine/x64/ for VampEngine and bin-int/Debug/Application/x64/ for Application.

Now in the makefile's output, you might see ../../bin/Debug and this is because it exists two folders deeper from where bin/Debug exists.

It uses the library from the previous step. But I just figured out something else!!!

I tried this: nm -CD libVampEngine.so

Output:
 
nm:libVampEngine.so: no symbols


For some reason, the symbols are not being generated. But I'm positive that core.cpp is getting compiled and linked into a .so library. So what can cause something like that?
Last edited on
@babaliaris, I appreciate that you are using relative paths, but items like the following seem to be an unnecessarily complex path/directory structure:
../../bin-int/Debug/VampEngine/x64/Debug/VampEngine/VampEngine/src/window.o


Can you show your premake4 script?
Last edited on
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
solution "VampEngine"
	configurations {"Debug", "Release"}
	platforms "x64"
	location (path.join("ides", _ACTION))




	--Application |PROJECT|==================================================
	project "Application"
		language "C++"
		kind "ConsoleApp"
		files {"Application/**.c", 
			   "Application/**.cpp", 
			   "Application/**.h", 
			   "Application/**.hpp"
		      }

		--Cross Platform Links.
		links "VampEngine"

		--Cross Plat Includes.
		includedirs {"Depedencies/Cross-Plat/glm-0.9.9.5",
					 "VampEngine/src"
					}

		--Debug bin outputs.
		configuration "Debug"
			targetdir "bin/Debug"
			objdir "bin-int/Debug/VampEngine"

		--Release bin outputs.
		configuration "Release"
			targetdir "bin/Release"
			objdir "bin-int/Release/VampEngine"

		--Windows, All configurations.
		configuration {}
		configuration "windows"

			--Windows, Defines.
			defines {"VAMP_PLATFORM_WINDOWS"}


		--Unix Based Systems, All configurations.
		configuration {}
		configuration "not windows"

			--Unix Based Systems, Includes.
			includedirs {"/usr/include/", "/usr/local/include/"}

			--Unix Based Systems, Lib Directories.
			libdirs {"/usr/lib/", "/usr/lib64/",
					 "/usr/local/lib/", "/usr/local/lib64/"
					}







	--VampEngine |PROJECT|====================================================
	project "VampEngine"
		language "C++"
		kind "SharedLib"
		files {"VampEngine/**.c", 
			   "VampEngine/**.cpp", 
			   "VampEngine/**.h", 
			   "VampEngine/**.hpp"
		      }

		--Cross Plat Includes.
		includedirs {"Depedencies/Cross-Plat/glm-0.9.9.5"}

		--Debug bin outputs.
		configuration "Debug"
			targetdir "bin/Debug"
			objdir "bin-int/Debug/VampEngine"

		--Release bin outputs.
		configuration "Release"
			targetdir "bin/Release"
			objdir "bin-int/Release/VampEngine"

		--Windows, All configurations.
		configuration {}
		configuration "windows"

			--Windows, Includes.
			includedirs {"Depedencies/Windows/glew-2.1.0/include",
			   			 "Depedencies/Windows/glfw-3.3.bin.WIN64/include"
			   			}

			--Windows, Lib Directories.
			libdirs {"Depedencies/Windows/glfw-3.3.bin.WIN64/lib-vc2019",
					 "Depedencies/Windows/glew-2.1.0"
					}

			--Windows, Links.
			links {"opengl32", "glfw3dll", "glew32"}

			--Windows, Defines.
			defines {"VAMP_BUILD_DLL", "VAMP_PLATFORM_WINDOWS"}




		--Unix Based Systems, All configurations.
		configuration {}
		configuration "not windows"

			--Unix Based Systems, Includes.
			includedirs {"/usr/include/", "/usr/local/include/"}

			--Unix Based Systems, Lib Directories.
			libdirs {"/usr/lib/", "/usr/lib64/",
					 "/usr/local/lib/", "/usr/local/lib64/"
					}

			--Unix Based Systems, Links.
			links {"GL", "GLEW", "glfw"}
By the way, I solve it! The problem was that I had a copy of libVampEngine.so inside /usr/lib, and the linker was finding that outdated binary first so it was linking against an outdated binary.

I accidentally copied that two days ago to try something.
Topic archived. No new replies allowed.