undefined reference to modbus_new_tcp Eclipse - c++

I'm very new to C/C++ and trying to connect to modbus. I am using Eclipse with the following code:
#include <stdio.h>
#include <stdlib.h>
#include <modbus/modbus.h>
int main(void) {
modbus_t *mb = modbus_new_tcp("10.84.4.128", "502");
puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
return EXIT_SUCCESS;
}
I get the error: src/Modbus.c:17: undefined reference to 'modbus_new_tcp'
In Eclipse I've when to properties and added /usr/include/modbus to the Library search path (-L) but still get the above error.
How do I define the reference?

Don't use
#include <modbus.h>
directly. Instead, include modbus_asc.h or modbus_rtu.h or modbus_tcp.h. This file modbus.h will be included automatically

To compile your code using cmake, add the modbus to target_link_libraries:
target_link_libraries([APP] modbus)

Related

How to properly include header C++ files (from ALGLIB) in Matlab MEX files

I am struggling to get a mexfunction in MATLAB to work from C++ file. Below you can find my code first the mex file to compile and call and a header and the corresponding cpp file.
And now I have 2 questions:
A basic question; that code compiles and runs, but normaly I have learned to include the header files
#include "Test.h" instead of #include "Test.cpp" in the cSpl_CPP.cpp file. But if I do so I get the following error, so I assume it does not find the Test function. Why is that so?
Error using mex
C:\Users\XXX\AppData\Local\Temp\mex_26596607590260_19292\cSpl_CPP.obj:cSpl_CPP.cpp:(.text+0x35):
undefined reference to `Test()'
collect2.exe: error: ld returned 1 exit status
Further this is just a basic test to implement the ALGLIB C++ files, which is a numerical analysis C++ code. The part I want to implement can be found in the Test.h header file within #include "interpolation.h". If I uncomment //real_1d_array x_old = "[-1.0,-0.5,0.0,+0.5,+1.0]"; in the Test.cpp file, the function should access the interpolation.h enviroment to use any necessary function within the namespace alglib. But it returns the following error. Seems to me the same problem as above, it does not find the function. Why? By the way calling all this within a C++ enviroment does work, of course with an abstracted cSpl_CPP.cpp file.
Error using mex
C:\Users\XXX\AppData\Local\Temp\mex_27656676634838_19292\cSpl_CPP.obj:cSpl_CPP.cpp:(.text+0x8f):
undefined reference to `alglib::real_1d_array::real_1d_array(char const*)'
C:\Users\XXX\AppData\Local\Temp\mex_27656676634838_19292\cSpl_CPP.obj:cSpl_CPP.cpp:(.text+0xb0):
undefined reference to `alglib::real_1d_array::~real_1d_array()'
C:\Users\XXX\AppData\Local\Temp\mex_27656676634838_19292\cSpl_CPP.obj:cSpl_CPP.cpp:(.text+0xf7):
undefined reference to `alglib::real_1d_array::~real_1d_array()'
collect2.exe: error: ld returned 1 exit status
Just to avoid any problems with the compilation command; I use:
ipath = ['-I' fullfile(pwd,'cSpl','src')]; %Folder where the c & h files except cSpl_CPP.cpp are located
mex(ipath,'cSpl_CPP.cpp')
Hoewever, putting everything in the same folder doesn't change anything.
That works for Question 1, so it seems to have the correct folder included, as it does find the Test.cpp, though obviously not the Test.h...
For me it seems like I have a general problem including header files, but I don't know which one. Has any one an idea how to solve that issue???
Thanks a lot guys!
Greetings from Germany
Pablo
Mex-Function called cSpl_CPP.cpp to compile and call:
#include "math.h"
#include "matrix.h"
#include "mex.h"
#include "Test.cpp"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello World!\n"); // prints !!!Hello World!!!
int res;
res = Test();
mexPrintf("Hello World #%d!\n",res); // prints !!!Hello World!!!
return;
}
Test header file Test.h:
#ifndef TEST_H_
#define TEST_H_
#include "interpolation.h"
using namespace alglib;
#include <iostream>
using namespace std;
int Test();
#endif /* TEST_H_ */
Test cpp file Test.cpp:
#include "Test.h"
int Test()
{
int c = 55;
cout << "Test works!" << endl;
//real_1d_array x_old = "[-1.0,-0.5,0.0,+0.5,+1.0]";
cout << "worked good\n";
return c;
}
Well actually the solution is to compile all cpp files that you will possibly use according to the command in Matlab, having all files in the same folder:
File = 'cSpl_CPP.cpp'; % Mexfunction
list = dir('**/*.cpp'); % Collect all cpp files
cppfiles = {list.name}; % Gather file names
cppfiles(find(strcmp(cppfiles,File))) = []; % delete the Mexfuntion from list
cppfiles = {File,cppfiles{:}}; % Put the Mexfuntion on the first position
mex(cppfiles{:}); % compile all files
of course all #include commands only contain *.h - files.

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).

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.