2

I want to include ./libdynamic/dynamic.h to my package with Rcpp. Directory libdynamic is nested in the project's folder src. While I am trying to build a R package, I face dyn.load error.

Error in dyn.load(file, DLLpath = DLLpath, ...) : 
      unable to load shared object

Sources structure.

.:
total 60K
-rw-rw-r-- 1 1.2K Dec 29 10:17 essential.h
drwxrwxr-x 3 4.0K Dec 28 19:24 libdynamic
-rw-rw-r-- 1 1.8K Dec 28 18:34 main.cpp
-rw-rw-r-- 1   45 Dec 29 09:59 Makevars
-rw-rw-r-- 1 1.1K Dec 28 20:17 RcppExports.cpp
-rw-rw-r-- 1    0 Dec 28 12:12 README
-rw-rw-r-- 1 4.6K Dec 29 09:49 Rulib.cpp
-rw-rw-r-- 1  335 Dec 28 13:55 structs.cpp
-rw-rw-r-- 1  22K Dec 28 19:24 structs.o

./libdynamic:
total 8.0K
drwxrwxr-x 2 4.0K Dec 28 15:49 dynamic
-rw-rw-r-- 1  453 Dec 29 10:12 dynamic.h

./libdynamic/dynamic:
total 60K
-rw-rw-r-- 1 2.1K Dec 28 15:49 buffer.c
-rw-rw-r-- 1  746 Dec 28 15:49 buffer.h
-rw-rw-r-- 1 5.4K Dec 28 15:49 cfarmhash.c
-rw-rw-r-- 1  136 Dec 28 15:49 cfarmhash.h
-rw-rw-r-- 1 4.1K Dec 28 15:49 mapi.c
-rw-rw-r-- 1 1.7K Dec 28 15:49 mapi.h
-rw-rw-r-- 1 5.1K Dec 28 15:49 maps.c
-rw-rw-r-- 1 1.5K Dec 28 15:49 maps.h
-rw-rw-r-- 1 3.7K Dec 28 15:49 string.c
-rw-rw-r-- 1 1.2K Dec 28 15:49 string.h
-rw-rw-r-- 1 2.3K Dec 28 15:49 vector.c
-rw-rw-r-- 1 1016 Dec 28 15:49 vector.h

I have made successful compilation in Eclipse with Rcpp.h and R.h:

gcc -O0 -g3 -Wall -c -fmessage-length=0 -o libdynamic/dynamic/string.o ../libdynamic/dynamic/string.c 
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o libdynamic/dynamic/cfarmhash.o ../libdynamic/dynamic/cfarmhash.c 
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o libdynamic/dynamic/mapi.o ../libdynamic/dynamic/mapi.c 
../libdynamic/dynamic/mapi.c: In function ‘mapi_rehash’:
../libdynamic/dynamic/mapi.c:212:7: warning: implicit declaration of function ‘aligned_alloc’ [-Wimplicit-function-declaration]
       nm.objects = (char *) aligned_alloc(64, MAPI_ROUNDUP(c * nm.object_size, 64));
       ^
../libdynamic/dynamic/mapi.c:212:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
       nm.objects = (char *) aligned_alloc(64, MAPI_ROUNDUP(c * nm.object_size, 64));
                    ^
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o libdynamic/dynamic/maps.o ../libdynamic/dynamic/maps.c 
g++ -D__cplusplus=201103L -I/home/cracs/workspace/R-ulib-wrapper/libdynamic -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/share/R/include -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -o structs.o ../structs.cpp 
g++ -D__cplusplus=201103L -I/home/cracs/workspace/R-ulib-wrapper/libdynamic -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/share/R/include -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -o main.o ../main.cpp 
g++ -D__cplusplus=201103L -I/home/cracs/workspace/R-ulib-wrapper/libdynamic -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/share/R/include -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -o Rulib.o ../Rulib.cpp 
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o libdynamic/dynamic/vector.o ../libdynamic/dynamic/vector.c 
g++ -D__cplusplus=201103L -I/home/cracs/workspace/R-ulib-wrapper/libdynamic -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/share/R/include -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -o RcppExports.o ../RcppExports.cpp 
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o libdynamic/dynamic/buffer.o ../libdynamic/dynamic/buffer.c 
g++ -L/usr/local/lib/R/site-library/Rcpp/libs -L/usr/lib/R/lib -o R-ulib-wrapper structs.o main.o libdynamic/dynamic/vector.o libdynamic/dynamic/string.o libdynamic/dynamic/maps.o libdynamic/dynamic/mapi.o libdynamic/dynamic/cfarmhash.o libdynamic/dynamic/buffer.o Rulib.o RcppExports.o -lR 

R CMD check

* installing *source* package ‘Rulib’ ...
** libs
g++ -I/usr/share/R/include -DNDEBUG -I/usr/local/lib/R/site-library/Rcpp/include -I../src/libdynamic -std=c++0x  -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c RcppExports.cpp -o RcppExports.o
g++ -I/usr/share/R/include -DNDEBUG -I/usr/local/lib/R/site-library/Rcpp/include -I../src/libdynamic -std=c++0x  -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c Rulib.cpp -o Rulib.o
g++ -I/usr/share/R/include -DNDEBUG -I/usr/local/lib/R/site-library/Rcpp/include -I../src/libdynamic -std=c++0x  -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c main.cpp -o main.o
g++ -I/usr/share/R/include -DNDEBUG -I/usr/local/lib/R/site-library/Rcpp/include -I../src/libdynamic -std=c++0x  -I"/usr/local/lib/R/site-library/Rcpp/include"   -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c structs.cpp -o structs.o
g++ -shared -Wl,-Bsymbolic-functions -Wl,-z,relro -o Rulib.so RcppExports.o Rulib.o main.o structs.o -L/usr/lib/R/lib -lR
installing to /home/cracs/src/Rulib.Rcheck/Rulib/libs
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/home/cracs/src/Rulib.Rcheck/Rulib/libs/Rulib.so':
  /home/cracs/src/Rulib.Rcheck/Rulib/libs/Rulib.so: undefined symbol: maps_begin
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/home/cracs/src/Rulib.Rcheck/Rulib’

Makevars:

PKG_CPPFLAGS+=-I../src/libdynamic -std=c++0x

Question:

Looks like when R building package it doesn't link previously compiled stuff. How I can force R to compile everything in nested folders and link it?

Cron Merdek
  • 1,084
  • 1
  • 14
  • 25
  • 2
    Look at existing packages with subdirectories in `src/` and see how they do it. – Dirk Eddelbuettel Dec 29 '15 at 12:24
  • I have went through [RcppGraphChi](https://github.com/thirdwing/RcppGraphChi/tree/master/src) and [rcppbugs](https://github.com/armstrtw/rcppbugs/tree/master/src). Both have similar setup. I have already done the same as they did with includes `-I` in `Makevars`. The only difference we have is that i include `.h` file, whether they include `.hpp`. Is it the reason? – Cron Merdek Dec 29 '15 at 13:17
  • 2
    I think you *completely* underestimate what you are up to. Look eg at package Matrix; you need to instruct _R_ to compile in subdirectories as well. (Your Eclipse mention is meaningless; Eclipse is not a compiler; R does not use CMake.) But you cannot expect me to teach you this one-on-one. Study the existing examples. It can be done. Else copy everything into `src/` and call it a day. – Dirk Eddelbuettel Dec 29 '15 at 13:19
  • 1
    And for what it is worth I think both examples you cite are inapplicable as they appear to be _header-only_ and do not involve compilation. Which is the sticky bit tripping you up. – Dirk Eddelbuettel Dec 29 '15 at 13:35
  • @DirkEddelbuettel thx Dirk. I just didn't expect that complexity in orginizing R packages. – Cron Merdek Dec 29 '15 at 13:42
  • 3
    Well it is you imposing the complexity. Keep all files in `src` and everything works automatically. – Dirk Eddelbuettel Dec 29 '15 at 13:57

1 Answers1

0

As mentioned by @dirk-eddelbuettel:

Keep all files in src and everything works automatically.

Cron Merdek
  • 1,084
  • 1
  • 14
  • 25