在 Rcpp 包中加载共享对象

Load shared object in Rcpp package

我想用 Rcpp./libdynamic/dynamic.h 添加到我的包裹中。目录 libdynamic 嵌套在项目的文件夹 src 中。 当我尝试构建 R 包时,遇到 dyn.load 错误。

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

来源结构。

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

我在 Eclipse 中使用 Rcpp.hR.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

问题:

看起来当 R 构建包时它没有 link 以前编译的东西。 我如何强制 R 编译嵌套文件夹中的所有内容并 link 它?

如@dirk-eddelbuettel 所述:

Keep all files in src and everything works automatically.