【问题标题】:Compile a program with using clang API使用 clang API 编译程序
【发布时间】:2013-04-30 08:39:43
【问题描述】:

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

./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

【问题讨论】:

  • 欢迎来到 StackOverflow!由于您找到了问题的答案,因此欢迎(并鼓励)您将其添加为答案并将该答案标记为正确。这样做将使该线程对将来遇到相同问题的其他用户更有用。

标签: c++ c clang


【解决方案1】:

解决方案

添加来自 llvm-config --cxxflags --ldflags --libs all 的所有参数

我写像this这样的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)

编译链接成功。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2023-02-14
    • 1970-01-01
    • 2015-01-29
    相关资源
    最近更新 更多