C语言 如何使用.so文件运行c程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34991934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:19:40  来源:igfitidea点击:

How to run c program with .so file

cgccgoshared-librariesshared-objects

提问by NIket

I have gone through all the solutions on StackOverflow as well as Ask Ubuntu.

我已经浏览了 StackOverflow 和 Ask Ubuntu 上的所有解决方案。

I have a Go program:

我有一个围棋程序:

package main

import "C"

//export Getint
func Getint() int {
        return  2
}

func main() {}

and I have generated .sofile for the same with name t.so and header filet.h`

我已经生成.so了名称为t.so and header fileth` 的文件

Now I would like to use this function in my C program.
I have written the code but I don't know how to execute it.

现在我想在我的 C 程序中使用这个函数。
我已经写了代码,但我不知道如何执行它。

#include <stdio.h>
#include <t.h>
int main()
{
int a;
a=Getint();
printf("number : %d",a);
return 0;
}

When I execute it with

当我执行它时

gcc c.c t.so

it generates a.outfile

它生成a.out文件

but at the time of running a.outwith ./a.outit gives an error:

但在运行的时候a.out./a.out它给出了一个错误:

./a.out
Error while loading shared libraries: t.so: can not open shared object file: no such file or directory exists.

then I tried with:

然后我尝试:

gcc -c c.c -l t.so

So it generates c.ofile and it is not executable.

所以它生成c.o文件并且它是不可执行的。

采纳答案by dmi

Most probably your loader cannot find the library. Try to put the path to the directory where the libarry is located to LD_LIBRARY_PATHprior to run your binary.

很可能您的加载程序找不到库。LD_LIBRARY_PATH在运行二进制文件之前,尝试将路径放在 libarry 所在的目录中。

export LD_LIBRARY_PATH=/path/to/my/library
./a.out

回答by Some programmer dude

You should use the linker option-rpath, which tells the linker to add information in the executable program where to find runtime libraries like your .sofile.

您应该使用链接器选项-rpath,它告诉链接器在可执行程序中添加信息,在哪里可以找到像您的.so文件这样的运行时库。

This can be done using the GCC option -Wlwhich instructs the GCC frontend program to pass an option to the linker:

这可以使用 GCC 选项来完成,该选项-Wl指示 GCC 前端程序将选项传递给链接器:

$ gcc c.c t.so -Wl,-rpath=$(pwd)

This will pass -rpath=$(pwd)to the linker, and $(pwd)causes the shell to call the pwdcommand to return the current directory.

这将传递-rpath=$(pwd)给链接器,$(pwd)并使 shell 调用pwd命令以返回当前目录。

As long as you don't move the library the program should work.

只要您不移动库,程序就应该可以工作。



You canuse the environment variable LD_LIBRARY_PATHtoo, but it's not recommended.

也可以使用环境变量LD_LIBRARY_PATH,但不推荐使用

回答by Jean-Baptiste Yunès

You should use LD_LIBRARY_PATHto let the dynamic linker find your shared library in the list. Syntax is similar to PATHa list of directories separted by :.

您应该使用LD_LIBRARY_PATH让动态链接器在列表中找到您的共享库。语法类似于PATH由 分隔的目录列表:

On OSX this environment variable is called DYLD_LIBRARY_PATH.

在 OSX 上,这个环境变量被称为DYLD_LIBRARY_PATH.

回答by Hyman

.so files are shared object, meaning object that are available to all applications that need them.. that is, shared. Due to this characteristics, they need to be stored in a well known place. Also, they need to be indexed by the dynamic linker.

.so 文件是共享对象,意思是所有需要它们的应用程序都可以使用的对象......即共享。由于这种特性,它们需要存放在众所周知的地方。此外,它们需要由动态链接器索引。

In linux for instance you typically have a file /etc/ld.so.confwhere all directories where shared object are automatically read from are stored

例如,在 linux 中,您通常有一个文件/etc/ld.so.conf,其中存储了自动读取共享对象的所有目录

So your options are:

所以你的选择是:

  • Put your shared object file in a well known place
  • Put your shared object file in a place of your choice and let the dynamic linker know about it: in linux you can modify ld.so.confand run ldconfigto update ld indexes
  • As other suggested write the path of your .so in the env variable LD_LIBRARY_PATH (since dynamic linker reads it before running your application). This must be done at each environment creation
  • As other suggested use -rpath when compiling. Note that in this way you cannot move your .so file after the compilation
  • 把你的共享对象文件放在一个众所周知的地方
  • 将您的共享对象文件放在您选择的位置,并让动态链接器知道它:在 linux 中,您可以修改ld.so.conf并运行ldconfig以更新 ld 索引
  • 正如其他人建议在环境变量 LD_LIBRARY_PATH 中写入 .so 的路径(因为动态链接器在运行应用程序之前读取它)。这必须在每个环境创建时完成
  • 其他建议在编译时使用 -rpath。请注意,通过这种方式,您无法在编译后移动 .so 文件

Personally I prefer installing the .so file in a system library path

我个人更喜欢在系统库路径中安装 .so 文件