使用clang API编译一个程序
Compile a program with using clang API
我在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)
编译链接成功
相关文章:
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何使用C++读取另一个程序中的源代码输出
- 3-3. 编写一个程序来计算每个不同单词在其输入中出现的次数
- 我的目标是编写一个程序来计算和存储字符串在字符数组中出现的位置
- 使用另一个程序调用正在运行的 C++ 程序中的函数
- 我正在尝试制作一个程序,在添加 n 天(整数)后告诉一个人什么是一天(例如星期一等)
- 编写一个程序,帮助Tudio将MIDI音符转换为名称和八度音阶
- 有没有办法在一个C++程序中同时使用 FFTW 和 cuFFTW?
- 一个C++程序,用于在输入位数时输出具有特定位数的 .txt 文件中的所有数字
- C++将结构复制到另一个程序的资源
- 我编写了一个程序来显示数组的内容,但它显示的是它的地址
- 我已经做了一个程序来使用队列链C++表显示学生姓名和身高,现在我想以我不知道如何显示的顺序显示它
- 我需要编写一个程序来读取一个文件,该文件将输出所有唯一的整数,如果已经看到整数,它将被关闭
- 仅在 boost::hash_combine 中运行一个程序期间保证确定性
- 如何制作一个程序,它使用特定单词加密和解密消息?
- 尝试创建一个程序来查找非二叉树的高度.最终得到一个很长的循环,没有答案
- 编写一个程序,提示用户输入一个整数,然后输出数字的单个数字和数字的总和
- 如何编写一个程序来打印特定基数的前N个数字
- 如何创建一个C++程序来读取字符串数组中的信息,然后将其排序到类中?