In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use dlopen and dlsym in C language". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use dlopen and dlsym in C language" can help you solve the problem.
Background
In order to decouple different logic, each business is generally encapsulated into a dynamic library, and then the main logic invokes each plug-in. The question here is, why do we use dlopen when we used to use include third-party header files and then connectors? Consider, for example, that we are going to use the sgemm function of the library cublas.
# include "cublas.h" int main () {cublas:: Mat a _ r b; cublas::sgemm (a _ r _ b);}
We know that cublas is provided by Nvidia, and people update the dynamic library every year. For example, after this year's update, the header file of the dynamic library has been changed to cublas_v2.h, and the function name has been changed to sgemm_v2. After such an operation, you not only have to upgrade the library, but also modify the code that has been online. If this sgemm function appears n times in your source code, it will be a disaster. But you can avoid this problem in the following ways:
/ / func.h#include # include # include / / if you know the exact information returned by the function, you can write the corresponding cublas_func below. # include extern std::once_flag cublas_dso_flag;extern void * cublas_dso_handle; struct DynLoad__add {template inline auto operator () (Args... Args)-> DECLARE_TYPE (add, args...) {using cublas_func = decltype (:: add (std::declval ()...)) (*) (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 (p_add) (args...) }}; extern DynLoad__add add;// func.cDynLoad__add add;// main.cc#include # include # include "func.h" int main () {add (2 Magne7)) As you can see from the above code, as long as you change the dynamic library road strength and function name of the func.h file each time, the other add functions don't need to be modified at all. It's really convenient. The above code refers to the source code of paddle: paddle/fluid/platform/dynload/cublas.hdemo production dynamic library int add (int a _ line int b) {return (a + b);} int sub (int a _ line int b) {return (a-b);}
Gcc-fPIC-shared caculate.c-o libcaculate.so
Call dlopen#include void * dlopen (const char * filename, int flag); char * dlerror (void); void * dlsym (void * handle, const char * symbol); int dlclose (void * handle)
Dlopen loads dynamic link libraries, and flag can set different modes (RTLD_LAZY suspends the decision, unscrambles symbols when necessary, and RTLD_NOW decides immediately to release all undecided symbols before returning.) Dlopen can return the handle of the dynamic library, and dlsym is to get the specific function name or variable name in the dynamic library. Dlopen closes the dynamic library.
# include # include # include typedef int (* FUNC) (int, int); int main () {void * handle; char * error; FUNC func = NULL; / / Open dynamic link library handle = dlopen (". / libcaculate.so", RTLD_LAZY); / / get a function * (void * *) (& func) = dlsym (handle, "add"); printf ("add:% d\ n", (* func) (2p7)) / / close the dynamic link library dlclose (handle);}
Gcc-rdynamic-o main main.c-ldl
This is the end of the introduction to "how to use dlopen and dlsym in C". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.