9

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?

1

1 Answer 1

4
+50

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.

Sign up to request clarification or add additional context in comments.

6 Comments

This works! As a note, I had to #define __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS at the top of the file before including. Do you know why the approach using a custom built installation didn't work?
I ask, because brew install llvm only installs LLVM 3.5 instead of 4.0 which I built from source.
I'm glad this works! Please remember to mark it as "accepted". In which file did you have to add the #defines? I think those are usually used only for compiling, and the Homebrew version comes precompiled, so it seems strange that you would need them. Additionally, the version that Homebrew installs for me is actually 4.0.0 (please run /usr/local/opt/llvm/bin/llc --version to check). Try running brew update && brew upgrade llvm to ensure that you have the latest version installed.
I added the includes at the top of the bridging header in Xcode. There is some sort of preprocessor issue: the error was the same as the one in the slides I linked in the question (Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h). Perhaps this is not an issue with 4.0. I'll try that out!
P.S. I'm saving the bounty if someone can figure out how to get this to work with a custom-build install of LLVM, but if there's no such answer before it's going to expire then I'll award it to you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.