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?

Jumhyn
  • 6,687
  • 9
  • 48
  • 76
  • Hi @Jumhyn, it is hard to see what you have done already and where exactly it has failed, so I would recommend you to look at either of these articles: https://lowlevelbits.org/how-to-use-llvm-api-with-swift/, https://medium.com/compileswift/how-to-setup-xcode-swift-project-to-use-llvm-c-apis-3ccbf081d002 – AlexDenisov May 08 '17 at 08:22

1 Answers1

4

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.

Community
  • 1
  • 1
Coder-256
  • 5,212
  • 2
  • 23
  • 51
  • 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? – Jumhyn May 09 '17 at 01:15
  • I ask, because `brew install llvm` only installs LLVM 3.5 instead of 4.0 which I built from source. – Jumhyn May 09 '17 at 15:20
  • I'm glad this works! Please remember to mark it as "accepted". In which file did you have to add the `#define`s? 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. – Coder-256 May 09 '17 at 17:07
  • 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! – Jumhyn May 10 '17 at 01:42
  • 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! – Jumhyn May 10 '17 at 03:46
  • Why not creating a C++ file but written in the C way, where you declare functions as `extern "C"` so you can export them to Swift/Objective-C – user9335240 Oct 18 '18 at 22:58