Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Haxe hxcpp wrapper for a C library

Tags:

c++

c

haxe

hxcpp

since a couples of days I am trying to write a Haxe hxcpp wrapper for linenoise which is programmed in C code; the library is very simple and the header also contains an extern C. I am following the snowkit linc macro template but I can't get a way to compile the C module and that it links with the rest of the project, I am not sure how to continue.

I have no problems to compile the code as a C object and link it with a C executable in my system (OSX el Capitan) so I assume I am doing something wrong in my haxe project, perhaps I can't really link hxcpp with a C library using directly the build process from haxe or I should pipe it manually by writing per hand the commands.

@:keep
@:structAccess
@:include('linenoise.h')
#if !display
@:build(linc.Linc.touch())
@:build(linc.Linc.xml('linenoise'))
#end

extern class LineNoise {
 @:native("linenoiseClearScreen")
 static function linenoiseClearScreen(): Void;
} //LineNoise

Recently I am getting this error:

g++ -o Test-debug -stdlib=libstdc++ -framework Cocoa -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -m64 @/Users/vresko/projects/linenoise/test/cpp/obj/darwin64-debug/all_objs 

Undefined symbols for architecture x86_64: 

 "_linenoiseClearScreen", referenced from: 

 Test_obj::main() in ab184b9a_Test.o 

ld: symbol(s) not found for architecture x86_64 

clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am aware that this error has several mentions, but I am not sure how to solve that within the context of haxe hxcpp.

If I use a C++ wrapper including the hxcpp.h as the typical linc example does the error is still the same, and the function I am declaring here (linenoiseClearScreen) is actually a trivial print statement .

I also read about other possibilities such as CFFI for neko to create a wrapper around the library (I may have read everything on the internet about it) but I wanted to keep the code static linked if possible and compatible with all hxcpp targets.

like image 628
Víctor R. Escobar Avatar asked Dec 09 '15 00:12

Víctor R. Escobar


1 Answers

sounds like you are missing a step when setting up your dependencies for hxcpp, in your case linenoise. hxcpp doesnt know about the .c-file.

This is obviously missing in the empty template since there is no actual dependency used.

Anyway, i'm the author of https://github.com/snowkit/linc_enet, the binding for ENet for hxcpp. It might help you to compare your setup to a more complete example like this.

In this case, ENet, as a dependency, is setup as a special hxcpp git submodule inside the lib-folder. It can be found here as part of the native-toolkit: https://github.com/native-toolkit/enet

What you should look at are the 2 xml-files defines.xml and files.xml. They basically describe the dependency for hxcpp. you can literally copy'n paste those, adapt the defines and list of files for linenoise.

Also, I wouldnt recommend including linenoise.h directly via @:include in the binding. Linc-libraries use an indirection at this level(see the linc-folder in the linc_enet-root) to allow for extensions/helpers of/for the binding on the C++-side without touching the actual dependencies.

If you follow that concept see linc/linc_enet.xml where everything is tied together for compilation.

like image 197
Michael Bickel Avatar answered Nov 15 '22 18:11

Michael Bickel