linux下静态库.a与.o生成动态库.so

项目中需要用到时间同步,直接使用开源项目ntp中的ntpdate作为客户端来同步时间。
但是又不想直接使用ntpdate这个命令,而是以动态库的形式使用。

 

1 生成ntpdate.so
在编译ntpdate可执行程序时,可以看到ntpdate依赖libntp.a, version.o, ntpdate.o。
所以ntpdate.so需要由libntp.a, version.o, ntpdate.o生成。

 

1.1 Makefile

so:
	gcc -shared -o ntpdate.so ntpdate.o version.o -L. -lntp -lrt


2 ntpdate.so使用demo
2.1 demo.c

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int main(int argc, char **argv)
{
    void *handle;
    void (*callfun)();
    char *error;
    handle = dlopen("./ntpdate.so", RTLD_LAZY);
    if(!handle)
    {
        printf("[%s:%d] %s \n", __func__, __LINE__, dlerror());
        exit(1);
    }

    callfun=dlsym(handle,"ntpdatemain");
    if((error=dlerror())!=NULL)
    {
        printf("[%s:%d] %s \n"
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值