非Clang编译器下,CMake构建时如何依赖libClang?
链接libClang库时出现未解析外部符号错误
我正在更新Anaconda的c99-to-c89分支,使其支持CMake构建。该项目依赖Clang解析C源码,但无需使用Clang作为编译器。我使用vcpkg管理依赖,已在vcpkg.json中添加带clang组件的llvm,并按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
我已在网上及Stack Overflow搜索相关内容,但大多是Clang作为工具链时的链接错误解决方案,而非将Clang作为库链接的情况。找到一个类似问题,但“需要clang导入库”的解决方案我无法理解。我也在LLVM库中搜索缺失的符号,仅在Windows平台下的libclang.dll中找到匹配,但llvm-config --components列出的组件中没有能引用libClang的。请问我该如何正确配置?
内容的提问来源于stack exchange,提问作者GuyPaddock
相关产品推荐
相关产品推荐

