You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows 10下构建Clang AST工具遭遇未定义引用问题求助

Windows下开发Clang AST工具遭遇链接错误问题

我在Windows 10系统上,用Visual Studio Code开发首个基于C++的Clang AST工具,目标是兼容任意可用编译器。已经查阅过Clang官方文档、YouTube教程和Stack Overflow相关内容,但问题仍未解决。

此前我通过以下步骤,成功让tools/extra/ToolTemplate.cpp示例编译通过:

  • 安装Visual Studio Community 2022
  • 安装MSYS2和MinGW,使用MinGW的CMake套件(编译器版本为g++.exe (MinGW.org GCC-8.2.0-3) 8.2.0)
  • 在VS Code中通过CMake完成构建

但现在构建自己的项目时失败,出现大量未定义引用错误,示例错误如下:

clang::tooling::AtomicChange::AtomicChange(
clang::NamedDecl::getQualifiedNameAsString
clang::tooling::ExecutionContext::reportResult
clang::ast_matchers::MatchFinder::MatchCallback::~MatchCallback()
llvm::sys::PrintStackTraceOnErrorSignal
clang::tooling::createExecutorFromCommandLineArgs
llvm::errs
llvm::toString[abi:cxx11]
clang::ast_matchers::MatchFinder::MatchFinder

Clang二进制文件包含大量库,但没有文档说明各个API对应的库。除了可能缺失正确的链接库之外,我还遗漏了什么?Clang AST的概念确实很棒,但入门难度真的这么高吗?还是我错过了完整的入门教程?

当前使用的链接命令如下:

C:\WINDOWS\system32\cmd.exe /C "cd . && C:\msys64\ucrt64\bin\g++.exe
-g CMakeFiles/canalDoc.dir/main_clang.cpp.obj -o canalDoc.exe -Wl,
--out-implib,libcanalDoc.dll.a -Wl,--major-image-version,0,
--minor-image-version,0 -LC:/install/clang+llvm-18.1.8-x86_64-pc-windows-msvc/lib
-lclangAST -lclangASTMatchers -lclangBasic -lclangFrontend
-lclangSerialization -lclangTooling -lkernel32
-luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32
-luuid -lcomdlg32 -ladvapi32 && cd ."

我的CMakeLists.txt文件内容如下:

cmake_minimum_required(VERSION 3.5.0)
project(canalDoc VERSION 3.5.0 LANGUAGES C CXX)
  
include_directories(C:/install/clang+llvm-18.1.8-x86_64-pc-windows-msvc/include)
link_directories(C:/install/clang+llvm-18.1.8-x86_64-pc-windows-msvc/lib)
add_executable(canalDoc main_clang.cpp)

target_link_libraries(canalDoc
    PRIVATE
    clangAST
    clangASTMatchers
    clangBasic
    clangFrontend
    clangSerialization
    clangTooling
)

内容的提问来源于stack exchange,提问作者Yonatan.Lehman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 12:45:22