源码: https://github.com/haidragon/mydylib 创建一个dylib项目 源码: main.m
//
// testdylib.m
// testdylib
//
// Created by haidragon on 2019/4/3.
// Copyright © 2019 haidragon. All rights reserved.
//
#import "testdylib.h"
#import <Cocoa/Cocoa.h>
@implementation testdylib : NSObject
@end
__attribute__((constructor)) void DylibMain1(){
NSAlert *alert = [NSAlert alertWithMessageText:@"MessageBox"
defaultButton:@"defaultButton"
alternateButton:@"alternateButton"
otherButton:@"otherButton"
informativeTextWithFormat:@"DylibMain1"];
NSUInteger action = [alert runModal];
//响应window的按钮事件
// if(action == NSAlertDefaultReturn)
// {
// NSLog(@"defaultButton clicked!");
// }
// else if(action == NSAlertAlternateReturn )
// {
// NSLog(@"alternateButton clicked!");
// }
// else if(action == NSAlertOtherReturn)
// {
// NSLog(@"otherButton clicked!");
// }
}
__attribute__((constructor)) void DylibMain2(){
NSAlert *alert = [NSAlert alertWithMessageText:@"MessageBox"
defaultButton:@"defaultButton"
alternateButton:@"alternateButton"
otherButton:@"otherButton"
informativeTextWithFormat:@"DylibMain2"];
NSUInteger action = [alert runModal];
//响应window的按钮事件
// if(action == NSAlertDefaultReturn)
// {
// NSLog(@"defaultButton clicked!");
// }
// else if(action == NSAlertAlternateReturn )
// {
// NSLog(@"alternateButton clicked!");
// }
// else if(action == NSAlertOtherReturn)
// {
// NSLog(@"otherButton clicked!");
// }
}
void __attribute__ ((destructor)) unload(void){
NSAlert *alert = [NSAlert alertWithMessageText:@"MessageBox"
defaultButton:@"defaultButton"
alternateButton:@"alternateButton"
otherButton:@"otherButton"
informativeTextWithFormat:@"unload"];
NSUInteger action = [alert runModal];
//响应window的按钮事件
// if(action == NSAlertDefaultReturn)
// {
// NSLog(@"defaultButton clicked!");
// }
// else if(action == NSAlertAlternateReturn )
// {
// NSLog(@"alternateButton clicked!");
// }
// else if(action == NSAlertOtherReturn)
// {
// NSLog(@"otherButton clicked!");
// }
}
创建加载程序 源码 exe.m
//
// main.m
// testexe
//
// Created by haidragon on 2019/4/3.
// Copyright © 2019 haidragon. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#include <dlfcn.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
void* handle = dlopen("/Users/haidragon/Library/Developer/Xcode/DerivedData/testdylib-fgxcocyqbpxowtbcuglxlehxutzv/Build/Products/Debug/libtestdylib.dylib", RTLD_LAZY);
printf("%x",handle);
// insert code here...
NSLog(@"Hello, World!");
// NSAlert *alert = [NSAlert alertWithMessageText:@"messageText"
// defaultButton:@"defaultButton"
// alternateButton:@"alternateButton"
// otherButton:@"otherButton"
// informativeTextWithFormat:@"informativeText"];
//
// NSUInteger action = [alert runModal];
// //响应window的按钮事件
// if(action == NSAlertDefaultReturn)
// {
// NSLog(@"defaultButton clicked!");
// }
// else if(action == NSAlertAlternateReturn )
// {
// NSLog(@"alternateButton clicked!");
// }
// else if(action == NSAlertOtherReturn)
// {
// NSLog(@"otherButton clicked!");
// }
}
return 0;
}
运行程序 构造函数1 构造函数2
析构