当前位置:网站首页>Load Objective-C at runtime
Load Objective-C at runtime
2022-07-19 14:47:00 【Fighting Horse】
When a program is only weakly dependent on a function , You can use dynamic libraries to organize dependent functions , And use the code to load the dynamic library and its functional modules in the main program , This method is called runtime loading .
about C/C++ Language , I believe many people know about runtime loading .
For example Win32 platform :
auto module = LoadLibrary(L"A.dll");
// void test(int)
auto testFunc = (void(*)(int)) GetProcAddress(module, "test");
In the class unix platform :
auto module = dlopen("A.so", RTLD_LAZY); // stay MacOSX On ,A.dylib
// void test(int)
auto testFunc = (void(*)(int)) dlsym(module, "test");
When there are many functions , You usually need to use C++ Class encapsulation , Using pure virtual interface classes can facilitate runtime loading . Just export a function that creates objects .
class StreamBase
{
virtual ~Base() = 0;
virtual Open(int) = 0;
virtual Read(char *, int) = 0;
virtual White(char const *, int) = 0;
virtual Close() = 0;
};
extern "C" StreamBase * CreateStream();When we're in MacOSX platform , Use Objective-C When the function is realized , We can realize runtime loading more conveniently . There is no need to deliberately introduce a pure virtual interface class , There is no need to export a creator function . in fact ,Objective-C What is exported is the class object itself . With class object , Everything will be there .
Let's take an example :
This is a player implementation , It is provided to the main program through the dynamic library
@interface Player : NSObject
+ (void) initialize;
- (instancetype) initWithDisplayLayer: (AVSampleBufferDisplayLayer*) layer;
- (instancetype) initWithImageView: (NSView*) view;
- (int) open: (char const *) url;
- (NSSize) videoSize;
- (int) play;
- (void) stop;
- (void) close;
@endIn the main program , After getting the class object , Everything works as usual
auto module = dlopen("libPlayer.dylib", RTLD_LAZY);
Class cls = (__bridge Class) dlsym(module, "OBJC_CLASS_$_Player");
if (cls == nullptr) {
m_error = -2;
return;
}
Player * player = [cls alloc];
[player initWithImageView: imageView];
Go and have a try !
边栏推荐
- Common built-in functions, iteratable objects, iterator objects, exception capture, purpose of exception capture, generator objects, modules, absolute and relative imports, package concepts, modules
- Cmake learning notes
- Alibaba微服务组件Nacos注册中心
- Explain the operation of C language file in detail
- Deployment principle
- FPGA(VGA协议实现)
- Which company is better in data filling and report presentation? Yixin ABI gives you the answer
- Problème de la valeur maximale de la fenêtre coulissante
- Prefix equality [DP | hash]
- dba
猜你喜欢

Cmake learning notes

智康护物业养老服务方案

Dr. Tao's lunar line reverses by 6.0

STM32 positioning hardfault location method and encountered situation in keil environment

ping 命令还能这么玩?

MySQL view

Qchartview overwrites the previous control when it is added in qgridlayout
![[port 3000 is already in use, solution to the problem of 3000 port being occupied]](/img/6f/6c8fdbc6b0b2794433c97e77185111.png)
[port 3000 is already in use, solution to the problem of 3000 port being occupied]

Redis

JVM performance optimization
随机推荐
ping 命令还能这么玩?
How to quickly realize Zadig single sign on on authoring?
End repeated development and personalize the login system in twoorthree times
DMA方式的特点
Use of token in ogg
CompositionAPI 组件开发范式
Matplotlib draw multi line graph (solve the problem that Matplotlib Chinese cannot be displayed)
Redis source code and design analysis -- 4 Jump table
Huawei wireless device configuration intelligent roaming
JVM performance optimization
QChartView添加在QGridLayout中时覆盖了之前的控件
Preview of authtalk phase I | comprehensive dismantling of multi tenant solutions
Redis
Can ping command still play like this?
Deployment principle
C - pointer
js刷题练习---牛客网
ospf 附录 防环 重发布
TDesign CompositionAPI 重构之路
Read the paper: temporary graph networks for deep learning on dynamic graphs