[cfe-dev] libclang c API will not find c++ system headers

Jacob Carlborg via cfe-dev cfe-dev at lists.llvm.org
Wed Aug 21 11:40:12 PDT 2019


On 2019-08-21 16:54, Per Bull Holmen via cfe-dev wrote:
> Hi
> 
> I wanted to sort out a bug/limitation in a color coding plugin for neovim (not maintained by me), and tried to set up a vanilla c++ project to find out how libclang works. This is what I've done:
> 
> 1) Download prebuilt binaries of clang+llvm-8.0.0 for macOS. I later downloaded and tried 7.0.0 as well
> 2) Create command line tool project in Xcode
> 3) Link to libclang.dylib under Linked Frameworks and Libraries
> 4) Add /path/to/clang+llvm/lib under Library search paths
> 5) Add /path/to/clang+llvm/include under Header search paths
> 6) Add /path/to/clang+llvm/lib under runpath search paths
> 7) Create new c++ target with only the auto-generated code to test parsing
> 8) Build the new target from command line, and copy the clang invocation from the build log
> 9) Run regex replace on the clang invocation to be sure I have an exact copy of the command line args during build of the test target, as an array of c strings (const char *)
> 10) Call clang_parseTranslationUnit2 with those parameters, with and without argv[0] (the executable path).
> 
> This is my main implementation:
> 
> int main(int argc, const char * argv[]) {
>      CXIndex idx = clang_createIndex(0,1);
>      CXTranslationUnit tu;
> 
>      CXErrorCode errorCode;
> 
>      if( !( errorCode = clang_parseTranslationUnit2( idx,
>                                      NULL,
>                                      options,
>                                      opt_count,
>                                      NULL,
>                                      0,
>                                      0,
>                                      &tu) ) )
> 
>          std::cout << "Success\n";
>      else
>          std::cout << "error: " << errorCode << std::endl;
> 
>      clang_disposeTranslationUnit(tu);
>      clang_disposeIndex(idx);
>      return 0;
> }
> 
> When I try it on a c file, with system header includes, it produces the following output:
> Success
> 
> With a c++ file, it produces the following output:
> /Users/pbholmen/Projects/ColorCodingTest/Vanilla/main.cpp:9:10: fatal error: 'iostream' file not found
> Success
> 
> This is the file it tries to parse:
> #include <iostream>
> 
> int main(int argc, const char * argv[]) {
>       // insert code here...
>       std::cout << "Hello, World!\n";
>       return 0;
> }
> 
> I have tried to invoke the clang binary inside the downloaded binaries folder, from the command line, compiling the same file with the exact same options as given to
> clang_parseTranslationUnit2, and it compiles without issues.
> 
> I can post the list of command line options input to clang_parseTranslationUnit2 as well, but the message is getting long and I'd feel I'm spamming you with tons of Xcode-generated
> options, but if you ask for it I'll post it as well. For argv[0] I tried both with the path to the Xcode-provided clang, the path to the dowloaded clang, and omitting the path. I tried to provide
> the source file both as an argument to clang_parseTranslationUnit2, and embedded in my argv. Same result either way. I have not deleted any files or folders inside the downloaded prebuilt
> binaries folder or relocated libclang.dylib.

The per-compiled binaries available at http://releases.llvm.org are 
configured differently compared to the binaries shipped by Apple. You 
need to either manually (either on the command line or directly in your 
application) add these paths or run an extra installation step to 
install the headers in /usr/include where both binaries are looking. If 
you run Clang with the "-v" flag and compare the one shipped by Apple 
and the one provided at http://releases.llvm.org you will see the 
difference.

-- 
/Jacob Carlborg




More information about the cfe-dev mailing list