How to #import <NSObjCRuntime.h> to use objc_msgSend
Asked Answered
O

3

38

I'd like to import runtime's header to use objc_msgSend but I'm getting:

error: NSObjCRuntime.h: No such file or directory

Should I add something to the header search path?

Orient answered 4/2, 2011 at 9:40 Comment(0)
K
61

You need to include <objc/message.h> (you'll find the related headers in /usr/include/objc) and link to the objc (/usr/lib/libobjc.dylib) library.

Kerouac answered 4/2, 2011 at 9:56 Comment(1)
It worked! Can you share where did you find that? I've browsed the apple docs, google code search, and nothing !Orient
S
38

#import <Foundation/NSObjCRuntime.h> does work

but you probably need

#import <objc/runtime.h>

like this Apple example does

upd: since iOS 7 #import <Foundation/NSObjCRuntime.h> replaced to #import <objc/NSObjCRuntime.h> but i recommend to use #import <objc/runtime.h> anyway

Sterigma answered 9/10, 2011 at 1:42 Comment(1)
The method objc_msgSend is declared in <objc/message.h>, not in <objc/runtime.h>. So you will get a warning for implicitly declaring library function.Westfalen
W
2

When using Xcode 6 and later, you will get an error after #include<objc/message.h>. It can be resolved like this

#include <objc/message.h>
void foo(void *object) {
  typedef void (*send_type)(id, SEL, int);
  send_type func = (send_type)objc_msgSend;
  func(object, sel_getUid("foo:"), 5);
}

http://devstreaming.apple.com/videos/wwdc/2014/417xx2zsyyp8zcs/417/417_whats_new_in_llvm.pdf

Westfalen answered 28/9, 2015 at 5:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.