Experiences with native extensions (SOLVED)

Hello,

I want to make a native extension.
I’ve followed the tutorial here: Writing native extensions for Defold

A remark about this tutorial: you’ll have to add #include<malloc.h> , otherwise you get an error at compilation.

Ok, then i tried to put the Rot13 function in another unit compilation (another file), prototyping it in the main source with an extern.

main.cpp contains the whole Lua+Defold definitions,
fx_helpers.cpp contains only Rot13 function.

When compiling with C-b , i get this error at linking stage:

/test_extension/ext.manifest
   /tmp/job5528805850526081991/build/lib8fd472ce-c858-4136-a9df-a4cffef89179.a(main.cpp_0.o):(.[data.rel.ro](https://slack-redir.net/link?url=http%3A%2F%2Fdata.rel.ro&amp;v=3)+0x8): undefined reference to `Rot13(lua_State*)'

I tried and added fx_helpers.o to linkFlags in ext.manifest , but didn’t work either.

So, how can i tell to the linker to use fx_helpers.o ?

I’m joining the project sample, if you want to cast a glance at it.
extension.zip (4.8 KB)

Thank you for reading :slight_smile:

2 Likes

As far as I can see:

  • Your manifest file is wrong. You don’t need empty platform declarations. Remove them and just left the name:
name: "TestExtension"
  • Just for caution use folder name like this: testextension not ‘test_extension’
  • You don’t need to extern Rot13 function. If you want to use separate c++ files use headers and include it in.
  • Looks like you don’t need malloc.h. just remove it
  • fx_helpers is not a C++ file it is a C file with .c extension. Use .cpp instead
2 Likes

Here is the working example:

ne.zip (7.7 KB)

2 Likes

Thank you for your answer !

I’ve looked your working example, but you are returning to the original sample (only one source file).

At last, i’ve found the answer : in the helper source file, the function was declared static, so invisible outside of this compilation unit (my bad: stupid cut’n’paste from the original sample).

I’ve just removed the static keyword , without changing anything else, and everything was fine.

There is just one thing i still don’t understand: how can you compile without including malloc.h ? If i don’t include it, i get those errors:

/test_extension/src/fx_helpers.c
	Line 13: ‘malloc’ was not declared in this scope
     char *rot = (char *) malloc(len + 1);
                                        ^
	Line 34: ‘free’ was not declared in this scope
     free(rot);
             ^

Thank you again for the time spent on my issue :slight_smile:

3 Likes

You are correct, this file should work on all platforms, so including malloc.h is ofc correct. I’ll fix the documentation.
The reason why it works for others is that it’s most likely an include that gets added from the sdk.h.

1 Like