簡體   English   中英

在 Centos7 上將 static 庫與 gcc 鏈接不起作用

[英]Linking a static library with gcc on Centos7 not working

我有一個 Java JNI 項目,它使用以下命令在 Mac OS 上構建

gcc -g -I/my-project-dir -o libmylibrary.jnilib -lc -shared -I"${JAVA_HOME}"/include -I"${JAVA_HOME}"/include/darwin -L ./quickjs-2021-03-27 -l quickjs /my-project-dir/Native.c

其中/my-project-dir包含Native.c使用的文件Native.h quickjs-2021-03-27是我下載、解壓縮並運行make install這個開源項目/庫,它生成了/quickjs-2021-03-27/libquickjs.a 它在 MacOS 上完美運行並成功構建/鏈接。

然后我嘗試在 Centos7 中運行以下代碼

gcc -v -Wall -Wl,-no-undefined -fPIC -I/my-project-dir -o libmylibrary.so -lc -shared -I"${JAVA_HOME}"/include -I"${JAVA_HOME}"/include/linux -L ./quickjs-2021-03-27 -l quickjs /my-project-dir/Native.c

我收到以下錯誤:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-7/root/usr --mandir=/opt/rh/devtoolset-7/root/usr/share/man --infodir=/opt/rh/devtoolset-7/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --with-default-libstdcxx-abi=gcc4-compatible --with-isl=/builddir/build/BUILD/gcc-7.3.1-20180303/obj-x86_64-redhat-linux/isl-install --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-g' '-fPIC' '-I' '/my-project-dir' '-o' 'libmylibrary.so' '-shared' '-I' '/usr/java/jdk-16//include' '-I' '/usr/java/jdk-16//include/linux' '-L./quickjs-2021-03-27' '-mtune=generic' '-march=x86-64'
 /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/cc1 -quiet -v -I /my-project-dir -I /usr/java/jdk-16//include -I /usr/java/jdk-16//include/linux /my-project-dir/Native.c -quiet -dumpbase Native.c -mtune=generic -march=x86-64 -auxbase QuickJSTranspiler_Linux -g -version -fPIC -o /tmp/ccmFWDe5.s
GNU C11 (GCC) version 7.3.1 20180303 (Red Hat 7.3.1-5) (x86_64-redhat-linux)
    compiled by GNU C version 7.3.1 20180303 (Red Hat 7.3.1-5), GMP version 6.0.0, MPFR version 3.1.1, MPC version 1.0.1, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/include-fixed"
ignoring nonexistent directory "/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /my-project-dir
 /usr/java/jdk-16//include
 /usr/java/jdk-16//include/linux
 /opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/include
 /usr/local/include
 /opt/rh/devtoolset-7/root/usr/include
 /usr/include
End of search list.
GNU C11 (GCC) version 7.3.1 20180303 (Red Hat 7.3.1-5) (x86_64-redhat-linux)
    compiled by GNU C version 7.3.1 20180303 (Red Hat 7.3.1-5), GMP version 6.0.0, MPFR version 3.1.1, MPC version 1.0.1, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 9297c6e7ca8f6c9f2474dcdc9331bbe8
COLLECT_GCC_OPTIONS='-v' '-g' '-fPIC' '-I' '/my-project-dir' '-o' 'libmylibrary.so' '-shared' '-I' '/usr/java/jdk-16//include' '-I' '/usr/java/jdk-16//include/linux' '-L./quickjs-2021-03-27' '-mtune=generic' '-march=x86-64'
 /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/as -v -I /my-project-dir -I /usr/java/jdk-16//include -I /usr/java/jdk-16//include/linux --64 -o /tmp/ccwWcjln.o /tmp/ccmFWDe5.s
GNU assembler version 2.28 (x86_64-redhat-linux) using BFD version version 2.28-11.el7
COMPILER_PATH=/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/:/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/:/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/:/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/:/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/:/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-g' '-fPIC' '-I' '/my-project-dir' '-o' 'libmylibrary.so' '-shared' '-I' '/usr/java/jdk-16//include' '-I' '/usr/java/jdk-16//include/linux' '-L./quickjs-2021-03-27' '-mtune=generic' '-march=x86-64'
 /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/collect2 -plugin /opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/liblto_plugin.so -plugin-opt=/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/lto-wrapper -plugin-opt=-fresolution=/tmp/ccY5tQIG.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -shared -o libmylibrary.so /lib/../lib64/crti.o /opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/crtbeginS.o -L./quickjs-2021-03-27 -L/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7 -L/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/../../.. -no-undefined -lc -lquickjs /tmp/ccwWcjln.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /opt/rh/devtoolset-7/root/usr/lib/gcc/x86_64-redhat-linux/7/crtendS.o /lib/../lib64/crtn.o
/tmp/ccwWcjln.o: In function `JS_ToCString':
/usr/local/include/quickjs/quickjs.h:707: undefined reference to `JS_ToCStringLen2'
/tmp/ccwWcjln.o: In function `JS_NewCustomContext':
/my-project-dir/Native.c:205395: undefined reference to `JS_NewContextRaw'
/my-project-dir/Native.c:205398: undefined reference to `JS_AddIntrinsicBaseObjects'
/my-project-dir/Native.c:205399: undefined reference to `JS_AddIntrinsicDate'
/my-project-dir/Native.c:205400: undefined reference to `JS_AddIntrinsicEval'
/my-project-dir/Native.c:205401: undefined reference to `JS_AddIntrinsicStringNormalize'
/my-project-dir/Native.c:205402: undefined reference to `JS_AddIntrinsicRegExp'
/my-project-dir/Native.c:205403: undefined reference to `JS_AddIntrinsicJSON'
/my-project-dir/Native.c:205404: undefined reference to `JS_AddIntrinsicProxy'
/my-project-dir/Native.c:205405: undefined reference to `JS_AddIntrinsicMapSet'
/my-project-dir/Native.c:205406: undefined reference to `JS_AddIntrinsicTypedArrays'
/my-project-dir/Native.c:205407: undefined reference to `JS_AddIntrinsicPromise'
/my-project-dir/Native.c:205408: undefined reference to `JS_AddIntrinsicBigInt'
/tmp/ccwWcjln.o: In function `Java_com_glide_script_TranspilerEngine_transpile0':
/my-project-dir/Native.c:205419: undefined reference to `JS_NewRuntime'
/my-project-dir/Native.c:205421: undefined reference to `js_std_set_worker_new_context_func'
/my-project-dir/Native.c:205422: undefined reference to `js_std_init_handlers'
/my-project-dir/Native.c:205424: undefined reference to `js_module_loader'
/my-project-dir/Native.c:205424: undefined reference to `JS_SetModuleLoaderFunc'
/my-project-dir/Native.c:205428: undefined reference to `js_std_eval_binary'
/my-project-dir/Native.c:205430: undefined reference to `JS_Eval'
/my-project-dir/Native.c:205433: undefined reference to `js_std_loop'
/my-project-dir/Native.c:205434: undefined reference to `JS_FreeContext'
/my-project-dir/Native.c:205435: undefined reference to `JS_FreeRuntime'
collect2: error: ld returned 1 exit status

但是,如果我在./quickjs-2021-03-27中執行ls ,我可以清楚地看到那里有編譯所需的libquickjs.aquickjs.hquickjs-libc.h文件。

任何幫助將不勝感激。

更新:

我現在實際上將命令更改為:

gcc -g -o libquickjs.so -fPIC -shared /my-project-dir/Native.c -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -I./quickjs-2021-03-27 -Wl,-Bstatic -L./quickjs-2021-03-27 -lquickjs -Wl,-Bdynamic -ldl -lpthread

現在得到以下錯誤:

/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /usr/local/lib/quickjs/libquickjs.a(quickjs.nolto.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /usr/local/lib/quickjs/libquickjs.a(libregexp.nolto.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /usr/local/lib/quickjs/libquickjs.a(libunicode.nolto.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /usr/local/lib/quickjs/libquickjs.a(cutils.nolto.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /usr/local/lib/quickjs/libquickjs.a(quickjs-libc.nolto.o): relocation R_X86_64_32 against symbol `js_realloc_rt' can not be used when making a shared object; recompile with -fPIC
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: /usr/local/lib/quickjs/libquickjs.a(libbf.nolto.o): relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

我能夠使用以下命令進行編譯:

gcc -g -Wall -Wl,-no-undefined -fPIC -I/my-project-dir /my-project-dir/Native.c -o libmylibrary.so -lc -shared -I"${JAVA_HOME}"/include -I"${JAVA_HOME}"/include/linux -L ./quickjs-2021-03-27 -l quickjs -lpthread -lm -ldl

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM