error: "undefined reference to" while compiling c++ - 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).

Related

Trying to link a test program to a library, getting error: iostream: No such file or directory

I'm trying to create a c++ library for the purpose of controlling a signal generator. I have a working script that compiles into an executable that runs the way I want, and now I want to create a static library with a 'signal' class such that the code can be integrated into a larger library being created by the research collaboration I'm part of for all the hardware we have. However, I'm having trouble compiling a test program (test.cc) for the signal.cpp source code and signal.h that I've written.
Here's signal.cpp:
#include "signal.h"
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
signal::signal(){
myfile.open("/dev/usbtmc1");
}
signal::~signal(){
myfile.close();
}
int signal::on(){
myfile<<"*RST\n";
myfile<<":DISPLAY OFF\n";
myfile<<"FUNC1:PULS:HOLD WIDT\n";
myfile<<"FUNC1:PULS:TRAN:LEAD 2.5NS\n";
myfile<<"FUNC1:PULS:TRAN:TRA 2.5NS\n";
myfile<<":FUNC1:PULS:WIDT 20NS\n";
myfile<<":FUNC1 PULS\n";
myfile<<":VOLT1 5.0V\n";
myfile<<":VOLT1:OFFS 1.797V\n";
myfile<<":FREQ1 100HZ\n";
myfile<<":OUTP1:IMP:EXT MAX\n";
myfile<<":ARM:SOUR1 IMM|INT\n";
myfile<<":OUTP1 ON\n";
myfile<<":OUTP1:COMP ON\n";
return 0;
}
int signal::off(){;
myfile<<"*RST\n";
return 0;
}
int signal::write(char comstring[80]){
strcat(comstring,"\n");
myfile<<comstring;
return 0;
}
Here's signal.h:
#ifndef SIGNAL_H
#define SIGNAL_H
#include <iostream>
#include <fstream>
#include <string.h>
//using namespace std;
class signal{
private:
std::ofstream myfile;
public:
signal();
~signal();
int on();
int off();
int write(char comstring[80]);
};
#endif
And the little test program I've written to try out calling the signal class:
#include "signal.h"
using namespace std;
int main(){
signal ser;
ser.on();
}
I can get the signal.cpp and the signal.h files to compile into a signal.so dynamic object, but when I try to call 'g++ test.cc -o test -l signal.h' at the terminal, I get the error:
signal.h:4:20: error: iostream: No such file or directory
signal.h:5:19: error: fstream: No such file or directory
signal.h:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘signal’
I'm confused by this, as I thought iostream and fstream were part of the c++ standard library and therefore wouldn't need to be linked when compiling using g++. Could anyone please illuminate me as to what I should fix or try to sort this? Many thanks. Sam.
Hoping your all files are in same folder.
g++ -I . test.cpp signal.cpp -o test

Undefined reference to library error in C++

this code:
#include "SoftwareSerial.h">
#include <avr/io.h>
#include <HardwareSerial.h>
#include <avr/interrupt.h>
void read_response();
int main () {
sei();
Serial.begin(2400);
uint8_t receivePin = 2;
uint8_t transmitPin = 3;
SoftwareSerial softSerial(receivePin, transmitPin);
softSerial.begin(2400);
while(1){
softSerial.println("to soft serial");
Serial.print(softSerial.read());
}
}
gives me this error at compile time:
undefined reference to `SoftwareSerial::SoftwareSerial(unsigned char, unsigned char, bool)'
I have tried using #include "SoftSerial.h" but no difference. The SoftSerial.h and SoftSerial.cpp files are in my libraries folder where the HardwareSerial.h files also resides.
What am i missing?
This is not a compile error. This is a linker error.
If SoftSerial is part of your own project, the CPP file is probably not part of the compiled project.
If it is an external library, you need to link to it. How you do that depends on your IDE/compiler.

c++ mingw STL installation

I recently installed MinGW and MSYS on my Windows 32 machine and it seems to be running fine.
On the C++ compiler, I am including a vector container and getting no errors to that. But I`m getting compile-time errors when I try to use it.
So, the code
#include <vector> // include vector.h
#include <stdio.h> // include stdio.h
using namespace std;
main() {
// vector<int> A;
printf("\nHeya ..");
}
is running just fine. However, the moment I un-comment line 8-- the vector declaration line, I get the following error (shortened) in compile time:
undefined reference to 'operator delete(void*)'
undefined reference to '__gxx_personality_v0'
You're probably compiling with gcc instead of g++. The actual compiler is the same, but g++ tells the linker to use the default C++ libraries, were gcc only tells it to look at the C libraries. As soon as you use and C++-specific parts of the standard library, gcc will fail.
As an aside, C++ doesn't support the default int rule from old C, so you should really specify the return type from main.
I don't see how you are compiling your code. Your main method is invalid, incorrect signature and you aren't returning anything.
Should be like this:
#include <vector> // include vector.h
#include <stdio.h> // include stdio.h
using namespace std;
int main(int, char**) {
// vector<int> A;
printf("\nHeya ..");
return 0;
}
Also you need to compile this with g++ and not gcc.

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!