Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 使用动态库编译二进制文件_Linux_Gcc_Dynamic Linking - Fatal编程技术网

Linux 使用动态库编译二进制文件

Linux 使用动态库编译二进制文件,linux,gcc,dynamic-linking,Linux,Gcc,Dynamic Linking,我有20个共享对象文件,我想使用“a.so”中的一个函数 但我的问题是a.so链接到其他库。 当我编译main时,出现以下错误: /usr/bin/ld: warning: b.so, needed by /home/test/lib/a.so, not found (try using -rpath or -rpath-link) /usr/bin/ld: warning: c.so, needed by /home/test/lib/a.so, not found (try using -r

我有20个共享对象文件,我想使用“a.so”中的一个函数 但我的问题是a.so链接到其他库。 当我编译main时,出现以下错误:

/usr/bin/ld: warning: b.so, needed by /home/test/lib/a.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: c.so, needed by /home/test/lib/a.so, not found (try using -rpath or -rpath-link)
gcc: error: unrecognized command line option ‘-WL,’; did you mean ‘-Wa,’?
gcc: error: unrecognized command line option ‘-rpath,.’
我如何编译我的main,以便能够在a.so中使用该函数? 我尝试过使用这个方法:
gcc-L-WL-rpath,。main.c-l{libraryName}-o main
但我得到了以下错误:

/usr/bin/ld: warning: b.so, needed by /home/test/lib/a.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: c.so, needed by /home/test/lib/a.so, not found (try using -rpath or -rpath-link)
gcc: error: unrecognized command line option ‘-WL,’; did you mean ‘-Wa,’?
gcc: error: unrecognized command line option ‘-rpath,.’
当我编译main时,出现以下错误:

/usr/bin/ld: warning: b.so, needed by /home/test/lib/a.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: c.so, needed by /home/test/lib/a.so, not found (try using -rpath or -rpath-link)
gcc: error: unrecognized command line option ‘-WL,’; did you mean ‘-Wa,’?
gcc: error: unrecognized command line option ‘-rpath,.’
上述说法有两个错误:

  • 你会得到一个警告,而不是一个错误,然后
  • 链接程序时得到它,而不是编译程序时
我如何编译我的main,以便能够在a.so中使用该函数

像这样:

gcc -L. -Wl,-rpath=. main.c -l{libraryName} -o main
另外,一般来说,将
-rpath
作为相对路径是一种不好的做法,因为如果从其他目录调用该程序,它将无法工作。更好的解决方案是使用绝对
-rpath

gcc -L/home/test/lib -Wl,-rpath=/home/test/lib main.c -l{libraryName} -o main

我收到以下错误:gcc:error:unrecognized命令行选项'-rpath=/home/test/lib'@alpico您是对的:新版本的gcc拒绝“bare”
-rpath
。我已经更新了答案。