日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C語言中dlopen和dlsym的使用方式詳解_C 語言

作者:s.feng ? 更新時間: 2022-05-29 編程語言

背景

為了是不同的邏輯解耦,一般會把各個業(yè)務(wù)封裝成動態(tài)庫,然后主邏輯去調(diào)用各個插件。這里有個問題是,為什么以前我們都是通過include第三方的頭文件,然后通過連接器實(shí)現(xiàn),現(xiàn)在卻要利用dlopen呢?考慮以下情況,比如我們要用cublas這個庫的sgemm函數(shù)。

#include "cublas.h"
int main()
{
	cublas:: Mat a, b;
	cublas::sgemm(a,b);
}

我們知道cublas是英偉達(dá)提供的,人家每年都要更新動態(tài)庫的,比如今年更新后,動態(tài)庫的頭文件改了cublas_v2.h, 函數(shù)名改為sgemm_v2, 這樣一頓操作后,你不僅要升級庫,也要修改已經(jīng)上線的代碼,假如這個sgemm函數(shù)在你源碼中出現(xiàn)了n多次,這將是一個災(zāi)難。但是通過下面的方式你就可以避免這個問題:

// func.h
#include <stdio.h>
#include <stdlib.h>
#include <cublas_v2.h> // 如果你知道確切的函數(shù)返回信息,這個對應(yīng)下面的cublas_func可以自己寫。
#include <dlfcn.h>
extern std::once_flag cublas_dso_flag;
extern void *cublas_dso_handle;
  struct DynLoad__add {                                                
    template <typename... Args>                                             
    inline auto operator()(Args... args) -> DECLARE_TYPE(add, args...) 
    { 
      using cublas_func =  decltype(::add(std::declval<Args>()...)) (*)(Args...);         
      std::call_once(cublas_dso_flag, []() {                                
        cublas_dso_handle = dlopen("./libcublas.so", RTLD_LAZY);       
      });                                                                   
      static void *p_add = dlsym(cublas_dso_handle, "add");          
      return reinterpret_cast<cublas_func>(p_add)(args...);            
    }                                                                       
  };                                                                        
  extern DynLoad__add add;
// func.c
DynLoad__add add;
// main.cc
#include <stdio.h>
#include <stdlib.h>
#include "func.h"
int main()
{
    add(2,7));
}
根據(jù)上面的代碼可以看到,只要你每次修改func.h文件的動態(tài)庫路勁和函數(shù)名就可以了,其他用到的add函數(shù)根本不需要再去修改。真是很方便,上面的代碼參考paddle的源碼:paddle/fluid/platform/dynload/cublas.h

demo

生產(chǎn)動態(tài)庫

int add(int a,int b)
{
    return (a + b);
}
int sub(int a, int b)
{
    return (a - b);
}

gcc -fPIC -shared caculate.c -o libcaculate.so

調(diào)用dlopen

#include <dlfcn.h>
void *dlopen(const char *filename, int flag);
char *dlerror(void);
void *dlsym(void *handle, const char *symbol);
int dlclose(void *handle);

dlopen是加載動態(tài)鏈接庫,flag可以設(shè)置不同的模式(RTLD_LAZY 暫緩決定,等有需要時再解出符號, RTLD_NOW 立即決定,返回前解除所有未決定的符號。), dlopen可以返回動態(tài)庫的句柄,dlsym是獲取動態(tài)庫中的具體函數(shù)名或者變量名。dlopen是關(guān)閉動態(tài)庫。

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
typedef int (*FUNC)(int, int);
int main()
{
    void *handle;
    char *error;
    FUNC func = NULL;
    //打開動態(tài)鏈接庫
    handle = dlopen("./libcaculate.so", RTLD_LAZY);
    //獲取一個函數(shù)
    *(void **) (&func) = dlsym(handle, "add");
    printf("add: %d\n", (*func)(2,7));
    //關(guān)閉動態(tài)鏈接庫
    dlclose(handle);
}

gcc -rdynamic -o main main.c -ldl

總結(jié)

原文鏈接:https://blog.csdn.net/feng__shuai/article/details/123760587

欄目分類
最近更新