dso15221 2016-01-25 11:52
浏览 325
已采纳

如何使用.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 2016-01-25 11:56
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大