g++ linker is unable to locate functions despite header inclusion [duplicate] - c++

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 5 years ago.
I have 4 files world.cpp, world.hpp, Helpers/tools.cpp, Helpers/tools.hpp.
wrold.cpp and tools.cpp include their respective headers and world.hpp includes tools.hpp
world.hpp/cpp use structures defined in tools.hpp/cpp
on my CMake file I have added
target_sources(voxel-world PUBLIC ${PROJECT_SOURCE_DIR}/Helpers/tools.cpp)
and
target_sources(voxel-world PUBLIC ${PROJECT_SOURCE_DIR}/World.cpp)
So, to my understading, the generated make file should attempt to link everything properly.
However when I attempt to compile my program I get the error message:
CMakeFiles/voxel-world.dir/source/World.cpp.o: In function `Chunk_Holder::Chunk_Holder()':
World.cpp:(.text+0x112a): undefined reference to `cirArray<cirArray<cirArray<Chunk*> > >::cirArray()'
CMakeFiles/voxel-world.dir/source/World.cpp.o: In function `Chunk_Holder::Chunk_Holder(int, int, int, World*)':
World.cpp:(.text+0x117c): undefined reference to `cirArray<cirArray<cirArray<Chunk*> > >::cirArray()'
World.cpp:(.text+0x11a2): undefined reference to `cirArray<cirArray<cirArray<Chunk*> > >::cirArray(unsigned int)'
World.cpp:(.text+0x11ee): undefined reference to `cirArray<cirArray<Chunk*> >::cirArray(unsigned int)'
...
...
I am not sure why the linker is failing to find these declarations, since the include statements are on each file. What have I missed?

The problem was caused because cirArray is a templated class, and apparently the compiler is unable to generate the relevant code if the implementation is in the .cpp file.
The solution is thus to move the class implementation into the header.

Related

Undefined reference to functions using multiple files [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
How does the compilation/linking process work?
(5 answers)
Closed 2 years ago.
I'm new to C++ and I'm trying to implement all functions in a separate Functions.cpp file and calling them in the main.cpp file defining all the function declarations on the top. Both Main and the Functions file are in the same folder (sources).
But I keep getting the error undefined reference to my functions. I'm using CodeBlocks 20.30.
somebody, please help.
Main.cpp file
Functions.cpp file
Error

g++ 'undefined reference' error when function is already defined [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I have a static library, libmylib.a, which contains lots of pre-compiled objects. All the header scripts for these object are stored in /path/to/includes/. I am compiling my main script, BACnetSearch.cpp using g++ with the line below:
g++ BACnetSearch.cpp -I/path/to/includes/ -L/path/to/libraries/ -lmylib
All functions used from the library work except one, which gives the undefined reference to 'function_name'. I have checked the function has been instantiated in the appropriate header file, exists within an object in the library, and I have included it at the top of my script. The library is BACnet, so assuming the release has no bugs, where do I start looking to fix this.
Any other info you need just ask I will try to add. Thanks :)
EDIT: Error message received:
/tmp/ccDIISDz.o: In function `main':
BACnetSearch.cpp:(.text+0x67e): undefined reference to `bvlc_receive'
collect2: error: ld returned 1 exit status
EDIT 2: Auto-marked as answered elsewhere, then linked to a generic question with too many possible problems. Only possibility is that when the library is compiled, the order scripts are compiled in causes this error based from scripts dependencies on each other.
You can always start by checking that the function has been implemented. It is a common mistake to defined a prototype like so:
int isLarger();
And never implement the function, and in that case you get isLarger is not defined errors....
Looking at their site perhaps you should compile the code with a make file and like so:
make BACDL_DEFINE=-DBACDL_MSTP=1 clean all

Duplicate symbol error adding new library [duplicate]

This question already has an answer here:
Odd duplicate symbols error
(1 answer)
Closed 7 years ago.
working in the ns-3 environment, I have made a library where there are some methods called in my code. In order to compile the library I add in the wscript file the link to the library and I control if it is already define as follow:
#ifndef MY_LIBRARY_H_
#define MY_LIBRARY_H_
.. my methods
#endif
When I build the code this following error is generated:
duplicate symbol __Z8getValueiib in:
src/model/bs-phy.cc.1.o
src/model/ue-phy.cc.1.o
ld: 3 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I think this is due to the fact that I use my methods in more then one class and maybe there are some error for multi-compiling. Any idea to solve the problems? (I'm not an expert and maybe I'm missing something!!!)
Thanks for the help!!
[EDIT]
uint32_t findOutSector()
{
uint32_t sector = 0;
return sector;
}
Is your function findOutSector written in the header directly? If yes, put the definition in a .c file (+ compile & link) and just the function declaration in the header. The function should just exist once in all compiled objects and the declaration serves as reference to it.

undefined reference to A::funcA(int) [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I am currently starting work on a project where i would have a file of shared tool functions which i can use without having to instance the class, i have read about using static functions for this so i decided to try to do the same. However i seem to keep getting an error of undefined reference to my static function. Looking for an answer I came upon several answers like the one in the following link but i still fail to compile my code:
undefined reference to a static function
I decided to copy and paste the code given in the link above, a.cpp/.h and b.cpp/.h files, but even then i get compilation errors:
undefined reference to main [this error i can remove by adding a simple main function in b.cpp]
undefined reference to A::funcA(int)
Am i forgeting something when compiling? I am simply using g++ b.cpp am using g++ 4.7.2.
Thanks in advance.
Using the code from the linked question as an example: since the definition of the function A::funcA(int) is in a.cpp, you also need to include that source file in your compilation, like
g++ b.cpp a.cpp
You need to compile a.cpp as well:
g++ b.cpp a.cpp
Otherwise the linker can't find the definition for A::funcA because it's located in a.cpp.

Strange output when compiling c++ code. Any Ideas?

When i compile my code i get a set of errors that appear to related to the output files as in the .o file. I'm not sure why these sorts of errors would occur. Any Ideas?
/tmp/ccjPLJVV.o: In function `PubSub::~PubSub()':
Video_process.cpp:(.text._ZN6PubSubD2Ev[_ZN6PubSubD5Ev]+0x12): undefined reference to `vtable for PubSub'
/tmp/ccjPLJVV.o: In function `main':
Video_process.cpp:(.text.startup+0x34): undefined reference to `vtable for PubSub'
Video_process.cpp:(.text.startup+0xeb): undefined reference to `PubSub::run()'
/tmp/ccjPLJVV.o:(.rodata._ZTI13Video_process[typeinfo for Video_process]+0x10): undefined reference to `typeinfo for PubSub'
collect2: ld returned 1 exit status
This is essentially the output i'm getting when I attempt to compile.
It appears you have unimplemented virtual methods.
class PubSub
{
//virtual destructors, although pure
//MUST have an implementation
virtual ~PubSub() = 0 { }
/*virtual?*/ void Run(); // <--- have you implemented this one?
};
Maybe you've implemented the method, but you have not linked it. If you're using GCC, -o flag is your friend; all your class .o files must be specified when compiling the main.cpp.
this is an error message from the linker, not the compiler. The linker cannot find some symbols which are declared, but not defined, in some files it tries to link together to make (most likely) an executable. The solution is to provide the definitions, i.e. the (compiled) code with those definitions. That code may already exist and you just have to "link against it" (tell the linker to search for symbols there) or may not, in which case you have to provide it...
for example, add the file defining the implementations of class PubSub to the linker/compiler command line should help ...