Skip to content

Instantly share code, notes, and snippets.

@LensPlaysGames
Created December 31, 2022 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LensPlaysGames/c47dac4ef7c2cf0d9fe149800a34faa9 to your computer and use it in GitHub Desktop.
Save LensPlaysGames/c47dac4ef7c2cf0d9fe149800a34faa9 to your computer and use it in GitHub Desktop.
tree-sitter basic CMake project
cmake_minimum_required(VERSION 3.14)
# NOTE: Just replace all three occurences of `tree-sitter-c` to whatever language grammar you are compiling. Nothing else needs changed.
# Then just run `cmake -B bld` and `cmake --build bld` to get a shared object file that can be installed according to https://tree-sitter.github.io/tree-sitter/syntax-highlighting#paths .
project(tree-sitter-c)
add_library(
tree-sitter-c
SHARED
src/parser.c
)
target_include_directories(
tree-sitter-c
PUBLIC
src
)
@LensPlaysGames
Copy link
Author

LensPlaysGames commented Dec 31, 2022

If you get errors that look like this

FAILED: libtree-sitter-cpp.dll libtree-sitter-cpp.dll.a
cmd.exe /C "cd . && C:\TDM-GCC-64\bin\gcc.exe    -shared -o libtree-sitter-cpp.dll -Wl,--out-implib,libtree-sitter-cpp.dll.a -Wl,--major-image-version,0,--minor-image-version,0 CMakeFiles/tree-sitter-cpp.dir/src/parser.c.obj  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/tree-sitter-cpp.dir/src/parser.c.obj:parser.c:(.rdata+0x2153f8): undefined reference to `tree_sitter_cpp_external_scanner_create'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/tree-sitter-cpp.dir/src/parser.c.obj:parser.c:(.rdata+0x215400): undefined reference to `tree_sitter_cpp_external_scanner_destroy'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/tree-sitter-cpp.dir/src/parser.c.obj:parser.c:(.rdata+0x215408): undefined reference to `tree_sitter_cpp_external_scanner_scan'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/tree-sitter-cpp.dir/src/parser.c.obj:parser.c:(.rdata+0x215410): undefined reference to `tree_sitter_cpp_external_scanner_serialize'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/tree-sitter-cpp.dir/src/parser.c.obj:parser.c:(.rdata+0x215418): undefined reference to `tree_sitter_cpp_external_scanner_deserialize'
collect2.exe: error: ld returned 1 exit status

you will have to add src/scanner.c or src/scanner.cc to the list of sources in add_library

cmake_minimum_required(VERSION 3.14)
project(tree-sitter-cpp)

add_library(
  tree-sitter-cpp
  SHARED
  src/parser.c
  src/scanner.cc
)

target_include_directories(
  tree-sitter-cpp
  PUBLIC
  src
)

This is needed because some parsers write their own "external scanner" implementation that the tree sitter parser then uses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment