Zalan Szakolci
2012-May-08 22:35 UTC
[Rd] R CMD check linking errors, when interfacing c++
Hi there, I am trying to interface c++ code in R and make a package. With R CMD SHLIB the dll was created, but when I try R CMD check, I am getting 'undefined reference to..' linkage error messages. The relevant c++ source from conf-infomap.cpp: #include "conf-infomap.h" #include "R.h" // R functions #include "Rinternals.h" #include "Rmath.h" // R math #include "Rdefines.h" using namespace std; using std::cout; using std::cin; using std::endl; [...] extern "C" { void RosvallDynamicSkeleton(int *seed,char** file, int *attempts, int *resamples, double *conf) { int ret = confInfomap(seed[0],file,attempts[0],resamples[0],conf[0]); if(ret == 0) Rprintf("cluster map has been written to %s\n", file[0]); R_FlushConsole(); R_ProcessEvents(); return; // Return Nothing. } } extern "C" { void findConfCoreWrapper(SEXP gdiag, SEXP bootClusters, SEXP membership, SEXP conf) { multimap<double,treeNode,greater<double> > treeMap; multimap<double,treeNode,greater<double> >::iterator it_tM; int l =LENGTH(membership); //Rprintf("beleptunk"); int p = 0; map<int,int> degrees; for(int i=0;i < l;i++){ SEXP members = VECTOR_ELT(membership,i); //Rprintf("%d\n",Nmembers); SEXP membersSXP = VECTOR_ELT(members,0); SEXP sizesxp = VECTOR_ELT(members,1); double* sizevc = REAL(sizesxp); double size = sizevc[0]; int Nmembers = LENGTH(membersSXP); treeNode tmp_tN; //Rprintf("the size of the module is: %f\n",size); it_tM = treeMap.insert(make_pair(size,tmp_tN)); //Rprintf("%d\n",Nmembers); for(int j=0;j<Nmembers;j++) { SEXP node = VECTOR_ELT(membersSXP,j); SEXP nodesizesxp = VECTOR_ELT(node,0); SEXP nodeidsxp = VECTOR_ELT(node,1); SEXP nodenamesxp = VECTOR_ELT(node,2); int degree = INTEGER(VECTOR_ELT(node,3))[0]; int nodeid = INTEGER(nodeidsxp)[0]; degrees.insert(make_pair(nodeid,degree)); string nodename = string(CHAR(STRING_ELT(nodenamesxp,0))); double nodesize = REAL(nodesizesxp)[0]; //Rprintf("nodename:%s",nodename.c_str()); it_tM->second.members.insert(make_pair(nodesize,make_pair(nodeid,nodename))); //Rprintf("the node is: %s\n",nodename.c_str()); //Rprintf("the size of the node is: %f\n",nodesize); //Rprintf("the id of the node is: %d\n",nodeid); } p = i; } //return; int boots = LENGTH(bootClusters); SEXP memberssxp = VECTOR_ELT(bootClusters,0); int ltemp = LENGTH(memberssxp); vector<vector<int > > bootstraps = vector<vector<int >>(boots,vector<int>(ltemp));for(int i =0 ; i< boots;++i) { SEXP memberssxp = VECTOR_ELT(bootClusters,i); int l = LENGTH(memberssxp); int* members = INTEGER(memberssxp); //bootstraps[i] = new int[l]; for(int j = 0; j<l;++j) { bootstraps[i][j] = members[j]; //Rprintf("id: %d\n",members[j]); } } double confnew = REAL(conf)[0]; int Nnode = LENGTH(VECTOR_ELT(bootClusters,0)); MTRand *R = new MTRand(long(443)); // Set random seed vector<bool> significantVec = vector<bool>(Nnode); findConfCore(treeMap,bootstraps,significantVec,confnew,R); vector<pair<int,int> > mergers; findConfModules(treeMap,bootstraps,significantVec,mergers,confnew); SEXP name = VECTOR_ELT(gdiag,0); SEXP links = VECTOR_ELT(gdiag,2); Rprintf("teve"); int totalDegree = INTEGER(VECTOR_ELT(gdiag,4))[0]*2; string networkName = string(CHAR(STRING_ELT(name,0))); ofstream outfile; ostringstream oss; int nLinks = INTEGER(links)[0]; oss.str(""); oss << networkName << ".smap"; outfile.open(oss.str().c_str()); outfile << "# modules: " << l << endl; outfile << "# modulelinks: " << "na" << endl; outfile << "# nodes: " << Nnode << endl; outfile << "# links: " << nLinks << endl; outfile << "# codelength: " << "na" << endl; outfile << "*Undirected" << endl; outfile << "*Modules " << l << endl; int k = 0; map<int,int>::iterator iter = degrees.begin(); for(multimap<double,treeNode,greater<double> >::iterator it treeMap.begin(); it != treeMap.end(); it++){ outfile << k+1 << " \"" << it->second.members.begin()->second.second << ",...\" " << it->first << " " << (double)iter->second /(double)totalDegree<< endl; p++; k++; } outfile << "*Insignificants " << mergers.size() << endl; for(vector<pair<int,int> >::iterator it = mergers.begin(); it !mergers.end(); it++) outfile << it->first+1 << ">" << it->second+1 << endl; outfile << "*Nodes " << Nnode << endl; k = 1; for(multimap<double,treeNode,greater<double> >::iterator it treeMap.begin(); it != treeMap.end(); it++){ string s; s.append(to_string(k)); printSignificantTree(s,it,&outfile,significantVec); k++; } outfile << "*Links " << nLinks << endl; /* for(multimap<double,pair<int,int>,greater<double> >::iterator it sortedLinks.begin();it != sortedLinks.end();it++) outfile << it->second.first << " " << it->second.second << " " << 1.0*it->first << endl; */ outfile.close(); delete R; Rprintf("done"); R_FlushConsole(); R_ProcessEvents(); return; // Return Nothing. } } Now when I run R CMD check, in file 00install.out I am getting the following linking errors: * installing *source* package ‘DyA’ ... ** libs ** arch - g++ -I/usr/share/R/include -DNDEBUG -fpic -I -Wall -O3 -funroll-loops -pipe -c conf-infomap.cpp -o conf-infomap.o g++ -I/usr/share/R/include -DNDEBUG -fpic -I -Wall -O3 -funroll-loops -pipe -c GreedyBase.cpp -o GreedyBase.o g++ -I/usr/share/R/include -DNDEBUG -fpic -I -Wall -O3 -funroll-loops -pipe -c Greedy.cpp -o Greedy.o g++ -I/usr/share/R/include -DNDEBUG -fpic -I -Wall -O3 -funroll-loops -pipe -c Node.cc -o Node.o g++ conf-infomap.o GreedyBase.o Greedy.o Node.o mersenne.cpp stoc1.cpp userintf.cpp -lm -o conf-infomap /usr/lib/gcc/x86_64-linux-gnu/4.6.1/../../../x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' conf-infomap.o: In function `findConfCoreWrapper': conf-infomap.cpp:(.text+0xfd47): undefined reference to `LENGTH' conf-infomap.cpp:(.text+0xfdc2): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0xfdcf): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0xfde1): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0xfde9): undefined reference to `REAL' conf-infomap.cpp:(.text+0xfdf6): undefined reference to `LENGTH' conf-infomap.cpp:(.text+0xffc9): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0xffd6): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0xffe6): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0xfff6): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0x10006): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0x1000e): undefined reference to `INTEGER' conf-infomap.cpp:(.text+0x1001c): undefined reference to `INTEGER' conf-infomap.cpp:(.text+0x10071): undefined reference to `STRING_ELT' conf-infomap.cpp:(.text+0x10079): undefined reference to `R_CHAR' conf-infomap.cpp:(.text+0x10099): undefined reference to `REAL' conf-infomap.cpp:(.text+0x1022d): undefined reference to `LENGTH' conf-infomap.cpp:(.text+0x1023c): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0x10244): undefined reference to `LENGTH' conf-infomap.cpp:(.text+0x10638): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0x10643): undefined reference to `LENGTH' conf-infomap.cpp:(.text+0x1064e): undefined reference to `INTEGER' conf-infomap.cpp:(.text+0x10776): undefined reference to `REAL' conf-infomap.cpp:(.text+0x1078b): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0x10793): undefined reference to `LENGTH' conf-infomap.cpp:(.text+0x10aba): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0x10acc): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0x10add): undefined reference to `Rprintf' conf-infomap.cpp:(.text+0x10aec): undefined reference to `VECTOR_ELT' conf-infomap.cpp:(.text+0x10af4): undefined reference to `INTEGER' conf-infomap.cpp:(.text+0x10b03): undefined reference to `STRING_ELT' conf-infomap.cpp:(.text+0x10b0b): undefined reference to `R_CHAR' conf-infomap.cpp:(.text+0x10c17): undefined reference to `INTEGER' conf-infomap.cpp:(.text+0x11123): undefined reference to `Rprintf' conf-infomap.cpp:(.text+0x11128): undefined reference to `R_FlushConsole' conf-infomap.cpp:(.text+0x1112d): undefined reference to `R_ProcessEvents' conf-infomap.o: In function `RosvallDynamicSkeleton': conf-infomap.cpp:(.text+0x15133): undefined reference to `Rprintf' conf-infomap.cpp:(.text+0x15138): undefined reference to `R_FlushConsole' conf-infomap.cpp:(.text+0x1513e): undefined reference to `R_ProcessEvents' collect2: ld returned 1 exit status make: *** [conf-infomap] Error 1 Can anyone help, what am I doing wrong? I am using R 2.15.0 on ubuntu 11.10 Many thanks, Zalan [[alternative HTML version deleted]]
Dirk Eddelbuettel
2012-May-09 12:11 UTC
[Rd] R CMD check linking errors, when interfacing c++
On 9 May 2012 at 00:35, Zalan Szakolci wrote: | Hi there, | | I am trying to interface c++ code in R and make a package. With R CMD SHLIB | the dll was created, but when I try R CMD check, I am getting 'undefined | reference to..' linkage error messages. | | The relevant c++ source from conf-infomap.cpp: [...] | Now when I run R CMD check, in file 00install.out I am getting the | following linking errors: | | * installing *source* package ?DyA? ... | ** libs | ** arch - | g++ -I/usr/share/R/include -DNDEBUG -fpic -I -Wall -O3 -funroll-loops | -pipe -c conf-infomap.cpp -o conf-infomap.o | g++ -I/usr/share/R/include -DNDEBUG -fpic -I -Wall -O3 -funroll-loops | -pipe -c GreedyBase.cpp -o GreedyBase.o | g++ -I/usr/share/R/include -DNDEBUG -fpic -I -Wall -O3 -funroll-loops | -pipe -c Greedy.cpp -o Greedy.o | g++ -I/usr/share/R/include -DNDEBUG -fpic -I -Wall -O3 -funroll-loops | -pipe -c Node.cc -o Node.o | g++ conf-infomap.o GreedyBase.o Greedy.o Node.o mersenne.cpp stoc1.cpp | userintf.cpp -lm -o conf-infomap What is your src/Makevars file? This looks very wrong as you are obviously not linking against R itself. I would recommend a good long look at both the 'Writing R Extensions' manual as well as some of the existing CRAN packages uses C++ sources. You can (and should) also try R CMD COMPILE ... R CMD SHLIB ... R CMD INSTALL ... etc as 'R CMD check' is really only the final bit. Dirk -- R/Finance 2012 Conference on May 11 and 12, 2012 at UIC in Chicago, IL See agenda, registration details and more at http://www.RinFinance.com