0

I am currently trying to compile a program including #include <clang-c/Index.h> with cmake

cmake_minimum_required(VERSION 3.4.3)
project(ast-dump)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(LLVM REQUIRED CONFIG)
find_package(Clang REQUIRED)

include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
include_directories(${CLANG_INCLUDE_DIRS})
add_definitions(${CLANG_DEFINITIONS})

add_executable(ast-dump ast-dump.cpp)

llvm_map_components_to_libnames(llvm_libs all)

target_link_libraries(ast-dump ${llvm_libs})

set_target_properties(ast-dump PROPERTIES
    CXX_STANDARD 14
)

and I got the following errors:

Undefined symbols for architecture x86_64:
  "_clang_Cursor_isNull", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_Location_isInSystemHeader", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_createIndex", referenced from:
      _main in ast-dump.cpp.o
  "_clang_disposeIndex", referenced from:
      _main in ast-dump.cpp.o
  "_clang_disposeString", referenced from:
      toString(CXString) in ast-dump.cpp.o
  "_clang_disposeTranslationUnit", referenced from:
      _main in ast-dump.cpp.o
  "_clang_equalCursors", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_getCString", referenced from:
      toString(CXString) in ast-dump.cpp.o
  "_clang_getCursorDefinition", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_getCursorExtent", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_getCursorKind", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
      traverse(CXTranslationUnitImpl*) in ast-dump.cpp.o
  "_clang_getCursorLocation", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_getCursorSpelling", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_getCursorType", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_getRangeEnd", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_getRangeStart", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_getSpellingLocation", referenced from:
      toLineColumn(CXSourceLocation) in ast-dump.cpp.o
  "_clang_getTokenSpelling", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_getTranslationUnitCursor", referenced from:
      traverse(CXTranslationUnitImpl*) in ast-dump.cpp.o
  "_clang_getTypeSpelling", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_parseTranslationUnit", referenced from:
      _main in ast-dump.cpp.o
  "_clang_tokenize", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
  "_clang_visitChildren", referenced from:
      visit(CXCursor, CXCursor, void*) in ast-dump.cpp.o
      traverse(CXTranslationUnitImpl*) in ast-dump.cpp.o

However, when I try to use the following command line to compile, it works fine:

c++ -isystem /usr/local/opt/llvm/include -L/usr/local/opt/llvm/lib/ -std=c++14 ast-dump.cpp -lclang -o ast-dump

But using command line, if I try to add #include <cmath> in ast-dump.cpp, the catalina <cmath> error occurs and I do not know how to solve that.

Any help with be appreciated! Thanks a lot in advance.

stephen
  • 71
  • 8
  • I think you don't need all the LLVM libs, but `libclang` only. – arrowd Jan 21 '20 at 12:48
  • What is the contents of the `llvm_libs` variable? Try `message("llvm_libs: ${llvm_libs}")`. Are you sure `llvm_map_components_to_libnames` worked as expected in adding all components to that variable? – Kevin Jan 21 '20 at 12:50
  • It outputs this `llvm_libs: LLVMDemangle;LLVMSupport;` And there are a few libraries after this. How can I make sure if it is working or not? – stephen Jan 21 '20 at 13:01
  • I tried to change `all` to `libclang`, but got the following error `ld: library not found for -lLLVMlibclang` – stephen Jan 21 '20 at 13:02
  • You try to find the Clang package, but never link to any Clang libraries. Try adding `${CLANG_LIBS}` to your `target_link_libraries()` call. – Kevin Jan 21 '20 at 16:03
  • I have tried that, but still does not work. – stephen Jan 21 '20 at 19:05

0 Answers0