欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1753|回復: 0
打印 上一主題 下一主題
收起左側

如何動態加載.so文件

[復制鏈接]
跳轉到指定樓層
樓主
ID:107189 發表于 2016-3-5 19:57 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
1:
libf1.so
#ifndef F1_HH_
#define F1_HH_
extern int func1(char *p);
#endif
#include <iostream>
using namespace std;
#ifdef __cplusplus
extern "C" {
#endif
int func1(char *p)
{
   if(p)
   {
        std::cout <<"func1: " ;
        std::cout <<p << endl;
   }
   return 1;
}
#ifdef __cplusplus
}
#endif
libf2.so
類似, 只是輸出不同
2: 編譯so
g++ f1.cpp -shared -fPIC -g -o libf1.so
g++ f2.cpp -shared -fPIC -g -o libf2.so
3: 應用程序
注冊信號、動態加載;收到信號后重新加載
幾個注意點:
a)  so的編譯
b)  #ifdef __cplusplus ; 防止找不到符號。 so的編譯器與應用程序的編譯器保持一致
c)  g++ -rdynamic -lf1 -g -o test main.cpp -ldl 編譯應用程序。 -lf1的意思是動態鏈接libf1.so     -ldl是為了使得可以動態加載libf2.so
4)  應用程序使用的so必需是通過符號鏈接到真實的so文件; 可以直接加載so,但是這種情況下so不能被修改(覆蓋),覆蓋時會程序core掉
view plaincopy to clipboardprint?
#include <stdio.h>   
#include <stdlib.h>   
#include <dlfcn.h>   
#include <signal.h>   
#include <iostream>   
#include <errno.h>   
#include "f1.h"   
  
int isreload = 0;   
  
void sig_show(int s)   
{   
    printf("catched signal: %d\n", s);   
    return;   
}   
  
void sig_reloadso(int s)   
{   
    printf("catched signal: %d\n", s);   
    isreload = 1;   
    printf("sigfunc isreload ? %d\n", isreload);   
    return;   
}   
  
int main(int argc, char *argv [])   
{   
    std::cout <<"main begin\n";   
  
    struct sigaction show;   
    show.sa_handler = &sig_show;   
    show.sa_flags = SA_NOMASK;   
    show.sa_restorer = 0;   
    if(sigaction(3, &show, 0) == -1)   
    {   
        printf("sigaction failed. errno: %d\n", errno);   
        return 0;   
    }   
  
  
    struct sigaction reload;   
    reload.sa_handler = &sig_reloadso;   
    reload.sa_flags = SA_NOMASK;   
    reload.sa_restorer = 0;   
    if(sigaction(4, &reload, 0) == -1)   
    {   
        printf("sigaction failed. errno: %d\n", errno);   
        return 0;   
    }   
  
  
    void *libf2;   
    int (*f2)(char *);   
  
    const static char * h = "hello";   
    char buf[200];   
  
    if((libf2 = dlopen("./libf2.so", RTLD_NOW | RTLD_GLOBAL)) != 0)   
    {   
        f2 = (int (*)(char *)) dlsym(libf2, "func2");   
        if(dlerror())   
        {   
            printf("error? %s\n", dlerror());   
        }   
    }   
    else  
    {   
        printf("can not open libf2.so\n");   
        return 0;   
    }   
  
    int i;   
  
    while(1)   
    {   
        printf("isreload ? %d\n", isreload);   
  
        if(isreload)    //test if need reload   
        {   
            dlclose(libf2);   
  
            if((libf2 = dlopen("./libf2.so", RTLD_LAZY | RTLD_GLOBAL)) != 0)   
            {   
                f2 = (int (*)(char *)) dlsym(libf2, "func2");   
                if(dlerror())   
                {   
                    printf("error? %s\n", dlerror());   
                    return 0;   
                }   
            }   
  
            isreload = 0;   
            printf("successfully reload libf2.so\n");   
        }   
  
        ++i;   
  
        sprintf(buf, "%s %d", h, i);      
  
        f2(buf);        //from f2   
  
        func1(buf);     //from f1   
  
        sleep(4);   
    }   
      
    return 0;   
}  


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表