使用clang API编译一个程序

Compile a program with using clang API

本文关键字:一个 程序 clang API 编译 使用      更新时间:2023-10-16

我在Windows上使用mingw (Windows 8 64位,mingw 20120426)构建llvm与clang:

./configure --disable-docs --enable-optimized --enable-targets=x86,x86_64 && make && make install

构建成功。

现在我想用clang-c API编译一个简单的程序:

#include <clang-c/Index.h>
int main (int argc, char** argv) {
    CXIndex index = clang_createIndex (false,true);
}

运行gcc:

gcc 1.cpp -IC:/MinGW/msys/1.0/local/include -LC:/MinGW/msys/1.0/local/lib -lclang -lstdc++

编译正常,但链接失败:

C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0xe3): undefined reference to `llvm::sys::MutexImpl::~MutexImpl()'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x11f): undefined reference to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x168): undefined reference to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x333): undefined reference to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x394): undefined reference to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x435): undefined reference to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x464): more undefined references to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const' follow
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x476): undefined reference to `clang::SourceManager::getMacroArgExpandedLocation(clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x4e7): undefined reference to `clang::ASTUnit::Save(llvm::StringRef)'
...

超过700行

$ llvm-config.exe --version
3.1
$ llvm-config.exe --prefix
C:/MinGW/msys/1.0/local

SOLUTION

添加llvm-config——cxxflags——ldflags——libs all中的所有参数

我这样写makefile

CXX := g++
LLVMCOMPONENTS := all
RTTIFLAG := -fno-rtti
LLVMCONFIG := llvm-config
CXXFLAGS := $(shell $(LLVMCONFIG) --cxxflags) $(RTTIFLAG)
LLVMLDFLAGS := $(shell $(LLVMCONFIG) --ldflags --libs $(LLVMCOMPONENTS))
SOURCES = 1.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXES = $(OBJECTS:.o=)
CLANGLIBS = 
                -lclang
                -lclangTooling
                -lclangFrontendTool
                -lclangFrontend
                -lclangDriver
                -lclangSerialization
                -lclangCodeGen
                -lclangParse
                -lclangSema
                -lclangStaticAnalyzerFrontend
                -lclangStaticAnalyzerCheckers
                -lclangStaticAnalyzerCore
                -lclangAnalysis
                -lclangARCMigrate
                -lclangEdit
                -lclangAST
                -lclangLex
                -lclangBasic
                $(shell $(LLVMCONFIG) --libs)
all: $(OBJECTS) $(EXES)
%: %.o
    $(CXX) -o $@ $< $(CLANGLIBS) $(LLVMLDFLAGS)

编译链接成功

相关文章: