Sample CMakeLists.txt file for LLVM project

Lasse Espeholt picture Lasse Espeholt · Nov 27, 2011 · Viewed 13.2k times · Source

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! :)

Answer

arrowd picture arrowd · Nov 27, 2011

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.