macdylib的编写

源码:
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
macdylib的编写
构造函数2

macdylib的编写
析构
macdylib的编写

转载于:https://blog.51cto.com/haidragon/2378084

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值