How to create a shared library for Android from Windows QtCreator

  android, c++, cmake, qml, qt

What I have


Project on C++ – MyLibC build system CMake 3.14

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)

project(MyLibC LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(MyLibC SHARED
  MyLibC.cpp
  MyLibC.h
)

target_compile_definitions(MyLibC PRIVATE QRCD_LIBRARY)

.cpp and .h files have empty class.

Project on QtQuick – MyProjQt build system qmake

myprojqt.pro

QT += quick

CONFIG += c++14

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += 
        main.cpp 
        testclass.cpp

RESOURCES += qml.qrc

LIBS += -L"$$_PRO_FILE_PWD_/lib" -libmylibc

unix {
    ANDROID_EXTRA_LIBS=$$_PRO_FILE_PWD_/lib/libmylibc.so
}

win32 {
    INCLUDEPATH += "$$_PRO_FILE_PWD_/include"
    LIBS += "$$_PRO_FILE_PWD_/lib/libmylibc.dll"
}

TRANSLATIONS += 
    qrgenerator_en_US.ts

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += 
    testclass.h

What I alredy did

  1. Added toolchain.cmake file in lib project toolchain.cmake

toolchain.cmake

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_COMPILER g++)
  1. Added flag to start CMake parametrs -DCMAKE_TOOLCHAIN_FILE=./toolchain.cmake ..
  2. Builded lib and recived libmylibc.so
  3. Added .so and .h to MyProjQt
  4. Builded MyProjQt and have a problem.

Errors

:-1: ошибка: error: /lib/libmylibc.so:1:3: invalid character
:-1: ошибка: error: /lib/libmylibc.so:1:3: syntax error, unexpected $end
:-1: ошибка: error: /lib/libmylibc.so: not an object or archive

What I’m doing wrong?

Qt 5.15.2
CMake 3.14.2
Android Clang (C++, arm, NDK 21.3.6528147)

p.s.
I already did it once, but I forgot how

Source: Windows Questions C++

LEAVE A COMMENT