使用CMAKE和MINGW在Windows上链接LLVM库



我一直在使用LLVM作为后端编写编译器。到目前为止,我写过的Cmake文件已经在Linux上工作了,但是Windows上没有任何运气。该项目被分为库中的库,并在单独的子目录中使用自己的cmakelists.txt执行"驱动程序"。顶级cmakelists.txt看起来像这样:

cmake_minimum_required (VERSION 3.7.0)
project (compiler)
set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)
find_package (LLVM REQUIRED CONFIG)
message (STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message (STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
include_directories (${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
add_subdirectory (Compiler_Lib)
add_subdirectory (Compiler_exe)

库的cmakelists.txt:

cmake_minimum_required (VERSION 3.7.0)
add_library (compiler_lib 
        AST.cpp
        AST.h
        parser.cpp
        parser.h
        scanner.cpp
        scanner.h
        token.cpp
        token.h
        visualizer.cpp
        visualizer.h
        codegen.cpp
        codegen.h)
target_include_directories (compiler_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(compiler_lib LLVM)

和可执行文件的cmakelists.txt(这是链接到库失败的地方):

cmake_minimum_required (VERSION 3.7.0)
project (compiler_exe)
add_executable (compiler_exe Compiler_exe.cpp getopt.h getopt.cpp)
target_link_libraries (compiler_exe LINK_PUBLIC LLVM compiler_lib)

我运行命令"c:Program FilesCMakebincmake.exe" .. -G"MinGW Makefiles" -DCMAKE_PREFIX_PATH=C:UsersJamesllvm+clang-7.0.0-win64-msvc-release,其中C:UsersJamesllvm+clang-7.0.0-win64-msvc-release是预先构建的LLVM库的路径。但是,随后运行mingw32-make随着输出而失败

Scanning dependencies of target compiler_lib
[ 10%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/AST.cpp.obj
[ 20%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/parser.cpp.obj
[ 30%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/scanner.cpp.obj
[ 40%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/token.cpp.obj
[ 50%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/visualizer.cpp.obj
[ 60%] Building CXX object Compiler_Lib/CMakeFiles/compiler_lib.dir/codegen.cpp.obj
[ 70%] Linking CXX static library libcompiler_lib.a
[ 70%] Built target compiler_lib
Scanning dependencies of target compiler_exe
[ 80%] Building CXX object Compiler_exe/CMakeFiles/compiler_exe.dir/Compiler_exe.cpp.obj
[ 90%] Building CXX object Compiler_exe/CMakeFiles/compiler_exe.dir/getopt.cpp.obj
[100%] Linking CXX executable compiler_exe.exe
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lLLVM
collect2.exe: error: ld returned 1 exit status
Compiler_exeCMakeFilescompiler_exe.dirbuild.make:102: recipe for target 'Compiler_exe/compiler_exe.exe' failed
mingw32-make[2]: *** [Compiler_exe/compiler_exe.exe] Error 1
CMakeFilesMakefile2:176: recipe for target 'Compiler_exe/CMakeFiles/compiler_exe.dir/all' failed
mingw32-make[1]: *** [Compiler_exe/CMakeFiles/compiler_exe.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2

这是我第一次使用Cmake,所以我可能会错过一些明显的东西,但是正如我所说的那样,它似乎在Linux上起作用。

成功的链接成功两件事必须为真:

1)文件需要存在2)该文件必须在库搜索路径中的目录中

应该有一种方法来获取CMAKE告诉您搜索库的地方列表,您需要找到一种方法来获取libllvm的任何地方。

对部分答案表示歉意,但这是故障排除路径...

最新更新