0

I am trying to create a C/C++ parser using libclang in Windows. I am using MinGW 6.3.0.

This is the line of code that I am using to compile the file.

g++ -I../clang/include/ main.cpp -L../clang/lib -llibclang -o cdeep

Folder Structure:

-clang
  -include
    -clang-c
      -Index.h
      -etc
    -llvm-c
  -lib
    -libclang.lib
    -etc
-src
  -main.cpp

The code in main.cpp:

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

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

  clang_disposeTranslationUnit(unit);
  clang_disposeIndex(index);
}

The error:

g++ -I../clang/include/ main.cpp -L../clang/lib -llibclang -o cdeep
C:\Users\ineeve\AppData\Local\Temp\ccLfSBTa.o:main.cpp:(.text+0x26): undefined reference to `clang_createIndex'
C:\Users\ineeve\AppData\Local\Temp\ccLfSBTa.o:main.cpp:(.text+0x64): undefined reference to `clang_parseTranslationUnit'
C:\Users\ineeve\AppData\Local\Temp\ccLfSBTa.o:main.cpp:(.text+0xa9): undefined reference to `clang_disposeTranslationUnit'
C:\Users\ineeve\AppData\Local\Temp\ccLfSBTa.o:main.cpp:(.text+0xb4): undefined reference to `clang_disposeIndex'
collect2.exe: error: ld returned 1 exit status

I get the same error if I don't use the -llibclang parameter.

This question is not a duplicate as my friend can compile exactly the same code with exactly the same g++ command.

  • Are you sure the library files are same? Maybe they're compiled for a wrong architecture? – HolyBlackCat Jan 25 '18 at 15:42
  • He is using Windows 7, I am using Windows 10. Anyway, I am now using the libraries that I installed with LLVM and the error persists. – Renato Campos Jan 25 '18 at 15:45
  • 1
    `-llibclang` looks suspicious. It probably should be `-lclang` – HolyBlackCat Jan 25 '18 at 15:50
  • Also, what do `g++ --version` and `objdump -a libclang.a` output? – HolyBlackCat Jan 25 '18 at 15:50
  • objdump was giving me the following error: objdump: libclang.lib(libclang.dll): Recognised but unhandled machine type (0x8664) in Import Library Format archive objdump: libclang.dll: File format not recognized. I am now trying to install mingw_64 bit version – Renato Campos Jan 25 '18 at 16:05
  • Thank you @HolyBlackCat, in the end I was using MinGW 32 bit on a Windows10 64bit machine. Unninstalling MinGW-32 and installing MinGW-64 bit solved my problem. – Renato Campos Jan 25 '18 at 16:16
  • What's funny is that MinGW-w64 compiles for 32 bits by default. – HolyBlackCat Jan 25 '18 at 17:14

0 Answers0