Undefined reference to a static method - c++

I m trying to compile this code and linking fails with the following error:
this is how i m compiling it;
g++ logtester.cc -I/home/foo/include -L/home/foo/lib -llog4cxx
/tmp/ccADKreY.o(.text+0x120): In function `main': undefined reference to `FrameworkLogger::getInstance()'
collect2: ld returned 1 exit status
Why? how can i fix it?
#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>
using namespace log4cxx;
using namespace log4cxx::xml;
using namespace log4cxx::helpers;
class FrameworkLogger
{
private:
FrameworkLogger();
LoggerPtr logger;
public:
static LoggerPtr getInstance();
};
(Another file:)
#include "FrameworkLogger.h"
#include <iostream>
LoggerPtr FrameworkLogger::getInstance()
{
std::cout<<"test";
}
(Yet another file:)
#include "FrameworkLogger.h"
#include <iostream>
using namespace std;
int main(){
// LoggerPtr logger =
FrameworkLogger::getInstance();
std::cout<<"test";
}

This sounds like a linker error. Ensure that you are properly linking all of your object files

You need to list all compilation units (.cc files) in the compiler invocation:
g++ logtester.cc the-file-you-have-not-named.cc -I/home/foo/include -L/home/foo/lib -llog4cxx

Related

While compiling it showing undefined reference to

I'm doing this using a sublime text editor and GNU Compiler....
And I have created this in these three file at the same hierarchical level. Still don't why it showing .....
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe C:\Users\BIVASH~1\AppData\Local\Temp\cc1guWsB.o:Class.cpp:(.text+0xc): undefined reference to `speak()'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\BIVASH~1\AppData\Local\Temp\cc1guWsB.o:Class.cpp:(.text+0x11): undefined reference to `jump()'
collect2.exe: error: ld returned 1 exit status```
Class .cpp
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
speak();
jump();
return 0;
}
Cate.h
#ifndef CAT_H
#define CAT_H
void speak();
void jump();
#endif
Cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
void speak(){
cout<<"mmmeeeeoooowwww!!!!";
}
void jump(){
cout<<"jump jump jump !!!!!";
}
When you want to use functions written on a different compilation unit, you have to link with the other unit's object file.
To do this, it's not sufficient to include the other unit's header, because it gives you only the declarations of the functions. In order to reach the definitions, you should compile both of the units together. For example, For foo.cpp and bar.cpp: g++ foo.cpp bar.cpp
In your case, just write:
g++ Class.cpp Cat.cpp

Using same namespace in C++ for two classes declared and defined in separate files

I am trying to declare two classes C1 and C2 in files nstest1.h and nstest2.h which are defined in files nstest1.cpp and nstest2.cpp respectively. Both the classes are defined under same namespace.
Following are the files :
//nstest1.h
namespace Mine{
class C1{
public:
void callme();
};
}
//nstest2.h
namespace Mine {
class C2 {
public:
void callme();
};
}
//nstest1.cpp
#include<iostream>
#include "nstest1.h"
using namespace std;
using namespace Mine;
void Mine::C1::callme(){
std::cout << "Please call me " << std::endl;
}
//nstest2.cpp
#include<iostream>
#include "nstest2.h"
using namespace std;
using namespace Mine;
void Mine::C2::callme(){
std::cout << "Please call me too" << std::endl ;
}
Following file tries to use this classes using namespace Mine.
//nstest.cpp
#include<iostream>
#include "nstest1.h"
#include "nstest2.h"
using namespace std;
using namespace Mine;
int main(){
Mine::C1 c1;
Mine::C2 c2;
c1.callme();
c2.callme();
return 0;
}
When I compile using command "g++ nstest.cpp", I get following error :
/tmp/cc2y4zc6.o: In function `main':
nstest.cpp:(.text+0x10): undefined reference to `Mine::C1::callme()'
nstest.cpp:(.text+0x1c): undefined reference to `Mine::C2::callme()'
collect2: error: ld returned 1 exit status
If the definitions are moved to the declaration files (nstest1.h and nstest2.h), it works fine. Not sure whats happening here. Am I missing something ?
Thanks in advance :) .
You need to include the other .cpp files when building the program.
Option 1: Compile all the files and build the executable in one command
g++ nstest.cpp nstest1.cpp nstest2.cpp -o nstest
Option 2: Compile each file separately and then build the executable after that
g++ -c nstext1.cpp
g++ -c nstest2.cpp
g++ -c nstest.cpp
g++ nstest.o nstest1.o nstext2.o -o nstest
Your problem happens at link time. Your headers are fine. But you should compile the other cpp files aswell.

error: "undefined reference to" while compiling c++

i'm working in a little c++ application, i'm trying to use xdotool (libxdo: https://github.com/jordansissel/xdotool ).
i builded xdotool using the "make" command, and put the libxdo.so and libxdo.so.3 into /usr/lib. and xdo.h into /usr/local/include.
im trying to compile my application using:
g++ -I /usr/local/include/ -L /usr/lib/ LinuxTest.cpp -lXtst -lX11 -lxdo
but im getting this error:
undefined reference to `xdo_new(char const*)'
undefined reference to `xdo_move_mouse_relative(xdo const*, int, int)'
this is my source code:
#include <iostream>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/X.h>
#include <unistd.h>
#include <X11/extensions/XTest.h>
#include <xdo.h>
using namespace std;
#define KEYCODE XK_Tab
int mapa[2048];
void hook();
xdo_t* xdoMain;
int main() {
for (int i=0;i<2048;i++){
mapa[i]=0;
}
xdoMain = xdo_new(NULL);
xdo_move_mouse_relative(xdoMain,200,200);
hook(); //do some things using X11
return 0;
}
I am guessing this is because xdo is a C library.
You are linking and building a C++ application.
Thus your compiler is thinking that xdo_new() is a C++ name mangled function. But in reality it has been linked into libxdo. as a C name mangled function.
I would try this:
extern "C" {
#include <xdo.h>
}
You are basically telling the compiler to treat all the names in xdo as C function declarations. As long as there are no classes this should work (if there are classes then my assumption is incorrect to start with).

Dynamic library using static library in c++ name mangling error

I am trying to create a dynamic(.so) wrapper library along mongoDB c++ driver. There is no problem with the compilation but when I test it in a C++ sample program i get the error
undefined symbol: _ZN5mongo18DBClientConnection15_numConne
which i assume has something to do with name mangling issues.
I compiled the library as
g++ -fPIC -shared mongoquery.cpp -I/pathto/mongodriver -lmongoclient -lboost_thread-mt -lboost_filesystem -lboost_program_options -o libmongoquery.so
Here's the program I am using for testing:
#include <iostream>
#include <dlfcn.h>
#include "mongoquery.hpp"
using namespace std;
int main()
{
void *lib_handle;
int (*fn)(int *,string);
lib_handle=dlopen("./libmongoquery.so",RTLD_NOW);
if(!lib_handle)
{
cerr<<"Error"<<dlerror();
return 1;
}
fn=(int (*)(int *,string))dlsym(lib_handle,"count_query");
string q="{}";
int n;
(*fn)(&n,q);
cout<<n;
dlclose(lib_handle);
return 0;
}
the header mongoquery.hpp contains
#include <iostream>
#include <client/dbclient.h>
#define HOST "localhost"
#define COLLECTION "test.rules"
using namespace mongo;
using namespace std;
class mongoquery
{
private:
string q;
mongo::DBClientConnection c;
public:
mongoquery(string);
int result_count();
};
int count_query(int *,string);
Thanks
The answer can be followed from this question
Dynamic library uses statics libraries, undefined symbols appears
Added for achival purpose

undefined reference to a class ERROR

I am working in c++ /ubuntu.
I have:
libr.hpp
#ifndef LIBR
#define LIBR
#include <string>
using namespace std;
class name
{
public:
name();
~name();
std::string my_name;
std::string method (std::string s);
};
#endif
and
libr.cpp
#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"
using namespace std;
name::name()
{
}
std::string name::method(std::string s)
{
return ("YOUR NAME IS: "+s);
}
From these two I've created a libr.a.
In test.cpp:
#include <iostream>
#include <string>
#include <stdlib.h>
#include "libr.hpp"
using namespace std;
int main()
{
name *n = new name();
n->my_name="jack";
cout<<n->method(n->my_name)<<endl;
return 0;
}
I compile with g++ and libr.a. I have an error: "name::name() undefined reference", why?
I would like to mention that I've added in qt creator at qmake the .a. When I compile, I have the error. How can I solve it?
This is a linker error, not a compiler error. It means that you have called but you have not defined the constructor. Your allocation name *n = new name(); calls the constructor.
Since you defined the constructor in your libr.cpp, what this means is that this compilation unit is not making its way into your executable. You mentioned that you are compiling with libr.a. When you compile your libr.cpp the result is a .o file, not a .a file.
You are not linking libr.o into your executable.
What are the steps you're using to compile your "project"?
I performed the following steps and managed to build it with warnings/errors.
g++ -Wall -c libr.cpp
ar -cvq libr.a libr.o
g++ -Wall -o libr main.cpp libr.a
One last thing, if I change the order off the last command, like
g++ -Wall -o libr libr.a main.cpp
I get the following error:
Undefined first referenced
symbol in file
name::name() /tmp/cc4Ro1ZM.o
name::method(std::basic_string<char, std::char_traits<char>, std::allocator<char
> >)/tmp/cc4Ro1ZM.o
ld: fatal: Symbol referencing errors. No output written to libr
collect2: ld returned 1 exit status
in fact , you needn't define the destructor yourself because the default destructor will be used when the class calling is over.
and in the VS2008,it's all right!