20

I am having a hard time to get LLVM to work on a new project. I've tried multiple CMakeLists.txt examples from http://old.nabble.com/CMake-sample-project--td28871124.html and used a lot of time on it.

I can build LLVM and the examples perfectly but I want a project which is not inside the LLVM folder. I use Visual Studio 2010 on Windows 7. Right now my setup is this:

root
  - CMakeLists.txt (1)
  main
    - CMakeLists.txt (2)
    - main.cpp (an exact copy of the Fibonacci example)

(1)

cmake_minimum_required(VERSION 2.6)
project (TestLLVM)

set(LLVM_SRC_DIR "MY FOLDER/llvm-2.9" CACHE PATH "Directory LLVM source (includes) are in")
set(LLVM_BIN_DIR "MY FOLDER/llvm-2.9-install" CACHE PATH "Directory LLVM binaries (libraries) are in")

set (CMAKE_BUILD_TYPE Debug)

add_definitions (-D_DEBUG)

link_directories(${LLVM_BIN_DIR}/lib/Release)
include_directories(${LLVM_SRC_DIR}/include ${LLVM_BIN_DIR}/include)

add_subdirectory (main)

(2)

if(NOT WIN32 OR MSYS OR CYGWIN)
  set (PLATFORM_LIBS dl boost_system)
endif()

add_executable (main main.cpp)
target_link_libraries (main

    ${PLATFORM_LIBS}

    LLVMX86Disassembler
    LLVMX86AsmParser
    LLVMX86AsmPrinter
    LLVMX86CodeGen

    LLVMSelectionDAG

    LLVMAsmPrinter
    LLVMMCParser
    LLVMX86Info

    LLVMJIT
    LLVMExecutionEngine

    LLVMCodeGen
    LLVMScalarOpts
    LLVMTransformUtils

    LLVMipa
    LLVMAnalysis
    LLVMTarget
    LLVMMC

    LLVMCore
    LLVMSupport
)

CMake works fine and creates a solution file etc. but when I compile the project I get a lot of unresolved externals and mismatches from LLVMX86CodeGen.lib. And I also get this:

defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library

The problem may have something to do with:

  • I removed LLVMSystem from the list because it was not found.
  • My compiled libs is in lib/Release/ and not lib/ as the examples show.

Any help with the above problem would be a great help! :)

Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99

2 Answers2

11

Here is all information you need: http://llvm.org/docs/CMake.html#embedding-llvm-in-your-project.

You are observing such problem because some linkers can't automatically link static libraries in proper order. For this, you need to utilize llvm_map_components_to_libraries function.

Community
  • 1
  • 1
arrowd
  • 33,231
  • 8
  • 79
  • 110
  • Thanks, I've tried the include that code. But I get LLVMConfig not found - if copy the file or writing the full path to LLVMConfig I have to say `.cmake` and then the problem is LLVMDips. If I also fix the path to that file, I get `CMake Error at main/LLVMConfig.cmake:127 (message): Library 'jit' not found in list of llvm libraries.`. – Lasse Espeholt Nov 27 '11 at 16:46
  • You shouldn't copy anything, just run `cmake -DCMAKE_PREFIX_PATH=/path/to/llvm/share/llvm/cmake .`. Hmm, or was it CMAKE_MODULE_PATH... not sure. BTW, it's better to use second code snippet (which uses find_package()). – arrowd Nov 27 '11 at 16:50
  • I have tried to set as described and also in the CMakeLists files :/ I have tried the above and also tried to include path to FindLLVM.cmake and LLVMConfig etc. – Lasse Espeholt Nov 27 '11 at 17:25
  • Did you built LLVM using CMake? And did you installed it, or just built? You should point CMake to place where LLVM is installed. – arrowd Nov 27 '11 at 17:33
  • I followed this guide: http://llvm.org/docs/GettingStartedVS.html. Ran INSTALL and Fibonacci within the LLVM solution. And both went well. – Lasse Espeholt Nov 27 '11 at 17:40
  • Dunno what's your problem, then. Sorry. – arrowd Nov 27 '11 at 17:43
8

The answer from arrowdodger lead me in the right way :) Because llvm_map_components_to_libraries didn't work as expected, I had to do what it does manually which is finding the right order of dependencies. I came up with this:

set (LIBS
    ${LLVM_LIBRARIES_PATH}/LLVMSupport.lib
    ${LLVM_LIBRARIES_PATH}/LLVMCore.lib
    ${LLVM_LIBRARIES_PATH}/LLVMMC.lib
    ${LLVM_LIBRARIES_PATH}/LLVMTarget.lib
    ${LLVM_LIBRARIES_PATH}/LLVMAnalysis.lib
    ${LLVM_LIBRARIES_PATH}/LLVMipa.lib
    ${LLVM_LIBRARIES_PATH}/LLVMTransformUtils.lib
    ${LLVM_LIBRARIES_PATH}/LLVMInstCombine.lib
    ${LLVM_LIBRARIES_PATH}/LLVMScalarOpts.lib
    ${LLVM_LIBRARIES_PATH}/LLVMCodeGen.lib
    ${LLVM_LIBRARIES_PATH}/LLVMExecutionEngine.lib
    ${LLVM_LIBRARIES_PATH}/LLVMJIT.lib
    ${LLVM_LIBRARIES_PATH}/LLVMX86Utils.lib
    ${LLVM_LIBRARIES_PATH}/LLVMX86Info.lib
    ${LLVM_LIBRARIES_PATH}/LLVMMCParser.lib
    ${LLVM_LIBRARIES_PATH}/LLVMX86AsmParser.lib
    ${LLVM_LIBRARIES_PATH}/LLVMX86AsmPrinter.lib
    ${LLVM_LIBRARIES_PATH}/LLVMAsmPrinter.lib
    ${LLVM_LIBRARIES_PATH}/LLVMSelectionDAG.lib
    ${LLVM_LIBRARIES_PATH}/LLVMX86CodeGen.lib
    ${LLVM_LIBRARIES_PATH}/LLVMX86Disassembler.lib
    ${LLVM_LIBRARIES_PATH}/LLVMInterpreter.lib    
)

target_link_libraries(main ${LIBS})

And then I only had some issues with debug/release lib for LLVMX86Utils (_ITERATOR_DEBUG_LEVEL).

Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99