Minimal setup for C++ and Objective-C (Core Libraries) interop

Makes sense to show something Apple related here, right.


Going directly to source is sometimes the right move. On macOS that would be the core libraries using Objective-C. In this example, we use CoreWLAN to ask for information on the current wlan interface connection. You can find the complete source on github.


We have a makefile at project root level, organise our sourcefiles in a directory called src and will produce binaries in a directory called bin.

Let’s start by creating a makefile, which lets us manage building the project.


In the make target build, the first step is to create a dynamic library called libwlaninfo via the CXX compiler, which we have specified as clang. The -stdlib=libc++ flag ensures that the compiler links against libc++ (heavily favoured by Apple) and -std=c++11 tells the compiler to use the c++11 standard. Next we declare this operation to produce a dynamic library by specifying -shared. To get access to Apple’s core library, we have to link the appropriate libraries, which in our case is Foundation and CoreWLAN. The -fpic ensures that the library has relative address management. Finally we specify the Objective-C++ source file (indicated by the .mm extension) and tell the compile to put the output file in the bin directory. The next step is to compile our program entry (via the main function) contained in Main.cpp, link it with the dynamic library and put the executable in the bin directory.


The idea is, to create an interface which abstracts the fact, that we interact with a native Objective-C library and looks like plain old C. The main work will be done in GetCurrentWlanConnection. It returns a status code, which can be interpreted throught the enum WlanErrorCode and, if executing successfully, fills a structure WlanConnectionInfo with info about the interface’s name, the current connections SSID and the signal strength in dBm.


So to get access to Wlan information on macOs we use CoreWLAN. The first step is, to ask for the global CWWiFiClient instance. The client in turn gets us access to the current Wlan interface in form of a CWInterface instance. We check if the interface is currently powered on and connected to a network. If this is the case, we fetch the name of the interface, the SSID of the connected network and the signal strength to the service set.

Now in the main function of our little test tool, we call the interface function imported by libwlaninfo.dylib and print the result as json object.



We created a shared library using Objective-C++, to create a plain old C interface, which can then be linked by any C++ application which wants to make use of CoreWLAN.

Thank you for reading. May the force be with you.

Today’s impossible, will be tomorrow’s reality.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store