title: Simple android binary crashing with zlib
body: <p>I made a simple android executable with CMake that links with the native zlib on the ndk. Everything compiles correctly, but when calling <code>deflateInit</code> I get a segmentation fault.</p>
  
  <p>Here is the code:</p>
  
  <p><code>main.cpp</code></p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;zlib.h&gt;
  
  int main()
  {
      z_stream strm;
      deflateInit(&amp;strm, Z_DEFAULT_COMPRESSION);
      std::cout &lt;&lt; "it works!" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>And the corresponding <code>CMakeLists.txt</code></p>
  
  <pre class="lang-cmake prettyprint-override"><code>cmake_minimum_required(VERSION 3.8)
  project(zlib-on-android)
  
  add_executable(main main.cpp)
  
  find_package(ZLIB REQUIRED)
  
  set_target_properties(main
    PROPERTIES POSITION_INDEPENDENT_CODE ON)
  
  target_link_libraries(main PUBLIC ZLIB::ZLIB)
  </code></pre>
  
  <p>I then compile with the following command:</p>
  
  <p><code>cmake -H. -Bbuild -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_NDK=~/android-ndk-r17b -DCMAKE_ANDROID_STL_TYPE=c++_static -DCMAKE_SYSTEM_VERSION=16 -DCMAKE_ANDROID_NDK_TOOLCHAIN_VERSION=clang</code></p>
  
  <p>Everything works fine. Then I do the following:</p>
  
  <pre><code>cd build
  adb push main /data/local/tmp/.
  adb shell
  shell@device:/ $ cd /data/local/tmp
  shell@device:/ $ ./main
  [1] + Stopped (signal)     ./main
  </code></pre>
  
  <p>Does anyone know the reason? I am having a lot of trouble trying to hook up gdb with this executable. Since the same code works on my  Macosx, I cannot understand why it does not work on android.</p>
  
  <h3>EDIT</h3>
  
  <p>For some reason the same code crashes on Macosx (the one that worked was a similar code). This is the error: <code>bus error ./main</code></p>