c++makefilecmakeemscriptengdcm

Emscripten library make from CMake cannot link


I am using Grassroot Dicom viewer version 2.8.9 to compile into Emscripten library bc on Windows 10 64-bit platform.

I followed the instruction from this website

emconfigure cmake .
emmake make VERBOSE=1

After that I got library file ending with .a instead of .bc similar to

libgdcmcharls.a libgdcmCommon.a libgdcmDICT.a ....

when I looked into the assembly code for each static library file it starts with

! arch./

instead of

BC

as mention in the tutorial website Note

Some build systems may not properly emit bitcode using the above procedure, and you may see is not valid bitcode warnings. You can run file to check if a file contains bitcode (also you can manually check if the contents start with BC). It is also worth running emmake make VERBOSE=1 which will print out the commands it runs - you should see emcc being used, and not the native system compiler. If emcc is not used, you may need to modify the configure or cmake scripts.

I think this is the problem that I cannot generate libraries with contents start with BC but instead I got ! arch ./ ?

and of course those files cannot be linked from emcc ( cannot find reference error )

error: undefined symbol: _ZN4gdcm11ImageReader8GetImageEv
error: undefined symbol: _ZN4gdcm11ImageReaderC1Ev
error: undefined symbol: _ZN4gdcm11ImageReaderD1Ev
error: undefined symbol: _ZN4gdcm5Trace14GetErrorStreamE

So I modified GDCM CMakefile.txt according to this

if (EMSCRIPTEN)
    set(CMAKE_AR "emcc" CACHE)
    set(CMAKE_STATIC_LIBRARY_SUFFIX ".bc")
    set(CMAKE_C_CREATE_STATIC_LIBRARY "<CMAKE_AR> -o <TARGET> <LINK_FLAGS> <OBJECTS>")
    set(CMAKE_CXX_CREATE_STATIC_LIBRARY "<CMAKE_AR> -o <TARGET> <LINK_FLAGS> <OBJECTS>")
endif()

However, it does not help anything. The "make" still generates .a library files

The command that I compiled my code as follows; I also copy all .a files to the same folder as main.cpp

emcc -std=c++17 -O3 -s WASM=1  -s USE_WEBGL2=1 -s FULL_ES3=1 -s ALLOW_MEMORY_GROWTH=1 -o hello.html  -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']" --no-heap-copy libgdcmcharls.a libgdcmCommon.a libgdcmDICT.a libgdcmDSED.a libgdcmexpat.a libgdcmIOD.a libgdcmjpeg12.a libgdcmjpeg16.a libgdcmjpeg8.a libgdcmMEXD.a libgdcmMSFF.a libgdcmopenjp2.a libgdcmzlib.a libgdcmuuid.a libsocketxx.a main.cpp ...........

The link is my GDCM 2.8.9 library files. Compiled in Windows10 using emcmake cmake and finally emmake make ( based on mingw32-make ).

Here is my CMakeCXXCompiler.cmake after calling emconfigure cmake .

set(CMAKE_CXX_COMPILER "C:/workspace/emsdk/emscripten/1.38.14/em++.bat")
set(CMAKE_CXX_COMPILER_ARG1 "")
set(CMAKE_CXX_COMPILER_ID "Clang")
set(CMAKE_CXX_COMPILER_VERSION "6.0.1")
set(CMAKE_CXX_COMPILER_VERSION_INTERNAL "")
set(CMAKE_CXX_COMPILER_WRAPPER "")
set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "98")
set(CMAKE_CXX_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters;cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates;cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates;cxx_std_17")
set(CMAKE_CXX98_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters")
set(CMAKE_CXX11_COMPILE_FEATURES "cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates")
set(CMAKE_CXX14_COMPILE_FEATURES "cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates")
set(CMAKE_CXX17_COMPILE_FEATURES "")
set(CMAKE_CXX20_COMPILE_FEATURES "")

set(CMAKE_CXX_PLATFORM_ID "emscripten")
set(CMAKE_CXX_SIMULATE_ID "")
set(CMAKE_CXX_SIMULATE_VERSION "")



set(CMAKE_AR "C:/workspace/emsdk/emscripten/1.38.14/emar.bat")
set(CMAKE_CXX_COMPILER_AR "C:/Program Files/LLVM/bin/llvm-ar.exe")
set(CMAKE_RANLIB "C:/workspace/emsdk/emscripten/1.38.14/emranlib.bat")
set(CMAKE_CXX_COMPILER_RANLIB "C:/Program Files/LLVM/bin/llvm-ranlib.exe")
set(CMAKE_LINKER "C:/Program Files/LLVM/bin/wasm-ld.exe")
set(CMAKE_COMPILER_IS_GNUCXX )
set(CMAKE_CXX_COMPILER_LOADED 1)
set(CMAKE_CXX_COMPILER_WORKS TRUE)
set(CMAKE_CXX_ABI_COMPILED )
set(CMAKE_COMPILER_IS_MINGW )
set(CMAKE_COMPILER_IS_CYGWIN )
if(CMAKE_COMPILER_IS_CYGWIN)
  set(CYGWIN 1)
  set(UNIX 1)
endif()

set(CMAKE_CXX_COMPILER_ENV_VAR "CXX")

if(CMAKE_COMPILER_IS_MINGW)
  set(MINGW 1)
endif()
set(CMAKE_CXX_COMPILER_ID_RUN 1)
set(CMAKE_CXX_IGNORE_EXTENSIONS inl;h;hpp;HPP;H;o;O;obj;OBJ;def;DEF;rc;RC)
set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;mm;CPP)
set(CMAKE_CXX_LINKER_PREFERENCE 30)
set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES 1)

# Save compiler ABI information.
set(CMAKE_CXX_SIZEOF_DATA_PTR "4")
set(CMAKE_CXX_COMPILER_ABI "")
set(CMAKE_CXX_LIBRARY_ARCHITECTURE "")

if(CMAKE_CXX_SIZEOF_DATA_PTR)
  set(CMAKE_SIZEOF_VOID_P "${CMAKE_CXX_SIZEOF_DATA_PTR}")
endif()

if(CMAKE_CXX_COMPILER_ABI)
  set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_CXX_COMPILER_ABI}")
endif()

if(CMAKE_CXX_LIBRARY_ARCHITECTURE)
  set(CMAKE_LIBRARY_ARCHITECTURE "")
endif()

set(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX "")
if(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX)
  set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_CXX_CL_SHOWINCLUDES_PREFIX}")
endif()





set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "")
set(CMAKE_CXX_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")

Solution

  • Finally I found the problem. The library that generated from command;

    emconfigure cmake .

    is not the emcc compatible even when I see the compiler it used em++.bat as the compiler.

    How to spot the problem.

    I use a program called "Hex Editor Neo" to open the library files ( use only small library files to test, big library files the program crashes).

    If it is not compatible to use with emcc. The first few character will be ! arch This will not work. see the picture below for the library file that is wrong generated from cmake;

    enter image description here

    Make sure that you have BC like in the picture below;

    enter image description here

    but how to do this

    you cannot just use

    emconfigure cmake .  <---------- wrong
    

    you need to put an option to generate bitcode (.bc) by following command

    emconfigure cmake . -DEMSCRIPTEN_GENERATE_BITCODE_STATIC_LIBRARIES=1
    

    Then it will generate all library files ending with .bc and these library files can be linked with emcc