godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.66k stars 503 forks source link

Importing code in editor fails with "undefined symbol error" #357

Open TheFamousRat opened 4 years ago

TheFamousRat commented 4 years ago

Hello, I've been following exactly this https://docs.godotengine.org/en/3.1/tutorials/plugins/gdnative/gdnative-cpp-example.html C++ Gdnative tutorial. Back when I was using Windows 10 it worked fine, but now I am on Ubuntu 18.04 and it doesn't work. The code (exactly the one used in the tutorial) compiles without any problem; however, when I try to import the libs into Godot, I get those errors :

Can't resolve symbol godot_gdnative_init. Error: /home/thefamousrat/Documents/Godot/test/src/bin/x11/libgdexample.so: undefined symbol: godot_gdnative_init modules/gdnative/gdnative.cpp:347 - Failed to obtain godot_gdnative_init symbol modules/gdnative/gdnative.cpp:488 - No valid library handle, can't get symbol from GDNative object modules/gdnative/nativescript/nativescript.cpp:1504 - No nativescript_init in "res://src/bin/x11/libgdexample.so" found

Anyone has any inputs on this ? I've seen this error pop up for other persons on Linux. I'm using Godot 3.1.2 and the latest Godot-cpp/godot_headers repos (I tried using the nativescript-1.1 branch, but to no avail).

Thank you

abrajam92 commented 4 years ago

I have the same error in OSX 10.14.6

E 0:00:00:0589   Can't resolve symbol godot_gdnative_init. Error: dlsym(0x7fcc5c43abf0, godot_gdnative_init): symbol not found
E 0:00:00:0589   Failed to obtain godot_gdnative_init symbol
E 0:00:00:0589   No valid library handle, can't get symbol from GDNative object
E 0:00:00:0589   No nativescript_init in "res://bin/osx/libgdexample.dylib" found

Also I compiled changing the src folder path instead of beside to inside the project "/demo" directory contrary to what the documentation says, other way I couln't compile.

FilipLundby commented 4 years ago

Similar, Godot 3.1.2, Manjaro 4.19

E 0:00:00:0257   Can't resolve symbol godot_gdnative_init. Error: /home/biip/Documents/Projects/test/demo/bin/x11/libgdexample.so: undefined symbol: godot_gdnative_init
E 0:00:00:0257   Failed to obtain godot_gdnative_init symbol
E 0:00:00:0257   No valid library handle, can't get symbol from GDNative object
E 0:00:00:0257   No nativescript_init in "res://bin/x11/libgdexample.so" found
sheepandshepherd commented 4 years ago

Not sure why the symbol is missing, but here's a few possibilities to try:

FilipLundby commented 4 years ago

Man this is so strange. I removed the project and started for scratch. And now it works for some reason. I've already done this 3 times, so I'm not sure why it's suddenly working.

I repeated the process one more time to see if I could replicate the errors. But still works just fine.

@sheepandshepherd No changes - it's all copy/paste from the documentation.

This is actually more or less just a summary of the documentation - which worked for me:

  1. Create project folder: mkdir gdnative_cpp_example
  2. Go into folder: cd gdnative_cpp_example
  3. Clone C++ GDNative: git clone --recursive https://github.com/GodotNativeTools/godot-cpp (worked with both master-branch and 3.1-branch)
  4. Go into folder: cd godot-cpp
  5. Build bindings: scons platform=linux generate_bindings=yes bits=64
  6. Create folder for our source code: mkdir gdnative_cpp_example/src
  7. Inside src add new files gdexample.h, gdexample.cpp and gdlibrary.cpp. See doc for soruce
  8. Download SConstruct and place it in the main folder gdnative_cpp_example
  9. Open Godot and create project at: gdnative_cpp_example/demo/
  10. Create bin folder inside demo: mkdir gdnative_cpp_example/demo/bin
  11. Add new files gdexample.gdns and gdexample.gdnlib inside bin. See doc for source
  12. From main folder gdnative_cpp_example run scons platform=x11
  13. Go back into Godot, add the sprite, apply the Godot icon and reference gdexample.gdns (As described at the end of this paragraph)

File/folder layout looks like:

gdnative_cpp_example
    |-- demo
    |    |-- bin
    |         |-- gdexample.gdnlib
    |         |-- gdexample.gdns
    |
    |-- godot-cpp
    |-- src
    |    |-- gdexample.h
    |    |-- gdlibrary.cpp
    |    |-- gdlibrary.cpp
    |
    |-- SConstruct

Here is a ZIP of the files - except for godot-cpp. Perhaps @TheFamousRat could make a file compare to see if there is any differences: godot3.1-stable_gdnative_cpp_test.zip

TheFamousRat commented 4 years ago

@FilipLundby, Thanks a lot for your comment. I did indeed try to redo everything from scratch and it worked too. However I may have found the culprit. It seems as though Godot doesn't like it when the the binaries aren't located precisely in the "res://bin/x11" folder, and also has the same reaction when the .gdns and .gdnlib files aren't located in the "res://bin" folder.

Here's my current project file tree : ├── bin │   ├── gdexample.gdnlib │   ├── gdexample.gdns │   └── x11 │   └── libgdexample.so ├── default_env.tres ├── icon.png ├── icon.png.import ├── Node │   ├── Node.tscn │   └── src │   ├── gdexample.cpp │   ├── gdexample.h │   ├── gdexample.os │   ├── gdlibrary.cpp │   ├── gdlibrary.os │   └── SConstruct └── project.godot

I also naturally modified the paths in SConstruct accordingly. Hope this helps.

sheepandshepherd commented 4 years ago

They can be anywhere in the project. Godot uses file paths to reference resources inside other resources, so just make sure to update those references: