ubuntu 14.04 Clang编译安装,libclang解析c++代码

注意事项:

1. 内存8G以上,我当时4G内存,编译debug版就出现了内存耗尽的情况;

2.最好不要编译debug版本,我编译到83%左右时已经耗尽30多G磁盘空间,并且会产生很大的编译日志文件(提示12G左右),同样提示内存不足

针对内存耗尽的问题,当时也在网上找了很久,来自于stackoverflow(https://stackoverflow.com/questions/25197570/llvm-clang-compile-error-with-memory-exhausted)的回答:

  1. Add more RAM to your VM, or
  2. Use gold instead of ld as a linker, or (sudo ln -s `which gold` /usr/local/bin/ld)
  3. Build Release, not Debug build
关于源码,可以去官网下载: 下载地址 , 也可以从svn上面checkout下来,具体的编译过程可以参考官网的指南: http://clang.llvm.org/get_started.html ,如下:

If you would like to check out and build Clang, the current procedure is as follows:

  1. Get the required tools.
  2. Check out LLVM:
    • Change directory to where you want the llvm directory placed.
    • svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
  3. Check out Clang:
    • cd llvm/tools
    • svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
    • cd ../..
  4. Check out extra Clang tools: (optional)
    • cd llvm/tools/clang/tools
    • svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
    • cd ../../../..
  5. Check out Compiler-RT (optional):(不需要)
    • cd llvm/projects
    • svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
    • cd ../..
  6. Check out libcxx: (only required to build and run Compiler-RT tests on OS X, optional otherwise)不需要
    • cd llvm/projects
    • svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
    • cd ../..
  7. Build LLVM and Clang:
    • mkdir build (in-tree build is not supported)
    • cd build
    • cmake -G "Unix Makefiles" ../llvm(最好用 cmake -G "Unix Makefiles"-DCMAKE_BUILD_TYPE:String=Release ../llvm, 编译release版本,debug版本有三十多G
    • make
    • This builds both LLVM and Clang for debug mode.
    • Note: For subsequent Clang development, you can just run make clang.
    • CMake allows you to generate project files for several IDEs: Xcode, Eclipse CDT4, CodeBlocks, Qt-Creator (use the CodeBlocks generator), KDevelop3. For more details see Building LLVM with CMake page.
  8. If you intend to use Clang's C++ support, you may need to tell it how to find your C++ standard library headers. In general, Clang will detect the best version of libstdc++ headers available and use them - it will look both for system installations of libstdc++ as well as installations adjacent to Clang itself. If your configuration fits neither of these scenarios, you can use the -DGCC_INSTALL_PREFIX cmake option to tell Clang where the gcc containing the desired libstdc++ is installed.
  9. Try it out (assuming you add llvm/build/bin to your path):
    • clang --help
    • clang file.c -fsyntax-only (check for correctness)
    • clang file.c -S -emit-llvm -o - (print out unoptimized llvm code)
    • clang file.c -S -emit-llvm -o - -O3
    • clang file.c -S -O3 -o - (output native machine code)
  10. Run the testsuite:不需要
    • make check-clang
接下来就是如何利用libclang对c++代码进行抽象语法树生成,以及关键字提取。参考: https://shaharmike.com/cpp/libclang/

#include <iostream>
#include <clang-c/Index.h>
using namespace std;

ostream& operator<<(ostream& stream, const CXString& str)
{
  stream << clang_getCString(str);
  clang_disposeString(str);
  return stream;
}

int main()
{
  CXIndex index = clang_createIndex(0, 0);
  CXTranslationUnit unit = clang_parseTranslationUnit(
    index,
    "header.hpp", nullptr, 0,
    nullptr, 0,
    CXTranslationUnit_None);
  if (unit == nullptr)
  {
    cerr << "Unable to parse translation unit. Quitting." << endl;
    exit(-1);
  }

  CXCursor cursor = clang_getTranslationUnitCursor(unit);
  clang_visitChildren(
    cursor,
    [](CXCursor c, CXCursor parent, CXClientData client_data)
    {
      cout << "Cursor '" << clang_getCursorSpelling(c) << "' of kind '"
        << clang_getCursorKindSpelling(clang_getCursorKind(c)) << "'\n";
      return CXChildVisit_Recurse;
    },
    nullptr);

  clang_disposeTranslationUnit(unit);
  clang_disposeIndex(index);
}
header.hpp

enum class Cpp11Enum
{
  RED = 10,
  BLUE = 20
};

struct Wowza
{
  virtual ~Wowza() = default;
  virtual void foo(int i = 0) = 0;
};

struct Badabang : Wowza
{
  void foo(int) override;

  bool operator==(const Badabang& o) const;
};

template <typename T>
void bar(T&& t);



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值