undefined references building C++-Code with Android Studio for Flutter plug-in

  android-studio, c++, clang, cmake, linker-errors

I am trying to use multiple C++-libraries in a Flutter plug-in project (using Android Studio 4.2.2). Finally, I managed to get a structured setup in which I can compile multiple libraries using cmake.
Now, I ran into linker problems with undefined references. This happened albeit the library containing the supposed to be missing symbols is included in the linker command line. Also, the .so being linked exists and contains the required symbol (using a text search in the file I can find the function name).

For testing purposes, I created a minimal setup with two libraries base_dummy and dummy. dummy uses a function from base_dummy.

The error output looks like this:

[ ] [6/14] Linking CXX shared library <…>/libdummy.so

[ ] FAILED: <…>/intermediates/cmake/release/obj/armeabi-v7a/libdummy.so

[ ] cmd.exe /C "cd . &&
D:sdkAndroidSdkndk.1.6352462toolchainsllvmprebuiltwindows-x86_64binclang++.exe
–target=armv7-none-linux-androideabi16
–gcc-toolchain=D:/sdk/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64
–sysroot=D:/sdk/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64/sysroot
-fPIC -g -DANDROID -fdata-sections
-ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security -std=c++17 -Oz -DNDEBUG -Wl,–exclude-libs,libgcc.a
-Wl,–exclude-libs,libgcc_real.a -Wl,–exclude-libs,libatomic.a -static-libstdc++ -Wl,–build-id -Wl,–fatal-warnings -Wl,–exclude-libs,libunwind.a -Wl,–no-undefined -Qunused-arguments -shared -Wl,-soname,libdummy.so -o <…>intermediatescmakereleaseobjarmeabi-v7alibdummy.so
build/dummy/CMakeFiles/dummy.dir/src/dummy.cpp.o
<…>/intermediates/cmake/release/obj/armeabi-v7a/libbase_dummy.so
-latomic -lm && cd ."

[ ] <…>/dummy/src/dummy.cpp:14: error: undefined reference to ‘base_dummy::base_dummy()’

My main CMakeLists.txt looks this:

cmake_minimum_required(VERSION 3.2.0)

add_subdirectory("../../../cpp_libs/base_dummy" "build/base_dummy")
add_subdirectory("../../../cpp_libs/dummy" "build/dummy")

base_dummy

CMakeLists.txt

cmake_minimum_required(VERSION 3.2.0)

project(base_dummy)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

add_library(
    base_dummy
    SHARED
    "./src/base_dummy.cpp")
    
target_include_directories(
    base_dummy
    PUBLIC
    "./inc_api")
    
target_include_directories(
    base_dummy
    PRIVATE
    "./inc")

base_dummy.hpp

#ifndef BASE_DUMMY_BASE_DUMMY_HPP_
#define BASE_DUMMY_BASE_DUMMY_HPP_

namespace base_dummy {

void base_dummy();

}

#endif

base_dummy.cpp

#include "base_dummy/base_dummy.hpp"

#include <iostream>


namespace base_dummy {

void base_dummy() {
    std::cout << "base_dummy";
}

}

dummy

CMakeLists.txt

cmake_minimum_required(VERSION 3.2.0)

project(dummy)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

add_library(
    dummy
    SHARED
    "./src/dummy.cpp")
    
target_include_directories(
    dummy
    PUBLIC
    "./inc_api")
    
target_include_directories(
    dummy
    PRIVATE
    "./inc"
    "../base_dummy/inc_api")

target_link_libraries(
    dummy
    base_dummy)

dummy.hpp

#ifndef DUMMY_DUMMY_HPP_
#define DUMMY_DUMMY_HPP_

namespace dummy {

void dummy();

}

#endif

dummy.cpp

#include "dummy/dummy.hpp"

#include <iostream>

#include "base_dummy/base_dummy.hpp"


namespace dummy {

void dummy() {
    std::cout << "dummy";
    base_dummy::base_dummy();
}

}

What is going wrong here? What do I need to do differently?

Source: Windows Questions C++

LEAVE A COMMENT