Using the LLVM C API in a Swift Xcode Project

I am trying to use the LLVM C API in an Xcode project written in Swift. To do this, I freely follow the manual here , but I have problems. At the compilation stage, after adding the included paths to the build settings in Xcode, I get the following errors:

<unknown>:0: error: module 'LLVM_Backend.CodeGen.PBQP.math' requires feature 'cplusplus' /Users/freddy/Development/llvm-source/build/include/llvm/Support/DataTypes.h:35:10: note: submodule of top-level module 'LLVM_Backend' implicitly imported here #include <math.h> ^ <module-includes>:1:9: note: in file included from <module-includes>:1: #import "./Analysis.h" ^ /Users/freddy/Development/llvm-source/llvm/include/llvm-c/./Analysis.h:22:10: note: in file included from /Users/freddy/Development/llvm-source/llvm/include/llvm-c/./Analysis.h:22: #include "llvm-c/Types.h" ^ /Users/freddy/Development/llvm-source/llvm/include/llvm-c/Types.h:17:10: error: could not build module 'LLVM_Support_DataTypes' #include "llvm/Support/DataTypes.h" ^ /Users/freddy/Development/Xcode Projects/SwiftLLVMTest/SwiftLLVMTest/main.swift:10:8: error: could not build Objective-C module 'LLVM_C' import LLVM_C 

The next step in the slides is to add flags:

 -Xcc -D__STDC_CONSTANT_MACROS \ -Xcc -D__STDC_LIMIT_MACROS 

but I'm not sure where to put them in the build settings, adding them to the "Other C flags" or "Other quick flags" options seems to do nothing.

How can I do it?

+7
swift llvm
source share
1 answer

Try installing LLVM precompiled by simply running brew install llvm using Homebrew .

NOTE. I highly recommend using a Swift shell such as LLVMSwift , in which case you should follow the installation instructions from here. But if you want to directly access LLVM yourself, read on.

Add /usr/local/opt/llvm/include in the header search path and /usr/local/opt/llvm/lib to your library search path under the desired goal of your project in the "Build Settings" section:

Added to search path

And drag /usr/local/opt/llvm/lib/libLLVM.dylib (open in Finder with open -R '/usr/local/opt/llvm/lib/libLLVM.dylib' ) in the "Related Structures and Libraries" section "General" (and make it "Required" as shown):

Added to

Finally, create an Objective-C Bridging Header header ( steps 1-2 in this guide if you don't know how to do this) and specify the headers you need (for example, #include <llvm-c/Core.h> ) : Objective-C jumper header

And you're all set! Just use any LLVM class, as usual in Swift code.

+2
source share

All Articles