Static linking to lime on Windows

Tried to get it working and failed:

lime rebuild windows -static
lime create SimpleImage lime_sample_image_static_test
cd lime_sample_image_static_test
lime test windows -static

This results in numerous linking errors, this is summary of them:

cl.exe -nologo -O2 /WX- /fp:precise -DHX_WINDOWS -D_USING_V120_SDK71_ -GR -Oy- -c -EHs -GS- -arch:SSE -Ic:/projects/jerix_net/hxcpp/include -DSTATIC_LINK -DHXCPP_VISIT_ALLOCS -DHXCPP_API_LEVEL=321 -D_CRT_SECURE_NO_DEPRECATE -D_ALLOW_MSC_VER_MISMATCH -D_ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH -DHX_WIN_MAIN -wd4996 -MT ./Main.cpp -Foobj/msvc18-stat-ncxp/3c79f9bd_Main.obj
Main.cpp
link.exe -out:Main.exe -nologo -machine:x86 -subsystem:windows,5.01 -libpath:lib user32.lib @obj/msvc18-stat-ncxp/all_objs c:\projects\jerix_net\hxcpp\lib/Windows/libstd.lib c:\projects\jerix_net\hxcpp\lib/Windows/libregexp.lib c:\projects\jerix_net\hxcpp\lib/Windows/libzlib.lib c:\projects\jerix_net\lime\ndll/Windows/liblime.lib gdi32.lib opengl32.lib user32.lib kernel32.lib advapi32.lib winmm.lib imm32.lib ole32.lib oleaut32.lib version.lib ws2_32.lib wldap32.lib shell32.lib comdlg32.lib ApplicationMain.lib
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflate already defined in libzlib.lib(69ebc970_inflate.obj)
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflateCopy already defined in libzlib.lib(69ebc970_inflate.obj)
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflateEnd already defined in libzlib.lib(69ebc970_inflate.obj)
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflateGetHeader already defined in libzlib.lib(69ebc970_inflate.obj)
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflateInit2_ already defined in libzlib.lib(69ebc970_inflate.obj)
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflateInit_ already defined in libzlib.lib(69ebc970_inflate.obj)
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflatePrime already defined in libzlib.lib(69ebc970_inflate.obj)
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflateReset already defined in libzlib.lib(69ebc970_inflate.obj)
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflateSetDictionary already defined in libzlib.lib(69ebc970_inflate.obj)
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflateSync already defined in libzlib.lib(69ebc970_inflate.obj)
liblime.lib(5c4c0827_inflate.obj) : error LNK2005: _inflateSyncPoint already defined in libzlib.lib(69ebc970_inflate.obj)
   Creating library Main.lib and object Main.exp
3c79f9bd_Main.obj : error LNK2019: unresolved external symbol _hxRunLibrary referenced in function _WinMain@16
3c79f9bd_Main.obj : error LNK2019: unresolved external symbol _hxcpp_set_top_of_stack referenced in function _WinMain@16
liblime.lib(ee5a9484_MouseEvent.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(ee5a9484_TouchEvent.obj) : error LNK2001: unresolved external symbol _val_id

...

liblime.lib(ee5a9484_KeyEvent.obj) : error LNK2001: unresolved external symbol _val_call0
Main.exe : fatal error LNK1120: 65 unresolved externals
Error: error running link.exe -out:Main.exe -nologo -machine:x86 -subsystem:windows,5.01 -libpath:lib user32.lib @obj/msvc18-stat-ncxp/all_objs c:\projects\jerix_net\hxcpp\lib/Windows/libstd.lib c:\projects\jerix_net\hxcpp\lib/Windows/libregexp.lib c:\projects\jerix_net\hxcpp\lib/Windows/libzlib.lib c:\projects\jerix_net\lime\ndll/Windows/liblime.lib gdi32.lib opengl32.lib user32.lib kernel32.lib advapi32.lib winmm.lib imm32.lib ole32.lib oleaut32.lib version.lib ws2_32.lib wldap32.lib shell32.lib comdlg32.lib ApplicationMain.lib

How can I fix this?

The already defined errors are because zlib is added twice, probably once by lime and once by hxcpp, you’ll have to remove the inclusion in one of them.

There is solution for that zlib double linked…
But what about this numerous undefined reference erros? Maybe I’m doing something wrong?

cl.exe -nologo -O2 /WX- /fp:precise -DHX_WINDOWS -D_USING_V120_SDK71_ -GR -Oy- -c -EHs -GS- -arch:SSE -Ic:/projects/jerix_net/hxcpp/include -DSTATIC_LINK -DHXCPP_VISIT_ALLOCS -DHXCPP_API_LEVEL=321 -D_CRT_SECURE_NO_DEPRECATE -D_ALLOW_MSC_VER_MISMATCH -D_ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH -DHX_WIN_MAIN -wd4996 -MT ./Main.cpp -Foobj/msvc18-stat-ncxp/3c79f9bd_Main.obj
Main.cpp
link.exe -out:Main.exe -nologo -machine:x86 -subsystem:windows,5.01 -libpath:lib user32.lib @obj/msvc18-stat-ncxp/all_objs c:\projects\jerix_net\hxcpp\lib/Windows/libstd.lib c:\projects\jerix_net\hxcpp\lib/Windows/libregexp.lib c:\projects\jerix_net\hxcpp\lib/Windows/libzlib.lib c:\projects\jerix_net\lime\ndll/Windows/liblime.lib gdi32.lib opengl32.lib user32.lib kernel32.lib advapi32.lib winmm.lib imm32.lib ole32.lib oleaut32.lib version.lib ws2_32.lib wldap32.lib shell32.lib comdlg32.lib ApplicationMain.lib
   Creating library Main.lib and object Main.exp
3c79f9bd_Main.obj : error LNK2019: unresolved external symbol _hxRunLibrary referenced in function _WinMain@16
3c79f9bd_Main.obj : error LNK2019: unresolved external symbol _hxcpp_set_top_of_stack referenced in function _WinMain@16
liblime.lib(ee5a9484_MouseEvent.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(ee5a9484_TouchEvent.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(ee5a9484_WindowEvent.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(01ae46f8_OpenGLBindings.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(d513e181_Font.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(355208de_UpdateEvent.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(ee5a9484_GamepadEvent.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(ee5a9484_KeyEvent.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(e9eeb40c_ColorMatrix.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(e9eeb40c_Rectangle.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(e9eeb40c_Vector2.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(eddb7f48_SDLRenderer.obj) : error LNK2001: unresolved external symbol _val_id
libzlib.lib(541ae31c_ZLib.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(42ca91d4_AudioBuffer.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(66eb8e3d_ImageBuffer.obj) : error LNK2001: unresolved external symbol _val_id
liblime.lib(66eb8e3d_Image.obj) : error LNK2001: unresolved external symbol _val_id
libstd.lib(febcda1e_Init.obj) : error LNK2001: unresolved external symbol _val_id
libstd.lib(febcda1e_Sys.obj) : error LNK2001: unresolved external symbol _val_id
libstd.lib(febcda1e_Xml.obj) : error LNK2001: unresolved external symbol _val_id
libregexp.lib(91fe270a_RegExp.obj) : error LNK2001: unresolved external symbol _val_id
libstd.lib(febcda1e_Init.obj) : error LNK2019: unresolved external symbol _kind_share referenced in function _std_main
libregexp.lib(91fe270a_RegExp.obj) : error LNK2001: unresolved external symbol _val_throw
libzlib.lib(541ae31c_ZLib.obj) : error LNK2001: unresolved external symbol _val_throw
liblime.lib(38eda3da_ByteArray.obj) : error LNK2001: unresolved external symbol _val_throw
liblime.lib(01ae46f8_OpenGLBindings.obj) : error LNK2001: unresolved external symbol _val_throw
libstd.lib(febcda1e_File.obj) : error LNK2001: unresolved external symbol _val_throw
libstd.lib(febcda1e_Process.obj) : error LNK2001: unresolved external symbol _val_throw
libstd.lib(febcda1e_Socket.obj) : error LNK2001: unresolved external symbol _val_throw
libstd.lib(febcda1e_Xml.obj) : error LNK2001: unresolved external symbol _val_throw
libregexp.lib(91fe270a_RegExp.obj) : error LNK2001: unresolved external symbol _hx_fail
libzlib.lib(541ae31c_ZLib.obj) : error LNK2001: unresolved external symbol _hx_fail
libstd.lib(febcda1e_Socket.obj) : error LNK2001: unresolved external symbol _hx_fail
libstd.lib(febcda1e_String.obj) : error LNK2001: unresolved external symbol _hx_fail
libstd.lib(febcda1e_Sys.obj) : error LNK2001: unresolved external symbol _hx_fail
libstd.lib(febcda1e_Xml.obj) : error LNK2001: unresolved external symbol _hx_fail
libstd.lib(febcda1e_File.obj) : error LNK2001: unresolved external symbol _hx_fail
libstd.lib(febcda1e_Misc.obj) : error LNK2001: unresolved external symbol _hx_fail
libstd.lib(febcda1e_Process.obj) : error LNK2001: unresolved external symbol _hx_fail
libstd.lib(febcda1e_Random.obj) : error LNK2001: unresolved external symbol _hx_fail

Hmm, seems like something in the HXCPP static build, perhaps this was broken with a newer HXCPP, or perhaps the reverse – maybe updating HXCPP could make a difference?

I’m using hxcpp from git for testing static link. I’ll try it with hxcpp haxelib version and post about results.

No, still same errors even on haxelib version of hxcpp

When you do a -Dstatic_link build (which -static should define), HXCPP is supposed to include the “RunLibs.cpp” file

<file name = "src/hx/RunLibs.cpp" if="static_link"/>

It has the implementation of hxRunLibrary:

const HX_CHAR *hxRunLibrary()
{
   std_register_prims();
   regexp_register_prims();
   zlib_register_prims();
    
   try { 
      __hxcpp_lib_main();
      return 0;
   }
   catch ( Dynamic d ) {
      sgResultBuffer = d->toString().__s;
      return sgResultBuffer.c_str();
   }
}

You could check the contents of the generated “BuildMain.xml” file, see what NDLLs are listed to be linked. We can double-check that libstd is on the list

I’ve got it working!
here is my editings in lime:

 include.xml                        | 2 +-
 templates/cpp/static/BuildMain.xml | 5 ++++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/include.xml b/include.xml
index f1dc110..0f18dcb 100644
--- a/include.xml
+++ b/include.xml
@@ -41,7 +41,7 @@
        
        <ndll name="std" haxelib="hxcpp" if="cpp" />
        <ndll name="regexp" haxelib="hxcpp" if="cpp" />
-       <ndll name="zlib" haxelib="hxcpp" if="cpp" unless="emscripten || ios" />
+       <ndll name="zlib" haxelib="hxcpp" if="cpp" unless="emscripten || ios || static_link" />
        
        <ndll name="lime" if="included" />
        
diff --git a/templates/cpp/static/BuildMain.xml b/templates/cpp/static/BuildMain.xml
index 6771bd0..10ba915 100644
--- a/templates/cpp/static/BuildMain.xml
+++ b/templates/cpp/static/BuildMain.xml
@@ -5,6 +5,9 @@
    <files id="main">
        
        <file name="Main.cpp" />
+
+       <compilerflag value="-I${HXCPP}/project/thirdparty/zlib-1.2.3" if="static_link" />
+       <file name="${HXCPP}/project/libs/zlib/ZLib.cpp" if="static_link" />
        
    </files>
    
@@ -78,7 +81,7 @@
            
        </section>
        
-       <lib name="ApplicationMain${DBG}${LIBEXT}" />
+       <lib name="libApplicationMain${DBG}${LIBEXT}" />
        
    </target>
    

PiratePig builds and runs but without sound and with this output:

Primitive not found : lime_cairo_arc__MULT
Primitive not found : lime_cairo_arc_negative__MULT
Primitive not found : lime_cairo_clip__1
Primitive not found : lime_cairo_clip_preserve__1
Primitive not found : lime_cairo_clip_extents__5
Primitive not found : lime_cairo_close_path__1
Primitive not found : lime_cairo_copy_page__1
Primitive not found : lime_cairo_create__1
Primitive not found : lime_cairo_curve_to__MULT
Primitive not found : lime_cairo_destroy__1
Primitive not found : lime_cairo_fill__1
Primitive not found : lime_cairo_fill_extents__5
Primitive not found : lime_cairo_fill_preserve__1
Primitive not found : lime_cairo_get_antialias__1
Primitive not found : lime_cairo_get_current_point__1
Primitive not found : lime_cairo_get_dash__1
Primitive not found : lime_cairo_get_dash_count__1
Primitive not found : lime_cairo_get_fill_rule__1
Primitive not found : lime_cairo_get_group_target__1
Primitive not found : lime_cairo_get_line_cap__1
Primitive not found : lime_cairo_get_line_join__1
Primitive not found : lime_cairo_get_line_width__1
Primitive not found : lime_cairo_get_matrix__1
Primitive not found : lime_cairo_get_miter_limit__1
Primitive not found : lime_cairo_get_operator__1
Primitive not found : lime_cairo_get_reference_count__1
Primitive not found : lime_cairo_get_source__1
Primitive not found : lime_cairo_get_target__1
Primitive not found : lime_cairo_get_tolerance__1
Primitive not found : lime_cairo_has_current_point__1
Primitive not found : lime_cairo_identity_matrix__1
Primitive not found : lime_cairo_in_clip__3
Primitive not found : lime_cairo_in_fill__3
Primitive not found : lime_cairo_in_stroke__3
Primitive not found : lime_cairo_line_to__3
Primitive not found : lime_cairo_mask__2
Primitive not found : lime_cairo_mask_surface__4
Primitive not found : lime_cairo_move_to__3
Primitive not found : lime_cairo_new_path__1
Primitive not found : lime_cairo_paint__1
Primitive not found : lime_cairo_paint_with_alpha__2
Primitive not found : lime_cairo_pop_group__1
Primitive not found : lime_cairo_pop_group_to_source__1
Primitive not found : lime_cairo_push_group__1
Primitive not found : lime_cairo_push_group_with_content__2
Primitive not found : lime_cairo_rectangle__5
Primitive not found : lime_cairo_reference__1
Primitive not found : lime_cairo_rel_curve_to__MULT
Primitive not found : lime_cairo_rel_line_to__3
Primitive not found : lime_cairo_rel_move_to__3
Primitive not found : lime_cairo_reset_clip__1
Primitive not found : lime_cairo_restore__1
Primitive not found : lime_cairo_save__1
Primitive not found : lime_cairo_set_antialias__2
Primitive not found : lime_cairo_set_dash__2
Primitive not found : lime_cairo_set_fill_rule__2
Primitive not found : lime_cairo_set_line_cap__2
Primitive not found : lime_cairo_set_line_join__2
Primitive not found : lime_cairo_set_line_width__2
Primitive not found : lime_cairo_set_matrix__2
Primitive not found : lime_cairo_set_miter_limit__2
Primitive not found : lime_cairo_set_operator__2
Primitive not found : lime_cairo_set_source__2
Primitive not found : lime_cairo_set_source_rgb__4
Primitive not found : lime_cairo_set_source_rgba__5
Primitive not found : lime_cairo_set_source_surface__4
Primitive not found : lime_cairo_set_tolerance__2
Primitive not found : lime_cairo_show_page__1
Primitive not found : lime_cairo_status__1
Primitive not found : lime_cairo_stroke__1
Primitive not found : lime_cairo_stroke_extents__5
Primitive not found : lime_cairo_stroke_preserve__1
Primitive not found : lime_cairo_transform__2
Primitive not found : lime_cairo_translate__3
Primitive not found : lime_cairo_version__0
Primitive not found : lime_cairo_version_string__0
Primitive not found : lime_cairo_pattern_add_color_stop_rgb__5
Primitive not found : lime_cairo_pattern_add_color_stop_rgba__MULT
Primitive not found : lime_cairo_pattern_create_for_surface__1
Primitive not found : lime_cairo_pattern_create_linear__4
Primitive not found : lime_cairo_pattern_create_radial__MULT
Primitive not found : lime_cairo_pattern_create_rgb__3
Primitive not found : lime_cairo_pattern_create_rgba__4
Primitive not found : lime_cairo_pattern_destroy__1
Primitive not found : lime_cairo_pattern_get_color_stop_count__1
Primitive not found : lime_cairo_pattern_get_extend__1
Primitive not found : lime_cairo_pattern_get_filter__1
Primitive not found : lime_cairo_pattern_get_matrix__1
Primitive not found : lime_cairo_pattern_set_extend__2
Primitive not found : lime_cairo_pattern_set_filter__2
Primitive not found : lime_cairo_pattern_set_matrix__2
Primitive not found : lime_cairo_image_surface_create__3
Primitive not found : lime_cairo_image_surface_create_for_data__5
Primitive not found : lime_cairo_image_surface_get_height__1
Primitive not found : lime_cairo_image_surface_get_width__1
Primitive not found : lime_cairo_surface_destroy__1
Primitive not found : lime_cairo_surface_flush__1

Thanks! I’ve got it working now too :smile:

OpenAL is excluded from static builds, due to the LGPL license. Perhaps we could consider an SDL mixer path, or some way to force OpenAL to be included, even if it taints the license of the whole executable

Great!

Is it possible to link to OpenAL in fully dynamic way? I mean link to it statically an in runtime check if there is dynamic lib nearby and link to it if there is?

Now I’m trying build lime with mingw and got this:

Error: In file included from ./src/audio/format/OGG.cpp:2:0:
include/system/System.h:98:2: error: #error "Use this header only with Microsoft Visual C++ compilers!"
 #error "Use this header only with Microsoft Visual C++ compilers!"
  ^

// I can’t get enough, can I? :smiley:

If you look at “project/include/system/System.h”, there’s code like this:

#ifndef HX_WINDOWS
#include <stdint.h>
#else

There’s a stdint.h implementation afterward that is specific to MSVC, I’m guessing that this define (and perhaps others) would need to be adapted to work only #ifdef _MSC_VER and not HX_WINDOWS