-2

My source code

#include <tree_sitter/api.h>
int main() {
    TSParser *parser = ts_parser_new();
}

I compile it with the following gcc command

gcc \
-I ~/source_repos/tree-sitter/lib/include \
test-json-parser.c \
~/source_repos/tree-sitter-json/src/parser.c \
~/source_repos/tree-sitter/libtree-sitter.a \
-o test-json-parser

This works fine. But if I do the following in CMake

cmake_minimum_required(VERSION 3.17)
project(tmp_tree_sitter C)

set(CMAKE_C_STANDARD 11)


include_directories(/home/rip/source_repos/tree-sitter/lib/include)

add_executable(tmp_tree_sitter
        main.c
        /home/rip/source_repos/tree-sitter/libtree-sitter.a
        /home/rip/source_repos/tree-sitter-json/src/parser.c
)

The following error message raises

Scanning dependencies of target tmp_tree_sitter
[ 33%] Building C object CMakeFiles/tmp_tree_sitter.dir/main.c.o
[ 66%] Linking C executable tmp_tree_sitter
/usr/bin/ld: CMakeFiles/tmp_tree_sitter.dir/main.c.o: in function `main':
/home/rip/CLionProjects/tmp_tree-sitter/main.c:12: undefined reference to `ts_parser_new'

The only difference is, I have different directories, so in one the source file is named test-json-parser.c and in the other main.c. So how can I do the same in CMake, as I did with gcc?

Rip
  • 73
  • 2
  • 8
  • I don't know if I should post the error message, because than I would have to post the source code as well, and this is unrelated to my question, I think, because the source code is the same, that works well with gcc. – Rip Aug 30 '20 at 17:45
  • "doesn't work" isn't giving us a lot to work with. Error message would help. – Zyl Aug 30 '20 at 17:46
  • Ok, I added the error message. – Rip Aug 30 '20 at 18:55
  • Expansion of `~` is done by the shell. Maybe CMake does not do this. Did you try to use full path names for your C files? – Gerhardh Aug 30 '20 at 19:03
  • Yeah, I tried this as well. I will update the paths in the question. – Rip Aug 30 '20 at 19:11

1 Answers1

0

you need to tell Cmake that you want to link the libraries.

I use target_link_libraries for that. https://cmake.org/cmake/help/latest/command/target_link_libraries.html

0___________
  • 60,014
  • 4
  • 34
  • 74