{"id":239105,"date":"2022-04-07T10:40:58","date_gmt":"2022-04-07T02:40:58","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=239105"},"modified":"2022-03-29T10:42:12","modified_gmt":"2022-03-29T02:42:12","slug":"dlopen-dlsym-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/dlopen-dlsym-linux.html","title":{"rendered":"C\u8bed\u8a00\u4e2ddlopen\u548cdlsym\u7684\u4f7f\u7528\u65b9\u5f0f\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u8be6\u7ec6\u4ecb\u7ecd\u4e86C\u8bed\u8a00\u4e2ddlopen\u548cdlsym\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u6587\u4e2d\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u4eec\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u4f60\u5e26\u6765\u5e2e\u52a9
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u80cc\u666f<\/strong><\/div>\n

\u4e3a\u4e86\u662f\u4e0d\u540c\u7684\u903b\u8f91\u89e3\u8026\uff0c\u4e00\u822c\u4f1a\u628a\u5404\u4e2a\u4e1a\u52a1\u5c01\u88c5\u6210\u52a8\u6001\u5e93\uff0c\u7136\u540e\u4e3b\u903b\u8f91\u53bb\u8c03\u7528\u5404\u4e2a\u63d2\u4ef6\u3002\u8fd9\u91cc\u6709\u4e2a\u95ee\u9898\u662f\uff0c\u4e3a\u4ec0\u4e48\u4ee5\u524d\u6211\u4eec\u90fd\u662f\u901a\u8fc7include\u7b2c\u4e09\u65b9\u7684\u5934\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7\u8fde\u63a5\u5668\u5b9e\u73b0\uff0c\u73b0\u5728\u5374\u8981\u5229\u7528dlopen\u5462\uff1f\u8003\u8651\u4ee5\u4e0b\u60c5\u51b5\uff0c\u6bd4\u5982\u6211\u4eec\u8981\u7528cublas\u8fd9\u4e2a\u5e93\u7684sgemm\u51fd\u6570\u3002<\/p>\n

#include \"cublas.h\"\r\nint main()\r\n{\r\n    cublas:: Mat a, b;\r\n    cublas::sgemm(a,b);\r\n}<\/pre>\n

\u6211\u4eec\u77e5\u9053cublas\u662f\u82f1\u4f1f\u8fbe\u63d0\u4f9b\u7684\uff0c\u4eba\u5bb6\u6bcf\u5e74\u90fd\u8981\u66f4\u65b0\u52a8\u6001\u5e93\u7684\uff0c\u6bd4\u5982\u4eca\u5e74\u66f4\u65b0\u540e\uff0c\u52a8\u6001\u5e93\u7684\u5934\u6587\u4ef6\u6539\u4e86cublas_v2.h, \u51fd\u6570\u540d\u6539\u4e3asgemm_v2, \u8fd9\u6837\u4e00\u987f\u64cd\u4f5c\u540e\uff0c\u4f60\u4e0d\u4ec5\u8981\u5347\u7ea7\u5e93\uff0c\u4e5f\u8981\u4fee\u6539\u5df2\u7ecf\u4e0a\u7ebf\u7684\u4ee3\u7801\uff0c\u5047\u5982\u8fd9\u4e2asgemm\u51fd\u6570\u5728\u4f60\u6e90\u7801\u4e2d\u51fa\u73b0\u4e86n\u591a\u6b21\uff0c\u8fd9\u5c06\u662f\u4e00\u4e2a\u707e\u96be\u3002\u4f46\u662f\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u4f60\u5c31\u53ef\u4ee5\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n

\/\/ func.h\r\n#include \r\n#include \r\n#include  \/\/ \u5982\u679c\u4f60\u77e5\u9053\u786e\u5207\u7684\u51fd\u6570\u8fd4\u56de\u4fe1\u606f\uff0c\u8fd9\u4e2a\u5bf9\u5e94\u4e0b\u9762\u7684cublas_func\u53ef\u4ee5\u81ea\u5df1\u5199\u3002\r\n#include \r\nextern std::once_flag cublas_dso_flag;\r\nextern void *cublas_dso_handle;\r\n  struct DynLoad__add {                                                \r\n    template                                              \r\n    inline auto operator()(Args... args) -> DECLARE_TYPE(add, args...) \r\n    { \r\n      using cublas_func =  decltype(::add(std::declval()...)) (*)(Args...);         \r\n      std::call_once(cublas_dso_flag, []() {                                \r\n        cublas_dso_handle = dlopen(\".\/libcublas.so\", RTLD_LAZY);       \r\n      });                                                                   \r\n      static void *p_add = dlsym(cublas_dso_handle, \"add\");          \r\n      return reinterpret_cast(p_add)(args...);            \r\n    }                                                                       \r\n  };                                                                        \r\n  extern DynLoad__add add;\r\n\/\/ func.c\r\nDynLoad__add add;\r\n\/\/ main.cc\r\n#include \r\n#include \r\n#include \"func.h\"\r\nint main()\r\n{\r\n    add(2,7));\r\n}<\/cublas_func><\/args><\/pre>\n

\u6839\u636e\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u5230\uff0c\u53ea\u8981\u4f60\u6bcf\u6b21\u4fee\u6539func.h\u6587\u4ef6\u7684\u52a8\u6001\u5e93\u8def\u52b2\u548c\u51fd\u6570\u540d\u5c31\u53ef\u4ee5\u4e86\uff0c\u5176\u4ed6\u7528\u5230\u7684add\u51fd\u6570\u6839\u672c\u4e0d\u9700\u8981\u518d\u53bb\u4fee\u6539\u3002\u771f\u662f\u5f88\u65b9\u4fbf\uff0c\u4e0a\u9762\u7684\u4ee3\u7801\u53c2\u8003paddle\u7684\u6e90\u7801\uff1apaddle\/fluid\/platform\/dynload\/cublas.h<\/p>\n

demo<\/strong><\/div>\n
\u751f\u4ea7\u52a8\u6001\u5e93<\/strong><\/span><\/div>\n
int add(int a,int b)\r\n{\r\n    return (a + b);\r\n}\r\nint sub(int a, int b)\r\n{\r\n    return (a - b);\r\n}<\/pre>\n
gcc -fPIC -shared caculate.c -o libcaculate.so<\/pre>\n
\u8c03\u7528dlopen<\/strong><\/span><\/div>\n
#include \r\nvoid *dlopen(const char *filename, int flag);\r\nchar *dlerror(void);\r\nvoid *dlsym(void *handle, const char *symbol);\r\nint dlclose(void *handle);<\/pre>\n

dlopen\u662f\u52a0\u8f7d\u52a8\u6001\u94fe\u63a5\u5e93\uff0cflag\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u7684\u6a21\u5f0f(RTLD_LAZY \u6682\u7f13\u51b3\u5b9a\uff0c\u7b49\u6709\u9700\u8981\u65f6\u518d\u89e3\u51fa\u7b26\u53f7, RTLD_NOW \u7acb\u5373\u51b3\u5b9a\uff0c\u8fd4\u56de\u524d\u89e3\u9664\u6240\u6709\u672a\u51b3\u5b9a\u7684\u7b26\u53f7\u3002), dlopen\u53ef\u4ee5\u8fd4\u56de\u52a8\u6001\u5e93\u7684\u53e5\u67c4\uff0cdlsym\u662f\u83b7\u53d6\u52a8\u6001\u5e93\u4e2d\u7684\u5177\u4f53\u51fd\u6570\u540d\u6216\u8005\u53d8\u91cf\u540d\u3002dlopen\u662f\u5173\u95ed\u52a8\u6001\u5e93\u3002<\/p>\n

#include \r\n#include \r\n#include \r\ntypedef int (*FUNC)(int, int);\r\nint main()\r\n{\r\n    void *handle;\r\n    char *error;\r\n    FUNC func = NULL;\r\n    \/\/\u6253\u5f00\u52a8\u6001\u94fe\u63a5\u5e93\r\n    handle = dlopen(\".\/libcaculate.so\", RTLD_LAZY);\r\n    \/\/\u83b7\u53d6\u4e00\u4e2a\u51fd\u6570\r\n    *(void **) (&func) = dlsym(handle, \"add\");\r\n    printf(\"add: %d\\n\", (*func)(2,7));\r\n    \/\/\u5173\u95ed\u52a8\u6001\u94fe\u63a5\u5e93\r\n    dlclose(handle);\r\n}<\/pre>\n
gcc -rdynamic -o main main.c -ldl<\/pre>\n

\u672c\u7bc7\u6587\u7ae0\u5c31\u5230\u8fd9\u91cc\u4e86\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u4f60\u5e26\u6765\u5e2e\u52a9.<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4e3a\u4e86\u662f\u4e0d\u540c\u7684\u903b\u8f91\u89e3\u8026\uff0c\u4e00\u822c\u4f1a\u628a\u5404\u4e2a\u4e1a\u52a1\u5c01\u88c5\u6210\u52a8\u6001\u5e93\uff0c\u7136\u540e\u4e3b\u903b\u8f91\u53bb\u8c03\u7528\u5404\u4e2a\u63d2\u4ef6\u3002\u8fd9\u91cc\u6709\u4e2a\u95ee\u9898\u662f\uff0c\u4e3a\u4ec0\u4e48\u4ee5\u524d\u6211\u4eec\u90fd […]<\/p>\n","protected":false},"author":362,"featured_media":238023,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-239105","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=239105"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239105\/revisions"}],"predecessor-version":[{"id":239119,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/239105\/revisions\/239119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/238023"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=239105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=239105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=239105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}