This question already has answers here:
Rcpp - sourceCpp - undefined symbol
(2 answers)
Closed 4 years ago.
I have existing C code that consists of three files: a header file ( a ".h" file), a library file (a ".o" file), and a source file. They currently run under UNIX and as compiled "mex files" in Matlab. I would like to port them to R using Rcpp. They are all long and complex, so I made a minimal example to help me understand how to port them to R.
The simplified header file (my_header.h) is:
typedef unsigned int ui4;
ui4 add_one( ui4 );
The simplified "library" file (my_lib.cpp) is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "my_header.h"
ui4 add_one(ui4 x) {
return(x+1);
}
The simplified function program (my_program.cpp) is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <Rcpp.h>
#include <cmath>
#include "my_header.h"
using namespace Rcpp;
// [[Rcpp::export]]
ui4 my_program_r () {
//int main (int argc, const char * argv[]) {
//
// As a MATLAB mex file, this function calls "main(..."
//
ui4 value = add_one( (ui4)1 );
printf( "%d", value );
return value;
}
From the terminal (I'm on a Mac), I can compile these without an error:
$ g++ my_lib.cpp -c -o my_lib.o
$ g++ my_program.cpp -o my_program my_lib.o
When I try to compile them in RStudio, I get:
> library(Rcpp)
> sourceCpp( "my_program.cpp" )
Warning message:
In sourceCpp("my_program.cpp") :
No Rcpp::export attributes or RCPP_MODULE declarations found in source
>
Why isn't this compiling under Rcpp? How do I specify the linked file (the ".o" library file) in the "sourceCpp" command? Do I need to specify the header file?
The sourceCpp command is meant for single files only. If you have multiple files, you have to use a package:
Call Rcpp::Rcpp.package.skeleton(...) to create a skeleton package.
Copy *.h, *.c and *.cpp to the src folder.
Call Rcpp::compileAtrributes().
Use R CMD build ..., R CMD check ... and R CMD INSTALL ... to build, check and compile the package. (The check will complain about undocumented functions ...)
For more details see Rcpp-package vignette and for example this question. BTW, since R does not have an unsigned int type, I am not sure if your return value will work. You might have to switch to an int or double. I also get a different error message than you:
Error in dyn.load("/tmp/RtmpSbvXHx/sourceCpp-i686-pc-linux-gnu-0.12.17/sourcecpp_625ad24a943/sourceCpp_2.so") :
unable to load shared object '/tmp/RtmpSbvXHx/sourceCpp-i686-pc-linux-gnu-0.12.17/sourcecpp_625ad24a943/sourceCpp_2.so':
/tmp/RtmpSbvXHx/sourceCpp-i686-pc-linux-gnu-0.12.17/sourcecpp_625ad24a943/sourceCpp_2.so: undefined symbol: _Z7add_onej
Are you sure the above code is exactly what you used?
Related
While following the book C++ For Dummies, I have three files in my CodeBlocks project, main.cpp, Pen.h, and Pen.cpp. They look like this:
main.cpp:
#include <iostream>
#include "Pen.h"
//#include "Pen.cpp"
using namespace std;
int main()
{
Pen MyPen = Pen();
MyPen.test();
}
Pen.h:
#ifndef PEN_H_INCLUDED
#define PEN_H_INCLUDED
//#include "Pen.cpp" // Uncommenting this gives a different error
using namespace std;
class Pen
{
public:
// attributes omitted
// PROTOTYPES:
// other functions omitted
void test();
};
#endif // PEN_H_INCLUDED
Pen.cpp:
#include <iostream>
#include "Pen.h"
using namespace std;
//other function definitions omitted
void Pen::test()
{
cout << "Test successful." << endl;
}
When I run the code as listed above, I get an "undefined reference to `Pen::test()'" error. To fix this, I changed the #include statements at the top of main.cpp to:
#include <iostream>
//#include "Pen.h"
#include "Pen.cpp"
This works as intended and correctly prints out "Test successful."
My question is this: what in the world is the point of putting a function prototype in a header file if I have to import the .cpp file later on anyways?
EDIT: It turns out this was a problem with not knowing how to use Code::Blocks rather than with the C++ language.
Assuming you're using gcc, you can compile and link in one step by supplying multiple .cpp files via the command line.
g++ Pen.cpp main.cpp
clang should be similar.
clang++ Pen.cpp main.cpp
An #include should never reference a .cpp file. At all. There's no good reason to do it. Include your headers and then supply the names of all .cpp files when you compile. If your project gets big and you have too many .cpp files to reasonably list, then it's time to break out a makefile or similar.
In the main.cpp include the header file:
#include "Pen.h"
The Pen.h file it's ok.
You need to add the Pen.cpp file to the project tree.
Go to Project -> Add files... and add Pen.cpp
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 am writing a simple program to compile from the command prompt. There is an app1 program that uses a class located in a separate file. The class sample1 has a header file sample1.h where all the function declarations exist. The sample1.cpp file contains the function definitions.
app1.cpp:
#include <iostream>
#include "sample1.h"
using namespace std;
int main()
{
sample1 t;
cout<<"The program is ending"<<endl;
return 0;
}
sample1.h:
#ifndef SAMPLE1_H
#define SAMPLE1_H
class sample1
{
public:
sample1();
};
#endif
sample1.cpp:
#include<sample1.h>
#include <iostream>
using namespace std;
sample1::sample1()
{
cout<<"This error sucks"<<endl;
}
I am compiling this program using Windows Subsystem for Linux. However, when I try to compile with g++ 'app1.cpp' -o app1, I get this error:
I get that the error is telling me that the program cannot find the constructor sample::sample() but why is that? I included the header file in the program. These 3 files all exist in the same folder.
You have to compile all your .cpp files (sample1.h is included by preprocessor from information in app1.cpp only):
g++ app1.cpp sample1.cpp -o app1
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.
here is the structure of my program:
// File: main.cpp
#include <iostream>
#include <math.h>
using namespace std;
#include "do.cpp"
int main()
{
doit();
}
// File: do.cpp
void doit()
{
cout<<sqrt(2)<<endl;
}
When I do
g++ main.cpp
Everything goes fine. Now, if I open this as an Xcode project (I have chosen "Command line utility" as project type) and try to just build and run, it keeps saying:
Use of undeclared identifier sqrt
Unknown type name 'ostream'
What should I be supposed to do? Did I do something wrong?
Thank you very much!
Matteo
Don't do this:
#include "do.cpp"
but instead put that "do.cpp" file in the same Xcode project, alongside your main.m or main.cpp file.
And when you want to build from the command line, you can do:
g++ main.cpp do.cpp -o mytesttool
which would create the command line tool named "mytesttool".
The explanation is quite simple actually. You probably added both files to the project. Xcode tries to compile each file into an object file and then link them together.
When it tries to compile do.cpp it doesn't find the definition of cout because iostream is not included and neither math.h for sqrt, as part of do.cpp.
That file compiles fine when compiled as part of main.cpp, because it is included in the file and it finds iostream and math.h and also the using declaration.
Anyway if you remove do.cpp from the project (just the reference) everything should compile as expected.
The right way without a header file
// File: main.cpp
void doit(); // declare the function
int main()
{
doit();
}
// File: do.cpp
#include <iostream>
#include <math.h>
using namespace std;
void doit()
{
cout<<sqrt(2)<<endl;
}
The right way with a header file
// File do.h
#ifndef __DO_H_
#define __DO_H_
void doit();
#endif // __DO_H_
// File: main.cpp
#include "do.h"
int main()
{
doit();
}
// File: do.cpp
#include <iostream>
#include <math.h>
#include "do.h"
using namespace std;
void doit()
{
cout<<sqrt(2)<<endl;
}
When making the new file, I forgot to de-check the "target" selection, so that when it tried to build the project it tried to build all the single files and then link them together. By disabling the "target", I got it to work.
This question already has answers here:
main.cc:5:30: fatal error: folder/file.h: No such file or directory
(3 answers)
Closed 9 months ago.
I began learning MinGW to compile C++ program. I had a sample C++ file including test.cpp (main program) and srfft.h (extra header file I added, not from the libray). The process executed as below step:
g++ test.cpp -o test.exe
test.exe
#include <iostream>
using namespace std;
int main()
{
cout<< "Hello World!\n";
return 0;
}
I got the correct answer, but when I added #include in the C++ code as below:
#include <iostream>
#include <srfft.h>
using namespace std;
int main()
{
cout<< "Hello World!\n";
return 0;
}
CMD showed me" fatal error: srfft.h:No such file or directory"
How do I execute my code with MinGW?
Where is the problem?
Try
#include "srfft.h"
Notice "" instead of < and >. With "srfft.h", the file is searched relative to the current directory, whereas with <srfft.h> the search only takes place in the system and specified include directories, which normally doesn't include the current directory.