Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call standard C library from Swift?

Tags:

c

macos

swift

I want to use C libraries such as stdio.h, stdlib.h, etc. in my swift application. But I can't import . How can I do?

import <stdio.h> // BAD
#include <stdio.h> // BAD
#import <stdio.h> // BAD
import Foundation // cannot use printf etc.
like image 520
wing Avatar asked Jan 10 '23 00:01

wing


1 Answers

To import C functions in pure Swift on the OS-X platform use:

import Darwin

This can be seen as one of the sub-imports inside Foundation.swift, but importing it individually avoids potentially unnecessary processing of Cocoa and other iOS/OSX framework modules.

NeilJaff's answer works because it causes Foundation and all submodules to be imported, but import Darwin is more targeted.

like image 76
Chris Hatton Avatar answered Jan 15 '23 12:01

Chris Hatton