问题
I'm trying to parse a library using libclang, and I'm stuck with what could be a very simple issue: how to configure it with STL?
At the moment, it fails to parse a translation unit because it can't find <string>
.
Here's what I tried :
char *args[] = {"-x", "c++", "-Ic:/my/library/includes", "-IG:/Prog/libcxx-3.4/include"};
clang_parseTranslationUnit(index, "c:/my/library/test.cpp", args, 4, 0, 0, 0);
I'm on windows, with the precompiled clang binaries downloaded from llvm.org, and I tried with various STL implementations :
- Visual studio
- MingW
- libCXX
In each case, I ended up with unknown types.
For example, with mingw, I've got the following error messages :
/mingw/include\wchar.h:221:71: error: unknown type name '_locale_t'
/mingw/include\wchar.h:223:81: error: unknown type name '_locale_t'
/mingw/include\stdlib.h:173:65: error: unknown type name '_locale_t'
/mingw/include\stdlib.h:175:75: error: unknown type name '_locale_t'
/mingw/include\io.h:301:14: error: unknown type name 'off64_t'
/mingw/include\io.h:301:36: error: C++ requires a type specifier for all declarations
/mingw/include\io.h:302:14: error: unknown type name 'off64_t'
/mingw/include\io.h:302:39: error: unknown type name 'off64_t'
/mingw/include\unistd.h:65:20: error: unknown type name 'off_t'
The rare tutorials I've found about this subject don't talk about this subject...
回答1:
Since libclang was precompiled, it doesn't know about the exact paths of the standard libraries used by your compilers. You'll have to tell it about the standard include path using -I
switches in your arguments list, when calling clang_parseTranslationUnit
.
Here is the command I use to find about the inclusion paths for gcc
on Linux. You should be able to adapt it to MinGW in your windows environment:
$ echo "" | g++ -v -x c++ -E -
...
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.8
/usr/include/x86_64-linux-gnu/c++/4.8
/usr/include/c++/4.8/backward
/usr/lib/gcc/x86_64-linux-gnu/4.8/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
...
来源:https://stackoverflow.com/questions/22480534/how-to-use-libclang-with-stl