我正在尝试更新 Anaconda 的 c99-to-c89 分支 以使用 CMake 进行构建。该项目依赖 Clang 来解析 C 源代码,但它不需要使用 Clang 作为编译器。我正在使用 vcpkg 来解决项目的依赖关系。我的理解是,Clang 是更大的 LLVM 项目的一部分,因此我将
llvm
以及 clang
组件添加到我的 vcpkg.json
文件中,并按照 vcpkg 打印的指导如何导入 LLVM 相关组件,但我仍然遇到链接错误。
这是我的
vcpkg.json
文件:
{
"name": "c99-to-c89",
"version": "1.0.4",
"description": "Tool to convert C99 code to MSVC-compatible C89. Includes fixes from Anaconda Distributions.",
"homepage": "https://github.com/AnacondaRecipes/c99-to-c89/",
"license": "Apache-2.0",
"dependencies": [
{
"name": "vcpkg-cmake",
"host": true
},
{
"name": "vcpkg-cmake-config",
"host": true
},
{
"name": "llvm",
"default-features": false,
"features": [
"clang"
]
}
]
}
这是我的
CMakeLists.txt
文件:
cmake_minimum_required(VERSION 3.22)
project(C99_to_C89)
# Build steps for `c99conv`.
add_executable(c99conv convert.c)
# Locate and configure LLVM as a dependency, so we can pull in CLang from it.
find_package(LLVM CONFIG REQUIRED)
list(APPEND CMAKE_MODULE_PATH ${LLVM_CMAKE_DIR})
include(HandleLLVMOptions)
add_definitions(${LLVM_DEFINITIONS})
target_include_directories(c99conv PRIVATE ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})
# Find the libraries that correspond to the LLVM components that we wish to use
llvm_map_components_to_libnames(llvm_libs Core)
# Link against LLVM libraries
target_link_libraries(c99conv PRIVATE ${llvm_libs} clangIndex)
# Build steps for `c99wrap`.
add_executable(c99wrap compilewrap.c)
我在构建时遇到这些错误:
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getCString referenced in function find_token_index
convert.obj : error LNK2019: unresolved external symbol __imp_clang_disposeString referenced in function find_token_index
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getFileName referenced in function callback
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getSpellingLocation referenced in function get_token_offset
convert.obj : error LNK2019: unresolved external symbol __imp_clang_createIndex referenced in function convert
convert.obj : error LNK2019: unresolved external symbol __imp_clang_disposeIndex referenced in function convert
convert.obj : error LNK2019: unresolved external symbol __imp_clang_createTranslationUnitFromSourceFile referenced in function convert
convert.obj : error LNK2019: unresolved external symbol __imp_clang_disposeTranslationUnit referenced in function convert
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getTranslationUnitCursor referenced in function convert
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getCursorLocation referenced in function callback
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getCursorExtent referenced in function fill_struct_members
convert.obj : error LNK2019: unresolved external symbol __imp_clang_visitChildren referenced in function register_struct
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getCursorSpelling referenced in function find_anon_struct
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getTokenSpelling referenced in function find_token_index
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getTokenLocation referenced in function get_token_offset
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getTokenExtent referenced in function indent_for_token
convert.obj : error LNK2019: unresolved external symbol __imp_clang_tokenize referenced in function fill_struct_members
convert.obj : error LNK2019: unresolved external symbol __imp_clang_disposeTokens referenced in function fill_struct_members
convert.obj : error LNK2019: unresolved external symbol __imp_clang_getCursorKindSpelling referenced in function analyze_decl_context
我已经在网上和 SO 上搜索了有关此主题的任何内容,但我遇到的大多数帖子都是关于当 Clang 是工具链时如何处理链接错误,而不是当你尝试 link against Clang as一个图书馆。很难找到针对该问题的特定关键字。
我在 SO 上遇到了另一个问题,这似乎是我的问题,但是需要“clang 导入库”的解决方案对我来说没有意义。
我还尝试了 LLVM 包含的库,唯一匹配缺失符号的是
libclang.dll
(它是一个 DLL,因为我使用的是 Windows)。我在运行时看到的 LLVM 组件中没有一个能够让我引用 llvm-config --components
。我需要做什么才能把它拉进去?
libclang
文件中引用 LLVM;我只需要在
CMakeLists.txt
中引用它即可。然后,我可以只依赖 vcpkg.json
文件中的 CLang 和 libclang
。这是有效的:CMakeLists.txt