nothings / single_file_libs

List of single-file C/C++ libraries.
9.01k stars 593 forks source link

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

Open cesss opened 3 years ago

cesss commented 3 years ago

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)

nothings commented 3 years ago

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.

mackron commented 3 years ago

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.

cesss commented 3 years ago

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)

mackron commented 3 years ago

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.

cesss commented 3 years ago

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.

r-lyeh commented 3 years ago

Just use cc -ObjC file.c

r-lyeh commented 3 years ago

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.