Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

mySQL connector C++ lib to Android NDK

93 views
Skip to first unread message

skyLINE development Team

unread,
Jan 6, 2025, 9:30:40 AMJan 6
to android-ndk
Hi every one im trying to compile mysql connector c++ from sources Im using Ubuntu 24.04 x86_64 target Android x86_64 (as emulator machine for dev and testing) other target armv8 armv7

"All Operating Systems (Generic) (Architecture Independent), Compressed TAR Archive" https://dev.mysql.com/get/Downloads/Connector-C++/mysql-connector-c++-9.1.0-src.tar.gz

edit source of mysqlconnector struct Srv_host_detail  version using getaddrinfo  for Android becose standart resolv.h doesnt support.

I've precompiled  for Android x86_64
-OpenSSL
-ZSTD
-LZ4
-ZLIV
-PROTOBUF

cmake .. \
-DCMAKE_C_COMPILER=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/clang  \
-DCMAKE_CXX_COMPILER=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android34-clang++ \
-DCMAKE_C_FLAGS="-I/home/ripper/android-ndk/android-ndk-r27c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include" \
-DCMAKE_CXX_FLAGS="-I/home/ripper/android-ndk/android-ndk-r27c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include" \  
-DANDROID_ABI=x86_64 \
-DANDROID_PLATFORM=android-34 \
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake \
-DCMAKE_INSTALL_PREFIX=/home/ripper/sources_pck/android_libs/mysql-connector-cpp \
-DWITH_SSL=/home/ripper/sources_pck/android_libs/openssl-3.4.0-android/lib/libssl \
-DWITH_ZSTD=/home/ripper/sources_pck/android_libs/zstd-android-x86_64 \
-DWITH_LZ4=/home/ripper/sources_pck/android_libs/lz4-android-x86_64 \
-DWITH_ZLIB=/home/ripper/sources_pck/android_libs/zlib-1.3.1-android-x86_64 \
-DWITH_PROTOBUF=/home/ripper/sources_pck/android_libs/protobuf-android-x86_64 \
-DPROTOBUF_PROTOC_EXECUTABLE=/usr/bin/protoc-30.0.0 \
-DCMAKE_BUILD_TYPE=Release \
-DWITH_UNIT_TESTS=OFF \
-DWITH_EXAMPLES=OFF \
-DWITH_JDBC=OFF

/usr/bin/protoc-30.0.0  Ubuntu runnable

/usr/protobuf-android-x86_64/bin/protoc-30.0.0: error while loading shared libraries: /lib/x86_64-linux-gnu/libm.so: invalid ELF header - its precompiled

ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, with debug_info, not stripped Android runnable


but i cant compile it ((( 

if i compilling with path to procompiled libs i got that error


[1%] Building CXX object CMakeFiles/save_linker_opts.dir/cmake/libutils/save_linker_opts.cc.o [ 2%] Linking CXX executable libutils/save_linker_opts [ 2%] Built target save_linker_opts [ 4%] Building CXX object cdk/foundation/CMakeFiles/cdk_foundation.dir/error.cc.o [ 5%] Building CXX object cdk/foundation/CMakeFiles/cdk_foundation.dir/stream.cc.o [ 6%] Building CXX object cdk/foundation/CMakeFiles/cdk_foundation.dir/connection_tcpip.cc.o [ 8%] Building CXX object cdk/foundation/CMakeFiles/cdk_foundation.dir/socket.cc.o [ 9%] Building CXX object cdk/foundation/CMakeFiles/cdk_foundation.dir/diagnostics.cc.o [ 10%] Building CXX object cdk/foundation/CMakeFiles/cdk_foundation.dir/socket_detail.cc.o [ 12%] Building CXX object cdk/foundation/CMakeFiles/cdk_foundation.dir/connection_openssl.cc.o [ 13%] Linking CXX static library libcdk_foundation.a [ 13%] Built target cdk_foundation [ 15%] Preparing protobuf files for protobuf-lite [ 16%] Running C++ protocol buffer compiler on /home/ripper/sources_pck/mysql-connector-c++-9.1.0-src/build/cdk/protocol/mysqlx/lite/mysqlx_sql.proto /bin/sh: 1: /home/ripper/sources_pck/android_libs/protobuf-android-x86_64/bin/protoc: not found make[2]: *** [cdk/protocol/mysqlx/CMakeFiles/cdk_proto_mysqlx.dir/build.make:155: cdk/protocol/mysqlx/protobuf/mysqlx_sql.pb.cc] ERROR 127 make[1]: *** [CMakeFiles/Makefile2:1063: cdk/protocol/mysqlx/CMakeFiles/cdk_proto_mysqlx.dir/all] ERROR 2 make: *** [Makefile:136: all] ERROR 2

if i doesnt give any protoc path
i have that

[ 99%] Linking CXX static library libprotocd.a
[ 99%] Built target libprotoc
[ 99%] Building CXX object protobuf-3.19.6/cmake/CMakeFiles/protoc.dir/__/src/google/protobuf/compiler/main.cc.o
warning: unknown warning option '-Wno-stringop-overflow'; did you mean '-Wno-shift-overflow'? [-Wunknown-warning-option]
warning: unknown warning option '-Wno-stringop-overread' [-Wunknown-warning-option]
2 warnings generated.
[100%] Linking CXX executable ../../runtime_output_directory/protoc
ld.lld: error: undefined symbol: __android_log_write
>>> referenced by common.cc:146 (/home/ripper/sources_pck/mysql-connector-c++-9.1.0-src/cdk/extra/protobuf/protobuf-3.19.6/src/google/protobuf/stubs/common.cc:146)
>>>               common.cc.o:(google::protobuf::internal::DefaultLogHandler(google::protobuf::LogLevel, char const*, int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char>> const&)) in archive libprotobufd.a
>>> referenced by common.cc:154 (/home/ripper/sources_pck/mysql-connector-c++-9.1.0-src/cdk/extra/protobuf/protobuf-3.19.6/src/google/protobuf/stubs/common.cc:154)
>>>               common.cc.o:(google::protobuf::internal::DefaultLogHandler(google::protobuf::LogLevel, char const*, int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char>> const&)) in archive libprotobufd.a
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[5]: *** [protobuf-3.19.6/cmake/CMakeFiles/protoc.dir/build.make:103: runtime_output_directory/protoc-3.19.6.0] Помилка 1
gmake[4]: *** [CMakeFiles/Makefile2:209: protobuf-3.19.6/cmake/CMakeFiles/protoc.dir/all] Помилка 2
gmake[3]: *** [Makefile:91: all] Помилка 2
CMake Error at /home/ripper/sources_pck/mysql-connector-c++-9.1.0-src/cdk/cmake/ext/ext-build.cmake:55 (message):
  External build failed: 2


make[2]: *** [cdk/protocol/mysqlx/CMakeFiles/protobuf-build.dir/build.make:70: cdk/protocol/mysqlx/CMakeFiles/protobuf-build] Помилка 1
make[1]: *** [CMakeFiles/Makefile2:1086: cdk/protocol/mysqlx/CMakeFiles/protobuf-build.dir/all] Помилка 2
make: *** [Makefile:136: all] Помилка 2

Dan Albert

unread,
Jan 7, 2025, 5:33:27 PMJan 7
to andro...@googlegroups.com
-DCMAKE_C_COMPILER=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/clang  \
-DCMAKE_CXX_COMPILER=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android34-clang++ \
-DCMAKE_C_FLAGS="-I/home/ripper/android-ndk/android-ndk-r27c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include" \
-DCMAKE_CXX_FLAGS="-I/home/ripper/android-ndk/android-ndk-r27c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include" \  

Don't do things like this. That's the toolchain file's job, and adding these arguments likely interferes with the toolchain file. These are the only flags you need to configure CMake for Android:

-DANDROID_ABI=x86_64 \
-DANDROID_PLATFORM=android-34 \
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake \


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/android-ndk/5a6cda9c-2264-4418-9de5-99ae0ba1d01en%40googlegroups.com.

Andrea Fiorito

unread,
Jan 9, 2025, 9:40:10 AMJan 9
to android-ndk
This library seems not to be very Android friendly.
There is something weird going on with the bootstrap function (cdk/cmake/bootstrap.cmake) that seems to redefine C and CXX compilers, and I guess the OP tried to re-overwrite those overwritten values. One workaround could be using ninja as build system, in fact in the main CMakelists.txt file you can see a section:
```
# Note: Does not work well with Ninja -- not sure why

if(NOT CMAKE_GENERATOR MATCHES "Ninja")
  bootstrap()
endif()
```

Regarding the libraries, it seems that only OpenSSL needs to be downloaded and compiled, while the others are shipped with the connector-cpp code and in general, it is usually preferable to use the versions that come with the source code for compatibility reason.

The protoc-executable you pass, given it's path, seems relative to the host machine. I wonder if the library WITH_PROTOBUF needs to be compiled for the target architecture, or it is just needed the protoc executable. The error message seems to indicate to me that an executable built for android is trying to load a host system library.

Even with this command,
```
${ANDROID_CMAKE}/cmake \
    -G Ninja \
    -DCMAKE_SYSTEM_NAME=Android \
    -DCMAKE_SYSTEM_VERSION=34 \
    -DANDROID_PLATFORM=android-34 \
    -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=${ARCH} \
    -DANDROID_NDK=${ANDROID_NDK_ROOT} \
    -DANDROID_TOOLCHAIN=clang \
    -DANDROID_STL=c++_shared \
    -DCMAKE_INSTALL_PREFIX=./android_libs \
    -DWITH_SSL=${OPENSSL_DIR} \
    -DOPENSSL_INCLUDE_DIR=${OPENSSL_DIR}/include \
    -DOPENSSL_CRYPTO_LIBRARY=${OPENSSL_DIR}/lib/libcrypto.so \
    -DOPENSSL_SSL_LIBRARY=${OPENSSL_DIR}/lib/libssl.so \
    -DWITH_ZSTD=ON \
    -DWITH_LZ4=ON \
    -DWITH_ZLIB=ON \
    -DWITH_PROTOBUF=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DWITH_TESTS=OFF \
    -DWITH_JDBC=OFF \
    ../..
```
Some things still not work. On the output of the config phase done by cmake you can see CMAKE_SYSTEM_VERSION: 1. Looks like the android platform value is ignored:
-- Looking for sys/types.h
-- Looking for sys/types.h - not found
-- Looking for stdint.h
-- Looking for stdint.h - not found
-- Looking for stddef.h
-- Looking for stddef.h - not found
-- Check size of wchar_t
-- Check size of wchar_t - failed
-- Setting up Protobuf.
== configuring external build of protobuf
-- sources at: /home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/cdk/extra/protobuf
-- generator: Ninja
-- option CMAKE_BUILD_TYPE: Release
-- option CMAKE_SYSTEM_NAME: Android
-- option CMAKE_SYSTEM_VERSION: 1
-- option CMAKE_SYSTEM_PROCESSOR: aarch64
-- option CMAKE_C_COMPILER: /home/bloom/Android/Sdk/ndk/28.0.12433566/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
-- option CMAKE_CXX_COMPILER: /home/bloom/Android/Sdk/ndk/28.0.12433566/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++

When ninja builds, there are errors:
1) This looks like this struct is not defined, and I wonder if there is a standard workaround for that, given it seems related to the system header resolv.h
5/78] Building CXX object cdk/foundation/CMakeFiles/cdk_foundation.dir/socket_detail.cc.o
FAILED: cdk/foundation/CMakeFiles/cdk_foundation.dir/socket_detail.cc.o
/home/bloom/Android/Sdk/ndk/28.0.12433566/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android34 --sysroot=/home/bloom/Android/Sdk/ndk/28.0.12433566/toolchains/llvm/prebuilt/linux-x86_64/sysroot -DCONCPP_BUILD_SHARED -I/home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/cdk/include -I/home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/build/arm64-v8a/cdk/include -I/home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/cdk/extra/rapidjson/include -isystem /home/bloom/sw_develop/Android/libraries/connector-cpp/openssl/build/out/arm64-v8a/include -Wall -std=c++17 -std=c++17 -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security   -O3 -DNDEBUG  -fPIC -fPIC -fvisibility-ms-compat -Wall -DRAPIDJSON_HAS_CXX11_NOEXCEPT=1 -DRAPIDJSON_HAS_CXX11_RANGE_FOR=1 -DRAPIDJSON_HAS_CXX11_RVALUE_REFS=1 -MD -MT cdk/foundation/CMakeFiles/cdk_foundation.dir/socket_detail.cc.o -MF cdk/foundation/CMakeFiles/cdk_foundation.dir/socket_detail.cc.o.d -o cdk/foundation/CMakeFiles/cdk_foundation.dir/socket_detail.cc.o -c /home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/cdk/foundation/socket_detail.cc
/home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/cdk/foundation/socket_detail.cc:1097:22: error: variable has incomplete type 'struct __res_state'
 1097 |   struct __res_state state {};
      |                      ^
/home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/cdk/foundation/socket_detail.cc:1097:10: note: forward declaration of '__res_state'
 1097 |   struct __res_state state {};
      |          ^
1 error generated.

2) This seems just a simple linking problem and it should be easy to fix linking the Android log library, but I tried adding find_package on one of the CMakeLists with no luck:
[4/78] cd /home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-s.../Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/cdk/cmake/ext/ext-build.cmake
FAILED: cdk/protocol/mysqlx/CMakeFiles/protobuf-build /home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/build/arm64-v8a/cdk/protocol/mysqlx/CMakeFiles/protobuf-build
cd /home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/build/arm64-v8a/cdk/protocol/mysqlx/protobuf && /home/bloom/Android/Sdk/cmake/3.30.5/bin/cmake -DBIN_DIR=/home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/build/arm64-v8a/cdk/protocol/mysqlx/protobuf -DCONFIG=Release -DOPTS=--parallel 20 -P /home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/cdk/cmake/ext/ext-build.cmake
== Running extrnal build at: /home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/build/arm64-v8a/cdk/protocol/mysqlx/protobuf (Release)
[1/2] Linking CXX executable runtime_output_directory/protoc-3.19.6.0
FAILED: runtime_output_directory/protoc-3.19.6.0
: && /home/bloom/Android/Sdk/ndk/28.0.12433566/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -std=c++17  -O3 -DNDEBUG  protobuf-3.19.6/cmake/CMakeFiles/protoc.dir/__/src/google/protobuf/compiler/main.cc.o -o runtime_output_directory/protoc-3.19.6.0  protobuf-3.19.6/cmake/libprotoc.a  protobuf-3.19.6/cmake/libprotobuf.a  -llog && :
ld.lld: error: unable to find library -llog

clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
CMake Error at /home/bloom/sw_develop/Android/libraries/connector-cpp/mysql-connector-c++-9.1.0-src/cdk/cmake/ext/ext-build.cmake:55 (message):
  External build failed: 1

I have a playground at https://github.com/AndrewBloom/connector-cpp-android if anyone wants to attempt the compilation.

Andrea Fiorito

unread,
Jan 14, 2025, 9:39:47 AMJan 14
to android-ndk
I was able to compile this library for Android, and a fully automated build script can be found at https://github.com/AndrewBloom/connector-cpp-android.

Points to note:
1) The Cmake files start a bootstrap procedure that overwrites the C_COMPILER and CXX_COMPILER passed through the android toolchain file, the easiest way to bypass that is to use ninja build system (the bootstrap is skipped in that case). Moreover, the Cmake files use heavily the function execute_process, which is used to start other cmake sessions to compile the dependency library. Each spawned session does not inherit the command line parameters from the spawner, and it's needed to add this propagation on the Cmake files.
I think that using execute_process in this case is abusing the command, and it's not the right way to compile dependency libraries in cmake. Interestingly, the characteristic of not to inherit from the parent cmake session is usually leveraged to compile projects that are cross-compiled and contain tools that need to be executed on the host platform, as protobuf for example. Protobuf is comprised of a tool, the protoc compiler, that is used on the host to generate files (cpp) from .proto definitions, and a runtime library that is linked to the target platform target. 

2) The compilation process uses two tools, protoc and save_linker_opts. These two tools need to be compiled for the host environment and then used to compile for the target environment.

3) Add an Android compatible function in socket_detail.cc that is equivalent to the one using res_nsearch, but uses android NDK API. I mimicked res_nsearch behaviour with android_res_nquery, but I have no idea if that's the right way.

4) remove the resolv library from linked libraries and add the android one, and disabling BMI2 in case of x86_64 architectures for zstd library. Here I have no idea if BMI2 is not supported on Android or else, and would be interesting to know the experts' opinion on that.

The build generates the artifacts but was not tested in a real scenario, Nonetheless it should give a good indication of the steps needed to cross-compile mysql-connector-cpp. It is intended as proof-of-concept, while a cleaner solution would imply a full rewrite of all CMake files.

enh

unread,
Jan 14, 2025, 9:46:20 AMJan 14
to andro...@googlegroups.com
On Tue, Jan 14, 2025 at 9:39 AM Andrea Fiorito <ing.f...@gmail.com> wrote:
>
> I was able to compile this library for Android, and a fully automated build script can be found at https://github.com/AndrewBloom/connector-cpp-android.
>
> Points to note:
> 1) The Cmake files start a bootstrap procedure that overwrites the C_COMPILER and CXX_COMPILER passed through the android toolchain file, the easiest way to bypass that is to use ninja build system (the bootstrap is skipped in that case). Moreover, the Cmake files use heavily the function execute_process, which is used to start other cmake sessions to compile the dependency library. Each spawned session does not inherit the command line parameters from the spawner, and it's needed to add this propagation on the Cmake files.
> I think that using execute_process in this case is abusing the command, and it's not the right way to compile dependency libraries in cmake. Interestingly, the characteristic of not to inherit from the parent cmake session is usually leveraged to compile projects that are cross-compiled and contain tools that need to be executed on the host platform, as protobuf for example. Protobuf is comprised of a tool, the protoc compiler, that is used on the host to generate files (cpp) from .proto definitions, and a runtime library that is linked to the target platform target.
>
> 2) The compilation process uses two tools, protoc and save_linker_opts. These two tools need to be compiled for the host environment and then used to compile for the target environment.
>
> 3) Add an Android compatible function in socket_detail.cc that is equivalent to the one using res_nsearch, but uses android NDK API. I mimicked res_nsearch behaviour with android_res_nquery, but I have no idea if that's the right way.
>
> 4) remove the resolv library from linked libraries and add the android one, and disabling BMI2 in case of x86_64 architectures for zstd library. Here I have no idea if BMI2 is not supported on Android or else, and would be interesting to know the experts' opinion on that.

BMI2 is not included. see
https://developer.android.com/ndk/guides/abis#sa for the full ABI
details.
> To view this discussion visit https://groups.google.com/d/msgid/android-ndk/45b5cbc4-819c-4c0f-9820-9e370f7ab984n%40googlegroups.com.

skyLINE development Team

unread,
Jan 21, 2025, 8:57:27 AMJan 21
to android-ndk
will try to compile with  https://github.com/AndrewBloom/connector-cpp-android source will replay soon

вторник, 14 января 2025 г. в 16:46:20 UTC+2, enh:
Reply all
Reply to author
Forward
0 new messages