single_file_libs icon indicating copy to clipboard operation
single_file_libs copied to clipboard

Single file libs: What when you need C and ObjC?

Open cesss opened this issue 4 years ago • 7 comments

When your single-file C lib needs to use Objective C for accessing Mac-specific APIs but however it's 100% C in non-Mac OSs, how would you arrange the code? Would you still make it single-file? Or would you put all the Objective C in a separate file?

The horrible thing here is that if you use such a lib from a C program, you need to compile your C code as Objective-C when building on Mac.

What's the best way for doing this? (I wish I could invoke Cocoa from C, but it's not possible AFAIK)

cesss avatar May 06 '21 19:05 cesss

Unfortunately, I haven't used objective C in over 25 years, so I have no idea how the tooling works there, much less how to make something that compiles both ways.

nothings avatar May 06 '21 20:05 nothings

You'll need to compile as ObjC if you want to go down the single file route. I maintain a cross platform audio library which supports all the major platforms. It's written in C, but for iOS I just include ObjC directly within the C code and it just works, provided the user compiles the code with an ObjC compiler. Don't think there's any way around it these days since Apple seems to be moving further and further away from C.

mackron avatar May 06 '21 20:05 mackron

Thanks a lot!! Your approach in miniaudio is much better than the idea I had, because I was thinking in putting all the C code in a header, and all the ObjectiveC code in another source file (so, on non-Apple platforms you would use only the header, but on Apple you would use both the header and the ObjectiveC source)... but your approach is much better: just put everything in the header, and tell the user to compile it as ObjectiveC when in an Apple system.

BTW, miniaudio is great, I had in my todo list to find a minimal crossplatform audio lib for desktop+mobile, and I got two questions solved in the same thread: the single-file dilemma when in Apple systems, and the audio lib 😄

Also, BTW, do you know of any minimal single-file crossplatform desktop+mobile GLUT/GLFW alternative? (because writing it was what motivated my question, and if it's already done, I wouldn't reinvent the wheel)

cesss avatar May 07 '21 08:05 cesss

Yeah it's the only practical solution, and it's not really a big deal since all the tools just seamlessly work between C/C++/ObjC (in my experience at least). Not aware of a GLUT/GLFW alternative, but my glbind and vkbind projects can do the API loading for you if that's something you require.

mackron avatar May 07 '21 08:05 mackron

I just found that Sokol, apart from being a 3D API wrapper, also has another header lib for creating a window, so it can work as a GLUT/GLFW substitute with just one header. It seems the window creation code can be used independently from the gfx wrapper.

cesss avatar May 07 '21 10:05 cesss

Just use cc -ObjC file.c

r-lyeh avatar May 07 '21 15:05 r-lyeh

Also, I've used miniaudio + https://github.com/erkkah/tigr for a small windowing replacement. You get Windows/Linux/OSX out of the box. And Android as well with some extra effort, though.

r-lyeh avatar May 07 '21 16:05 r-lyeh