Search code examples
c++llvm-clangkaleidoscope

Kaleidoscope Example Code Compile Errors on MacOS with LLVM(8|10)


Compiling kaleidoscope tutorial code fails with clang++ -g -O3 toy.cpp $(llvm-config --cxxflags) -std=c++17 (as the example goes) and outputs the following error:

Undefined symbols for architecture x86_64:
  "llvm::DisableABIBreakingChecks", referenced from:
      llvm::VerifyDisableABIBreakingChecks in toy-e1a114.o
ld: symbol(s) not found for architecture x86_64
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)

LLVM were installed with brew install llvm (version 10 when this question is posted), later changed to brew install llvm@8.

Interesting thing is, removing header llvm/ADT/STLExtras.h actually solves this. But I'm afraid removing this will not be a general solution.


Solution

  • I think the problem might be that $(llvm-config --cxxflags) doesn't work properly in macOS. I don't know exactly what it is with --cxxflags, but I personally experienced a problem that $(llvm-config --libfiles) didn't return the correct path to the shared library libLLVM in macOS (that command worked in Linux anyway).

    However, I suggest to use CMake, which you will need anyway when working with LLVM. Below is a sample CMake code copied from the LLVM website. I followed this CMake code and can compile my project in macOS.

    cmake_minimum_required(VERSION 3.4.3)
    project(SimpleProject)
    
    find_package(LLVM REQUIRED CONFIG)
    
    message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
    message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
    
    # Set your project compile flags.
    # E.g. if using the C++ header files
    # you will need to enable C++11 support
    # for your compiler.
    
    include_directories(${LLVM_INCLUDE_DIRS})
    add_definitions(${LLVM_DEFINITIONS})
    
    # Now build our tools
    add_executable(simple-tool tool.cpp)
    
    # Find the libraries that correspond to the LLVM components
    # that we wish to use
    llvm_map_components_to_libnames(llvm_libs support core irreader)
    
    # Link against LLVM libraries
    target_link_libraries(simple-tool ${llvm_libs})