Haxe - Create a C++ Stand-alone executable

岁酱吖の 提交于 2019-12-07 15:41:17

问题


I have written a haxe program that tries to communicate with a remote server. I was able to compile to the C++ target successfully. The executable runs just fine on my system. However, when I try to run the same on another windows box, it fails with the following error

Error: Could not load module std@socket_init__0

I then installed haxe and hxcpp which worked like a charm. I was able to run the exe. I understand now that there is dependency on hxcpp.

That still did not solve my problem as I want to create a stand-alone application. After some research I found a file (ExampleMain.CPP) with the following instructions that I think might solve my problem. However, I am a novice and do not quite follow. Can some one walk me through with this? Thanks

ExampleMain.CPP

This is an example mainline that can be used to link a static version. First you need to build the static version of the standard libs, with:

cd $HXCPP/runtime

haxelib run hxcpp BuildLibs.xml -Dstatic_link

Then the static verion of your application with (note: extra space before 'static_link'):

haxe -main YourMain -cpp cpp -D static_link

You then need to link the above libraries with this (or a modified version) main. You may choose to create a VisualStudio project, and add the libraries from $HXCPP/bin/Windows/(std,regexp,zlib).lib and your application library.

Note also, that if you compile with the -debug flag, your library will have a different name.

Linking from the command line for windows (user32.lib only required for debug version):

cl ExampleMain.cpp cpp/YourMain.lib $HXCPP/bin/Windows/std.lib $HXCPP/bin/Windows/zlib.lib $HXCPP/bin/Windows/regexp.lib user32.lib

From other OSs, the compile+link command will be different. Here is one for mac:

g++ ExampleMain.cpp cpp/Test-debug.a $HXCPP/bin/Mac/regexp.a $HXCPP/bin/Mac/std.a $HXCPP/bin/Mac/zlib.a

If you wish to add other static libraries besides these 3 (eg, nme) you will need to compile these with the "-Dstatic_link" flag too, and call their "register_prims" init call. The inclusion of the extra static library will require the library in the link line, and may requires additional dependencies to be linked. Also note, that there may be licensing implications with static linking thirdparty libraries.


回答1:


I'm not sure, but it seems that you are taking the same extra steps hxcpp does for you already. When you compile your standalone application it is actually standalone and doesn't have a dependency on hxcpp per se - but it has a dependency on the standard libraries within hxcpp you may have used. For instance, if you use regular expressions, you will need the regexp.dll that hxcpp has for it, as you noted. The haxe standard library is in the std.dll and the zlib is if you used compression from the zip packages.

If I am not mistaken, the default is to reference these components dynamically. In order for your application to be standalone as you suggest, you simply have to copy these dll's alongside your binary.

If you want to link to these library components statically, automatically from your haxe code, just import the types from the cpp.link package. This instructs hxcpp to automatically bring its libraries as part of the compilation, linking it statically into your binary instead of dynamically. No extra steps are necessary!

Short answer: add import cpp.link.StaticStd; and any other library components in the link package somewhere to your code. It can be anywhere as long as it's imported, it will be linked in.



来源:https://stackoverflow.com/questions/40930486/haxe-create-a-c-stand-alone-executable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!