dso15221
dso15221
2016-01-25 11:52

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

I have gone through all the solutions on stackoverflow as well as ask ubuntu.

I have a go program

package main

import "C"

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

func main() {}

and i have generated .so file for the same with name t.so and header file t.h

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.

#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.out file

but at the time of running a.out with ./a.out it gives error

./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.o file and it is not executable.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • dongqingchan2385 dongqingchan2385 5年前

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

    export LD_LIBRARY_PATH=/path/to/my/library
    ./a.out
    
    点赞 评论 复制链接分享
  • dongwen4630 dongwen4630 5年前

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

    On OSX this environment variable is called DYLD_LIBRARY_PATH.

    点赞 评论 复制链接分享
  • dongyuan6949 dongyuan6949 5年前

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

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

    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.conf and run ldconfig to 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

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

    点赞 评论 复制链接分享
  • dongyuli0964 dongyuli0964 5年前

    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 .so file.

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

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

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

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


    You can use the environment variable LD_LIBRARY_PATH too, but it's not recommended.

    点赞 评论 复制链接分享