discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Objective-C runtime via FFI


From: Richard Frith-Macdonald
Subject: Re: Objective-C runtime via FFI
Date: Wed, 6 Mar 2024 16:50:41 +0000


> On 6 Mar 2024, at 04:02, Boris D. <borisd@gmx.com> wrote:
> 
> Hello, I am interested in interacting with the Objective-C runtime from other 
> languages through FFI bindings. 
> In particular, I need to be able to load Foundation or other classes 
> dynamically. What needs to be done to load these classes in the runtime? 
> Here's an example illustrating the issue I'm facing: 
> // clang `gnustep-config --objc-flags` `gnustep-config --objc-libs` simple.c 
> -o simple
> #import <Foundation/Foundation.h>
> int main (int argc, const char * argv[])
> {
> Class c = objc_getClass("NSObject");
> printf("Got a class: %s\n", c ? "pass" : "fail");
> return 0;
> }
> Currently it fails.

It may not be the only problem, but you are linking your program without 
NSObject, so you definitely wouldn't expect to be able to have 
objc_getClass("NSObject") return it.
NSObject is implemented in the base library, so any code that wants to use it 
would need to be linked with that library (see 'gnustep-config --base-libs).

I don't know if it's true nowadays, but I remember in the past the linker on 
some systems wouldn't actually 'pull in' a library unless your code used a 
symbol from the library, and since you are not cmpiling ObjectiveC code it 
won't be using the classes in the library, so linking with the base library may 
well not be enough.

If that's the case, then in theory you could use C dynamic library loading 
functions to exlicitly load the base library into your running program and 
register things with the runtime, but that's probably really complicated.

A simple solution would be to compile a small objective-C code fragment

eg
void setup_base()
{
  [NSObject class];
}

You could then have yur C code link with that and call the setup_base() 
function at the start.
That would ensure that the base library is linked into your code.
You could then use the NSBundle class to dynamically load other stuff.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]