Godot can't find function definitions in a static library
Asked Answered
F

2

0

I'm trying to write a module using tree-sitter with some custom grammars. Here's a minimal example that works outside of Godot:

/* main.cpp */
#include <tree_sitter/api.h>

int main() {
  TSParser* p = ts_parser_new();
  return 0;
}
# SConstruct
env = Environment()

env.Append(LIBPATH="/home/teehee/git/tree-sitter")
env.Append(LIBS="tree-sitter")

env.Program("out", "main.cpp")

For the test in Godot, I made a trivial module and pasted in the above code:

/* printer_of_2s.cpp */
#include "printer_of_2s.h"
#include <tree_sitter/api.h>

int tt_fn() {
  TSParser* p = ts_parser_new();
  return 0;
}


void PrinterOf2s::print_two() {
  print_line(two);
}

void PrinterOf2s::_bind_methods() {
  ClassDB::bind_method(D_METHOD("print_two"), &PrinterOf2s::print_two);
}


PrinterOf2s::PrinterOf2s() {
  two = 2;
}
# SCsub
Import('env')

menv = env.Clone()
menv.add_source_files(env.modules_sources, "*.cpp")
menv.Append(CPPPATH="/home/teehee/git/tree-sitter/lib/include")

env.Append(LIBPATH="/home/teehee/git/tree-sitter")
env.Append(LIBS="tree-sitter")

After compiling with scons p=windows custom_modules=~/godot/modules/ -j 3 (I'm using WSL), I get a linker error:

/usr/bin/x86_64-w64-mingw32-ld: modules/libmodule_simple.windows.editor.x86_64.a(printer_of_2s.windows.editor.x86_64.o):printer_of_2s.:(.text+0x125): undefined reference to `ts_parser_new'

I know that the function is defined, because otherwise the first program wouldn't compile. I also know that scons can find the library, because that would give me a different error. I'm not sure what I could be doing differently.

Fiddler answered 15/2 at 23:35 Comment(0)
F
0

Solved with the help of jagh201 on the discord:

# SCsub
import os

Import('env')

env.add_source_files(env.modules_sources, "/home/teehee/git/tree-sitter/lib/src/lib.c")
env.Append(CPPPATH=["/home/teehee/git/tree-sitter/lib/include"
                     , "/home/teehee/git/tree-sitter/lib/src"])

env.StaticLibrary("/home/teehee/godot/modules/simple/ll/tree-sitter", "/home/teehee/git/tree-sitter/lib/src/lib.c")

env.Append(LIBPATH="/home/teehee/godot/modules/simple/ll")
env.Append(LIBS="tree-sitter")

menv = env.Clone()
menv.add_source_files(env.modules_sources, "*.cpp")

I had to let scons build the library itself. The call to StaticLibrary produced an object called libtree-sitter.windows.x86-64.a I renamed it libtree-sitter.a, commented out StaticLibrary, and it worked from there.

Fiddler answered 18/2 at 7:41 Comment(0)
G
0

Great that you solved your problem. I marked your post as "best answer"  👍

Glittery answered 18/2 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.