首页 > 解决方案 > R包安装-gcc编译失败(制作共享对象时不能使用XX;用-fPIC重新编译)

问题描述

我正在nloptr通过 R 控制台安装软件包。

有一个流行的问题建议通过 apt using 安装 nloptr sudo apt-get install libnlopt-dev。我确实尝试过,但没有成功。如果您看到详细信息,您会发现安装程序实际上检测到了 nloptr 库configure: Suitable NLopt library found

但是,似乎有这个奇怪的错误不允许我安装库。我什至尝试从 tar.gz 包安装旧版本,但仍然是同样的错误。

> install.packages('nloptr')
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
trying URL 'https://cloud.r-project.org/src/contrib/nloptr_1.2.1.tar.gz'
Content type 'application/x-gzip' length 2448313 bytes (2.3 MB)
==================================================
downloaded 2.3 MB

* installing *source* package ‘nloptr’ ...
** package ‘nloptr’ successfully unpacked and MD5 sums checked
checking whether the C++ compiler works... yes
checking for C++ compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking how to run the C++ preprocessor... g++ -E
checking whether we are using the GNU C++ compiler... (cached) yes
checking whether g++ accepts -g... (cached) yes
checking for pkg-config... yes
configure: Now testing for NLopt header file.
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking nlopt.h usability... yes
checking nlopt.h presence... yes
checking for nlopt.h... yes
configure: Now testing for NLopt versison number.
configure: Suitable NLopt library found.
configure: creating ./config.status
config.status: creating src/Makevars
config.status: creating R/PkgFlags.R
** libs
g++  -I/usr/share/R/include -DNDEBUG      -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-AitvI6/r-base-3.4.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c dummy.cpp -o dummy.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG     -I/usr/local/include  -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-AitvI6/r-base-3.4.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c init_nloptr.c -o init_nloptr.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG     -I/usr/local/include  -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-AitvI6/r-base-3.4.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c nloptr.c -o nloptr.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o nloptr.so dummy.o init_nloptr.o nloptr.o -L/usr/local/lib -lnlopt -lm -L/usr/lib/R/lib -lR
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(general.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(optimize.o): relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(mt19937ar.o): relocation R_X86_64_TPOFF32 against `mti' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(timer.o): relocation R_X86_64_TPOFF32 against `start_inited.4227' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(cdirect.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(pnet.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(crs.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(mlsl.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(ccsa_quadratic.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(mma.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(cobyla.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(newuoa.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(nldrmd.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(sbplx.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(auglag.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(isres.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(slsqp.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(esch.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(redblack.o): relocation R_X86_64_32S against symbol `nil' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(sobolseq.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(DIRect.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libnlopt.a(DIRsubrout.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
/usr/share/R/share/make/shlib.mk:6: recipe for target 'nloptr.so' failed
make: *** [nloptr.so] Error 1
ERROR: compilation failed for package ‘nloptr’
* removing ‘/usr/local/lib/R/site-library/nloptr’

The downloaded source packages are in
    ‘/tmp/RtmpNXGKfk/downloaded_packages’
Warning message:
In install.packages("nloptr") :
  installation of package ‘nloptr’ had non-zero exit status

我提到了很多 SO 问题,但无法弄清楚到底该怎么做。此外,配置文件无限大,我不知道在哪里进行更改

标签: rlinuxg++shared-libraries

解决方案


感谢@JBGruber 的评论,我找到了另一种解决方法。

sudo apt-get install r-cran-nloptr


推荐阅读