clang_parseTransitionUnit2 gives error

Enes Albay albayenes at gmail.com
Thu Apr 14 22:26:57 BST 2022


I guess I found the reason for the error. Clang works if I comment out the
line "QApplication app(argc, argv);" Otherwise It gives error like this:

$ ./helloworld
4
Unable to parse translation unit. Quitting.

As far as I understand this bug is related to this one
https://stackoverflow.com/questions/17953156/cannot-use-libclang-with-qt

But the solution suggested in stackoverflow does not work for me. Do you
have any idea ?

/******************MAIN **************************/
#include <QApplication>
#include "clang-c/Index.h"

#include <iostream>
using namespace std;

int main (int argc, char *argv[])
{
    QApplication app(argc, argv);
   CXIndex index = clang_createIndex(0, 0);
  CXTranslationUnit unit = nullptr;

  CXErrorCode code = clang_parseTranslationUnit2(
    index,
    "/media/enes/MyFiles/programming/cpp/cmakeExamples/ex1/main.cpp",
nullptr, 0,
    nullptr, 0,
    CXTranslationUnit_None, &unit);

  cerr << code << endl;
  if (code != CXError_Success) {
    cerr << "Unable to parse translation unit. Quitting." << endl;
    exit(-1);
  }

  return 0;
}


# CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

project (helloworld)

set(LLVM_ROOT /usr/lib/llvm-13)
set(QT_MIN_VERSION "5.15.2")

# Find Qt modules
find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS
    Core    # QCommandLineParser, QStringLiteral
    Widgets # QApplication
)

find_program(LLVM_CONFIG_EXECUTABLE NAMES llvm-config HINTS
${LLVM_ROOT}/bin DOC "llvm-config executable" NO_DEFAULT_PATH)
message(${LLVM_CONFIG_EXECUTABLE})
set(Clang_DIR /usr/lib/llvm-13/cmake)

# Find CMake file for Clang
find_package(Clang REQUIRED)

execute_process(
        COMMAND ${LLVM_CONFIG_EXECUTABLE} --includedir
        OUTPUT_VARIABLE LLVM_INCLUDE_DIRS
        OUTPUT_STRIP_TRAILING_WHITESPACE
        )

message(${LLVM_INCLUDE_DIRS})

execute_process(
        COMMAND ${LLVM_CONFIG_EXECUTABLE} --libdir
        OUTPUT_VARIABLE LLVM_LIBRARY_DIRS
        OUTPUT_STRIP_TRAILING_WHITESPACE
        )

include_directories(${LLVM_INCLUDE_DIRS})

set(helloworld_SRCS main.cpp)
add_executable(helloworld ${helloworld_SRCS})
target_link_libraries(helloworld
    Qt5::Widgets
    /usr/lib/llvm-13/lib/libclang.so
    /usr/lib/llvm-13/lib/libclang-cpp.so.13
)



~



On Tue, Apr 12, 2022 at 6:37 PM Milian Wolff <mail at milianw.de> wrote:

> On Tuesday, April 12, 2022 5:33:38 PM CEST Enes Albay wrote:
> > I found something. After removing clang and llvm from my system I get the
> > following error which is the same as before removing clang.
> >
> > I think the actual problem is clang_parseTranslationUnit2 can not call
> > clang even before removing clang
> >
> > Is there a way to verify such a situation?
>
> We use libclang directly, not clang. Try to ldd on the kdevclangsupport or
> use
> gdb and `info shared` to see where it loads that from. You didn't remove
> all
> of that yet apparently.
>
> --
> Milian Wolff
> http://milianw.de
>
>
>

-- 
Enes Albay
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kdevelop-devel/attachments/20220415/f9ad69bb/attachment.htm>


More information about the KDevelop-devel mailing list