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.
Related
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.
I have been trying to get my code to compile for a while. Kept looking online for a solution to my problem but I didn't find one. Some help would be appreciated.
Here are some of the files that I have:
gate.h
#ifndef GATE_H
#define GATE_H
#include <vector>
class Gate {
private:
std::vector<int> inWires, outWires;
char function;
public:
Gate (char function, const std::vector<int>& inputWires, const std::vector<int>& outputWires);
};
#endif
gate.cpp
#include "gate.h"
Gate::Gate (char functionality, const std::vector<int>& inputWires, const std::vector<int>& outputWires) {
function = functionality;
inWires = inputWires;
outWires = outputWires;
}
circuit_file_reader.h
#ifndef CIRCUIT_FILE_READER_H
#define CIRCUIT_FILE_READER_H
#include <string>
#include <iostream>
#include <vector>
#include "circuit.h"
#include "gate.h"
Circuit readCircuit(std::string filename);
#endif
circuit_file_reader.cpp
#include "circuit_file_reader.h"
Circuit readCircuit (std::string filename) {
std::vector<int> iw (1, 7);
std::vector<int> ow (1, 8);
Gate g0 ('a', iw, ow); // This is the problem
std::vector<Gate> gates;
// gates.push_back (g0);
return Circuit (gates, 0);
}
test_circuit_file_reader.cpp
#include <iostream>
#include <string>
#include "circuit_file_reader.h"
int main(int argc, char** argv) {
readCircuit("");
std::cout << "Test Worked!" << std::endl;
return 0;
}
Whenever I try to compile this code my compiler returns
circuit_file_reader.cpp:(.text+0xa5): undefined reference to `Gate::Gate(char, std::vector<int, std::allocator<int> > const&, std::vector<int, std::allocator<int> > const&)' collect2: error: ld returned 1 exit status
Which is strange because the Gate constructor has been defined so why can't it see it?
You declared the header in the header file, but its definition lies in the source (.cpp) file. You aren't specifying this to the compiler. You didn't specify what you're using to compile but if you compile all source files properly then it should work. Here is the example using g++.
You didn't provide the required circuit.h file to compile the program, so I made a dummy one just so it could compile.
Edit: I was asked to remove the picture so here is the g++ command in text.
g++ circuit_filer_reader.cpp gate.cpp main.cpp -o test
./test
Test Worked!
If for whatever reason anyone wants to compile it here is the dummy circuit.h file.
#include "gate.h"
struct Circuit
{
Circuit(std::vector<Gate> gates, int test)
{
return;
}
};
You didn't provide your compiler command, but I guess you didn't link gate.o while trying to generate the final binary.
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)
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).
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.