Sample CMakeLists.txt file for LLVM project

2020-05-31 05:13发布

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

标签: c++ cmake llvm
2条回答
男人必须洒脱
2楼-- · 2020-05-31 05:33

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).

查看更多
混吃等死
3楼-- · 2020-05-31 05:41

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.

查看更多
登录 后发表回答