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

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

Related

Undefined reference to linker error when using namespaces in headers in c++ [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 1 year ago.
I've read through all similar "Undefined reference to" threads I could find but couldn't find a solution. Most of the other threads also involved classes which I don't plan to use for this. The program compiles executes normally if I define the function within the header instead of using an external .cc file. I feel like I'm missing something simple here.
This is the simplest test I could put together that recreates the issue I'm having.
Compiler: g++ (Debian 8.3.0-6) 8.3.0
hntest.h
namespace hntest
{
void pewpew();
}
hntest.cc
#include <iostream>
#include "hntest.h"
namespace hntest
{
void pewpew()
{
std::cout << "pew pew pew!!!" << std::endl;
}
}
hntestmain.cc
#include "hntest.h"
int main(int argc, char* argv[])
{
hntest::pewpew();
}
I'm attempting to compile with:
g++ -lstdc++ hntestmain.cc -o hntestmain
And I get the following linker error:
hntestmain.cc:(.text+0x10): undefined reference to `hntest::pewpew()'
collect2: error: ld returned 1 exit status
I have tried reading through the code of a couple popular C++ libraries as well as some of my own older C (not ++) code and makefiles but haven't been able to find my error. I'm admittedly both and amateur an a bit rusty.
What am I missing?
You are not actually compiling the cpp file that has the definition of pewpew.
Try:
g++ -lstdc++ hntestmain.cc hntest.cc -o hntestmain
The compiler needs to know about all the source files. The header file is dealt with during pre-process and knows to look in the same folder. You can imagine that if you have more files, say 10, 100 or 10000, this would become impossible to manage by using command line. That is why people created build systems like make, cmake and bazel.
For much greater detail see the answer here which is specific for your case of linker error.

Trying to run separated classes in vs code in c++ [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 3 months ago.
I am new to vs code, after previously using atom a little bit. I am working on c++ with a program that has some classes in separated files. I have already installed the c/c++ extension, but I am facing a problem. When I try to run the program with all it's files included, it shows me the following error:
main.cpp:(.text+0x1f): undefined reference to `Car::sayHello()'
collect2: error: ld returned 1 exit status.
the code is pretty basic.
#include <iostream>
#include "Car.h"
using namespace std;
int main(){
Car c;
c.sayHello();
cout << "Hello world";
return 0;
}
From what I remember from atom, selecting all the files and running them would solve the problem, which I think is the same as running the following command in the terminal:
g++ Car.cpp Car.h main.cpp -o Classes
I have tried these in vs code and the problem seems to appear again. Help is much appreciated.
So if somebody else might have the same problem as me in the future. The problem was that I had declared the constructor and the destructor in the header file, but I hadn't defined them in the .cpp file, after doing that the program seems to be running well. Also as Fred mentioned Car.h is redundant to be mentioned in compilation process, but anyways the program will compile successfully, even if it is used.
So I had the same problem but I figured it out thanks to this link. Follow the instructions and test it out and it should work. There are also other ways to solve this problem based on your preference that are shown on this website. I did notice, however, that once you add multiple folders to your workspace, compiling whatever main.exe file you want becomes difficult since you use the arg ${workspaceFolder}\\*.cpp to compile all the .cpp files in your workspace folder. One way to solve that is to create multiple workspaces, but if anyone knows a more efficient way to do this, please let me know.
you can install fleeox in VS code that will help

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

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.

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.