Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LLVM C API in a Swift Xcode Project

Tags:

swift

llvm

I'm attempting to use the LLVM C API in an Xcode project written in Swift. To do so, I am loosely following the guide here, but am having trouble. In the compilation step, after adding the include paths to the build settings in Xcode, I am getting 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 the flags:

-Xcc -D__STDC_CONSTANT_MACROS \
-Xcc -D__STDC_LIMIT_MACROS

but I'm not sure where to put these in the build settings--adding them to the 'Other C Flags' or 'Other Swift Flags' options doesn't seem to do anything.

How should I go about doing this?

like image 261
Jumhyn Avatar asked May 05 '17 21:05

Jumhyn


1 Answers

Try installing LLVM pre-compiled by simply running brew install llvm using Homebrew.

NOTE: I strongly recommend using a Swift wrapper such as LLVMSwift, in which case you should follow its installation instructions from here on. But if you would like to directly access LLVM yourself, then read on.

Add /usr/local/opt/llvm/include to your header search paths and /usr/local/opt/llvm/lib to your library search paths under the desired target of your project under "Build Settings":

Added to search paths

And drag /usr/local/opt/llvm/lib/libLLVM.dylib (open in Finder with open -R '/usr/local/opt/llvm/lib/libLLVM.dylib') to "Linked Frameworks and Libraries" under "General" (and make it "Required" as shown):

Added to "Linked Frameworks and Libraries"

Finally, create an Objective-C Bridging Header (steps 1-2 in this tutorial if you are not sure how) and include whichever headers you need (for example, #include <llvm-c/Core.h>): Objective-C Bridging Header

And you're all set! Simply use any LLVM class as you normally would in Swift code.

like image 154
Coder-256 Avatar answered Nov 10 '22 00:11

Coder-256