Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use standard library with Clang and LibTooling

I want to use Clang and LibTooling to create some C++ source analysis and transformation tools. I've built Clang and LibTooling following this tutorial, and I've been able to run and create some analysis tools and compile C++ programs using the Clang binary I built. However, if I include headers from the standard library (in either source files or my tools), I run into issues when compiling or running the source files/tools. For instance, if I run clang-check on the following C++ source file:

#include <iostream>

int main() {
  std::cout << "Hello";
  return 0;
}

I get "fatal error: 'iostream' file not found". (Note: I can compile C++ programs, e.g. ones with user-defined classes, just not C++ programs using the standard library.) In an attempt to resolve the issue, I built libc++ (following this guide, building it in the llvm/project directory where I built LLVM and Clang), but I'm still having trouble getting Clang and the tools to use libc++. Now, if I try to compile a test file using:

export CPLUS_INCLUDE_PATH="~/clang-llvm/llvm/projects/libcxx/include"
export LD_LIBRARY_PATH="~/clang-llvm/llvm/projects/libcxx/lib"
~/clang-llvm/llvm/build/bin/clang++ ~/Documents/main.cpp

Then I get "fatal error: 'unistd.h' file not found". So my question is this: how do I properly point Clang and my tools to use libc++?

I am running OS X Yosemite 10.10 and using Clang 3.6.0.

like image 634
Jonathan Sharman Avatar asked Nov 23 '14 18:11

Jonathan Sharman


People also ask

Is there a source-to-source transformation tool based on Clang libtooling?

This post provides an introduction into building a source-to-source transformation tool based on Clang's libTooling. It should provide a foundation for building custom tools, and nicely complements the existing Clang libTooling documentation and examples. Check them out, by the way.

What is libtooling?

LibTooling is a library to support writing standalone tools based on Clang. This document will provide a basic walkthrough of how to write a tool using LibTooling. For the information on how to setup Clang Tooling for LLVM see How To Setup Clang Tooling For LLVM Tools built with LibTooling, like Clang Plugins, run FrontendActions over code.

Is it possible to develop tools like this in Clang?

Clang is moving forward very fast. Today, to develop tools similar to the one presented in that post one should use the "tooling" library (libTooling). I hinted at its existence even in that post, but it was still at its infancy then. Now libTooling is a fairly mature layer through which it's possible to interact with Clang.

What do I need to build a translation tool based on libtooling?

This document is intended to show how to build a useful source-to-source translation tool based on Clang’s LibTooling. It is explicitly aimed at people who are new to Clang, so all you should need is a working knowledge of C++ and the command line. In order to work on the compiler, you need some basic knowledge of the abstract syntax tree (AST).


1 Answers

Clang comes with some custom includes. So usually you have clang in /usr/bin/clang++ and the includes in /usr/lib/clang/3.6.1/include

but clang looks for them as a relative path: ../lib/clang/3.6.1/include

so make sure this relative path is accessible from either the clang++ binary, or your libtooling application.

like image 104
Rafał Dembek Avatar answered Sep 27 '22 02:09

Rafał Dembek