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.
Related
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.
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
I have been trying to compile the kobuki_keyop example in ROS Indigo from source on Ubuntu 14.04, I basically wrote a simple cpp file like the following:
#include "/opt/ros/indigo/include/kobuki_keyop/include/keyop_core/keyop_core.hpp"
using namespace keyop_core;
int main()
{
KeyOpCore keyy;
}
then I compiled with the following:
g++ test.cpp -L/opt/ros/indigo/lib/kobuki_keyop -o test
but it keeps giving me the same old holy grail of cpp errors:
/tmp/ccsh6f87.o: In function `main':
test.cpp:(.text+0x25): undefined reference to `keyop_core::KeyOpCore::KeyOpCore()'
test.cpp:(.text+0x34): undefined reference to `keyop_core::KeyOpCore::~KeyOpCore()'
Update:
as was pointed out by #Danh, there are solutions in the following link:
What is an undefined reference/unresolved external symbol error and how do I fix it?
however, my question is ROS related, I did not write the header files that might cause the problem, and I can't poke around all of the interlinked ones hoping to find a needle in the haystack, I was just hoping someone with background in ROS who had a similar problem might pick this up and point me to their solution.
Thank You.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 11 months ago.
#include<graphics.h>
#include<stdio.h>
main()
{
int gd=DETECT,gm;
int i,x,y;
initgraph(&gd,&gm,"");
line(0,0,640,0);
line(0,0,0,480);
line(639,0,639,480);
line(639,479,0,479);
for(i=0;i<=1000;i++)
{
x=rand()%639;
y=rand()%480;
putpixel(x,y,15);
}
getch();
closegraph();
}
The Following is a Basic Graphic Program,It Shows the Errors as
undefined reference to 'initgraph'
undefined reference to 'closegraph'
undefined reference to 'line'[4 times]
undefined reference to 'putpixel'
Compiler : CodeBlocks; Language:c++;
I Have Copied the graphics.h and winbgim.h in include folder and the libbgi.a in the lib folder also i have linked all the libraries required to be linked. Please Help.
Change
initgraph(&gd,&gm,"");
to
initgraph(&gd,&gm,NULL);
for compiling:
g++ -o filename filename.cpp -lgraph
to execute:
./filename
The functions in graphics.h are only supported by old ancient Turbo C and Turbo C++ compilers. Those functions can not be used by any modern 32-bit compiler. You can either get a copy of that old MS-DOS compiler or use win32 api GDI functions or get one of several graphics libraries such as OpenGL and QT.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I am using C library antlr3c. I installed the library using: sudo apt-get install libantlr3c-dev
#include "antlr3defs.h"
..
string DBparser::sparqlParser(const string& _sparql, SPARQLquery& _sparql_query)
{
pANTLR3_INPUT_STREAM input;
pSparqlLexer lex;
pANTLR3_COMMON_TOKEN_STREAM tokens;
pSparqlParser parser;
input = antlr3StringStreamNew((ANTLR3_UINT8 *)(_sparql.c_str()),ANTLR3_ENC_UTF8,_sparql.length(),(ANTLR3_UINT8 *)"QueryString");
}
When I run the program containing the above fragment I get the error:
NetBeansProjects/gstore/Parser/DBparser.cpp:25: undefined reference to `antlr3StringStreamNew'
I am not getting how to resolve this error as antlr3StringStreamNew is indeed declared in antlr3defs.h. Although I am unable to find its definition.
If this related to incompatibility with version 3.4 of antlr3c (as I have installed version 3.2). If this is indeed the case then, is there any alternate function in antlr3c version 3.4 by which I may achieve the same functionality.
Keith is right - the linker you are using is expecting different symbols due to C++ name mangling. Thus, though your code will compile, at the link stage it fails with that error.
If you surround your header include like so, the linker should find the symbols:
extern "C" {
#include "antlr3defs.h"
}
The result is that your code compiles with references to C-style function names, allowing the linker to match them up with corresponding symbols in the object files of the antlr3 library.