0

I'm trying to get a C library called tree-sitter working with my C++ project.

This is what my project structure looks like:

IDEProject
    cmake-build-debug
    Headers
    Sources
    tree-sitter (The library I want to include)
    tree-sitter-json (The library that provides a parser I need to use with tree-sitter)
    CMakeLists.txt

My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.1.0)

project(IDEProject)

set (CMAKE_PREFIX_PATH "/Users/Chloe/Qt/5.12.0/clang_64")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# Find includes in corresponding build directories
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Test)

include_directories(Headers)
include_directories(Sources)

file(GLOB project_headers Headers/*.h)

file(GLOB project_sources Sources/*.cpp)

file(GLOB project_forms Forms/*.ui)

# qt_wrap_cpp(project_headers_wrapped ${project_headers})

set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed
set(CMAKE_AUTOMOC ON)
# Create code from a list of Qt designer ui files
# set(CMAKE_AUTOUIC ON)


add_executable(IDEProject ${project_headers} ${project_sources} ${project_forms})
# Use the Widgets module from Qt 5
target_link_libraries(IDEProject PUBLIC Qt5::Core Qt5::Widgets Qt5::Gui)

add_library(tree-lib tree-sitter/lib/src/lib.c)
include_directories(
        tree-sitter/lib/src
        tree-sitter/lib/include
        tree-sitter/lib/utf8proc
        )

target_link_libraries(IDEProject PRIVATE tree-lib)

After compiling that, I get this error:

Undefined symbols for architecture x86_64:
  "tree_sitter_json()", referenced from:
      PlainTextEdit::onTextChanged() in TextEditor.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The code that my program tries to run is this:

    TSLanguage *tree_sitter_json();
    TSParser *parser = ts_parser_new();

    ts_parser_set_language(parser, tree_sitter_json());

NOTE: All the info regarding tree-sitter is taken from this website: https://tree-sitter.github.io/tree-sitter/using-parsers#an-example-program

Help would really really be appreciated since I've been trying to solve this problem for over 2 days now

Kevin
  • 16,549
  • 8
  • 60
  • 74
CountryTk
  • 3
  • 4
  • Since this is a C library shouldn't it be `extern "C" TSLanguage *tree_sitter_json();` in your code? – john Aug 09 '19 at 21:03
  • Possible duplicate https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c/1041880#1041880 – john Aug 09 '19 at 21:05
  • Note that not all valid C code is valid C++ code. And even when it is, it may have different semantics. – Jesper Juhl Aug 09 '19 at 21:05
  • The library should handle that: https://github.com/tree-sitter/tree-sitter/blob/master/lib/include/tree_sitter/api.h the extern C stuff – CountryTk Aug 09 '19 at 21:14

0 Answers0