Thread: macOS C Programming Help

  1. #1
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137

    Question macOS C Programming Help

    I had to switch environments for work and I am feeling like a total fish out of water on macOS between the actual macOS API stuff ("Core Foundation" I believe it's called), and the Unix wrapper stuff, etc...

    I need some help. The best way I can describe what I am trying to do is to give a Windows example. On Windows, I'm used to being able to do something like:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    int main(void)
    {
        fH = CreateFile(....)
    
       HANDLE myMem = HeapAlloc(...)
    
       return EXIT_SUCCESS;
    }
    And so on...

    However, I cannot find the <windows.h> equivalent for macOS. I checked their docs and it seems they only support either Swift or "Objective C" and I have no knowledge of those languages. However, I am having a hard time believing that I cannot access macOS API calls with just C itself... Is this the case? Is there a C API for these funcs?

    Also note that even the Windows API claims it's "C++" but I can do everything from within C by using header files like <windows.h> for example. Can I also do something similar in Mac? If not, can I use an Objective-C Compiler and still build C with it like how I can use a C++ compiler to build C for the most part?

    To make it even more confusing, macOS is based off of Unix and is POSIX and so that also means I can use Unix calls, but I'm finding strange inconsistencies from actual Linux and I imagine those calls are also just wrappers around some macOS API which I'd rather learn and use.


    Any guidance in this area would be greatly appreciated because there seems to be a lot less info on it online when compared with Windows.

    Thank you.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    You are thinking of the "wrapper" in the wrong way.
    The base operating system is BSD Unix.
    The mac stuff is the "wrapper".
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Quote Originally Posted by john.c View Post
    You are thinking of the "wrapper" in the wrong way.
    The base operating system is BSD Unix.
    The mac stuff is the "wrapper".
    So what you're saying John is that the core OS is mostly BSD and all of the NS and CF functions eventually make BSD calls at some level?I realize this is quite a bit of assumptions but is that the general idea?Kind of like how kernel32.dll -> ntdll.dll -> the Windows kernel interface?
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    I think it works something like that, but I don't use it so you should read up on it yourself. Read these and let me know the scoop:
    Apple'&#39;'s Operating System Guru Goes Back to His Roots | WIRED
    macOS - Wikipedia
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    john.c,

    Thanks for the links and especially that diagram - it's a big help. So I did some homework and came up with this:

    macOS has a lot of lingo… So based on my research on the topic, macOS is a platform which contains a GUI much like Gnome for Linux (called Aqua in the graphic)
    but the actual OS is called Darwin, which is open source
    and their entire kernel is called XNU which is also all open source
    XNU is made up of 2 main components: BSD and Mach
    Mach was a microkernel and BSD is BSD… They took parts of both and combined them into one
    and that created their XNU kernel
    Apparently you can call “Core OS” functions which are much like ntdll.dll on Windows or the native API
    but people rarely do that anymore… Mostly its using managed Cocoa which uses Swift or Objective-C
    much like C# on Windows
    ALso I noted that processes on Mac are called Tasks instead
    and what Windows called HANDLEs are called Ports on Mac
    so a thread can have ports
    After all of that, it's also POSIX compliant so I think the best solution here, to spare myself months of reading through managed-code funcs, is just to use the POSIX API. I was not that familiar with POSIX coming from Windows world but actually, it's pretty easy to use and I used it today to solve the problems I needed to like iterating through directories and running programs. To do this natively on Windows, I would typically use the Windows API which involved a lot more lines of code than using <dirent.h> in POSIX.
    Last edited by Asymptotic; 05-13-2018 at 03:16 AM.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MIT-Licensed(*) App for iOS, MacOS, Win, cross platform, full C++ sources!
    By thevinn in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 08-31-2011, 12:35 PM
  2. Visual Studio 2010 on MacOS X
    By trw in forum General Discussions
    Replies: 1
    Last Post: 05-26-2011, 12:46 PM
  3. lrt and lz flag in MacOs X
    By lafayette in forum C Programming
    Replies: 1
    Last Post: 10-01-2008, 09:21 AM
  4. why no macos programming section?
    By manav in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-15-2008, 02:23 PM
  5. installing libraries under MacOS, CodeWarrior 4.1
    By Captain Penguin in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2002, 11:28 AM

Tags for this Thread