0

I have a simple CMake project: a source library (which I compile into static lib) and NDK-wrapper (which I compile into shared lib to use from Android/Kotlin). The project is well-compiled from Linux/GCC command line.

But, when I compile it from Android Studio (clang++ compiler), there are error "undefined reference to" method from static library.

library source (h):

void method();

library source (cpp):

void method() {
}

wrapper source (cpp):

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_streelib_StreeLib_hash(JNIEnv *env, jobject thiz, jstring str) {
    method();
}

library CMakeList.txt:

cmake_minimum_required(VERSION 3.10)
project("streelib")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
add_compile_options("-v")
add_library(streelib SHARED streelib.cpp)
add_subdirectory(streebog ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
target_link_libraries(streelib PRIVATE streebog)

wrapper CMakeList.txt:

cmake_minimum_required(VERSION 3.10)
project (streebog VERSION 0.13 DESCRIPTION "GOST STREEBOG HASH")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
add_compile_options("-v")
add_library(${PROJECT_NAME} STATIC gost3411-2012-core.c)
#add_library(${PROJECT_NAME} SHARED gost3411-2012-core.c)
configure_file(config.h.in config.h)

gradle build script:

plugins {
    id 'com.android.library'
    id 'kotlin-android'
}

android {
    compileSdk 28

    defaultConfig {
        minSdk 22
        targetSdk 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
//            ndkBuild {
//                cFlags "-DDEBUG_MODE=1"
//            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
//        ndkBuild {
//            path "src/main/cpp/Android.mk"
//            version "3.10.2"
//        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Maybe there are any flags for clang++ compiler? I do not understand why I catch the error "undefined reference to" method.

Android.mk does not work too (the same error):

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := streebog
LOCAL_CFLAGS := "-v -undefined dynamic_lookup"
LOCAL_C_INCLUDES := $(LOCAL_PATH)/streebog
LOCAL_SRC_FILES := $(LOCAL_PATH)/streebog/gost3411-2012-core.c
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := streelib
LOCAL_CFLAGS := "-v -undefined dynamic_lookup"
LOCAL_SRC_FILES := streelib.cpp
LOCAL_STATIC_LIBRARIES := streebog
include $(BUILD_SHARED_LIBRARY)

Linker output (under Gradle):

Caused by: org.gradle.internal.UncheckedException: Build command failed.
Error while executing process /home/alex/Android/Sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /home/alex/StudioProjects/GostLib/StreeLib/.cxx/Debug/17395b63/armeabi-v7a streelib}
ninja: Entering directory `/home/alex/StudioProjects/GostLib/StreeLib/.cxx/Debug/17395b63/armeabi-v7a'
[1/4] Building C object /home/alex/StudioProjects/GostLib/StreeLib/build/intermediates/cxx/Debug/17395b63/obj/armeabi-v7a/CMakeFiles/streebog.dir/gost3411-2012-core.c.o
Android (7019983 based on r365631c3) clang version 9.0.9 (https://android.googlesource.com/toolchain/llvm-project a2a1e703c0edb03ba29944e529ccbf457742737b) (based on LLVM 9.0.9svn)
Target: armv7-none-linux-android22
Thread model: posix
InstalledDir: /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/bin
Found candidate GCC installation: /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
Selected GCC installation: /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
Candidate multilib: thumb;@mthumb
Candidate multilib: armv7-a;@march=armv7-a
Candidate multilib: armv7-a/thumb;@march=armv7-a@mthumb
Candidate multilib: .;
Selected multilib: armv7-a/thumb;@march=armv7-a@mthumb
 "/home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" -cc1 -triple thumbv7-none-linux-android22 -emit-obj -mrelax-all -mnoexecstack -disable-free -disable-llvm-verifier -discard-value-names -main-file-name gost3411-2012-core.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu generic -target-feature +soft-float-abi -target-feature +fpregs -target-feature +vfp2 -target-feature +vfp2d16 -target-feature +vfp2d16sp -target-feature +vfp2sp -target-feature +vfp3 -target-feature +vfp3d16 -target-feature +vfp3d16sp -target-feature +vfp3sp -target-feature -fp16 -target-feature -vfp4 -target-feature -vfp4d16 -target-feature -vfp4d16sp -target-feature -vfp4sp -target-feature -fp-armv8 -target-feature -fp-armv8d16 -target-feature -fp-armv8d16sp -target-feature -fp-armv8sp -target-feature -fullfp16 -target-feature +fp64 -target-feature +d32 -target-feature +neon -target-feature -crypto -target-feature -fp16fml -target-abi aapcs-linux -mfloat-abi soft -fallow-half-arguments-and-returns -dwarf-column-info -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=gdb -v -ffunction-sections -fdata-sections -coverage-notes-file /home/alex/StudioProjects/GostLib/StreeLib/build/intermediates/cxx/Debug/17395b63/obj/armeabi-v7a/CMakeFiles/streebog.dir/gost3411-2012-core.c.gcno -resource-dir /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.9 -dependency-file /home/alex/StudioProjects/GostLib/StreeLib/build/intermediates/cxx/Debug/17395b63/obj/armeabi-v7a/CMakeFiles/streebog.dir/gost3411-2012-core.c.o.d -sys-header-deps -MT /home/alex/StudioProjects/GostLib/StreeLib/build/intermediates/cxx/Debug/17395b63/obj/armeabi-v7a/CMakeFiles/streebog.dir/gost3411-2012-core.c.o -D ANDROID -D _FORTIFY_SOURCE=2 -isysroot /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot -internal-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/include -internal-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.9/include -internal-externc-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arm-linux-androideabi -internal-externc-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/include -internal-externc-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include -O0 -Wformat -Werror=format-security -fdebug-compilation-dir /home/alex/StudioProjects/GostLib/StreeLib/.cxx/Debug/17395b63/armeabi-v7a -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fno-signed-char -fobjc-runtime=gcc -fdiagnostics-show-option -o /home/alex/StudioProjects/GostLib/StreeLib/build/intermediates/cxx/Debug/17395b63/obj/armeabi-v7a/CMakeFiles/streebog.dir/gost3411-2012-core.c.o -x c /home/alex/StudioProjects/GostLib/StreeLib/src/main/cpp/streebog/gost3411-2012-core.c
clang -cc1 version 9.0.9 based upon LLVM 9.0.9svn default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/include
 /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.9/include
 /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arm-linux-androideabi
 /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
End of search list.
[2/4] Linking C static library /home/alex/StudioProjects/GostLib/StreeLib/build/intermediates/cxx/Debug/17395b63/obj/armeabi-v7a/libstreebog.a
[3/4] Building CXX object CMakeFiles/streelib.dir/streelib.cpp.o
Android (7019983 based on r365631c3) clang version 9.0.9 (https://android.googlesource.com/toolchain/llvm-project a2a1e703c0edb03ba29944e529ccbf457742737b) (based on LLVM 9.0.9svn)
Target: armv7-none-linux-android22
Thread model: posix
InstalledDir: /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/bin
Found candidate GCC installation: /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
Selected GCC installation: /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
Candidate multilib: thumb;@mthumb
Candidate multilib: armv7-a;@march=armv7-a
Candidate multilib: armv7-a/thumb;@march=armv7-a@mthumb
Candidate multilib: .;
Selected multilib: armv7-a/thumb;@march=armv7-a@mthumb
 "/home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++" -cc1 -triple thumbv7-none-linux-android22 -emit-obj -mrelax-all -mnoexecstack -disable-free -disable-llvm-verifier -discard-value-names -main-file-name streelib.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu generic -target-feature +soft-float-abi -target-feature +fpregs -target-feature +vfp2 -target-feature +vfp2d16 -target-feature +vfp2d16sp -target-feature +vfp2sp -target-feature +vfp3 -target-feature +vfp3d16 -target-feature +vfp3d16sp -target-feature +vfp3sp -target-feature -fp16 -target-feature -vfp4 -target-feature -vfp4d16 -target-feature -vfp4d16sp -target-feature -vfp4sp -target-feature -fp-armv8 -target-feature -fp-armv8d16 -target-feature -fp-armv8d16sp -target-feature -fp-armv8sp -target-feature -fullfp16 -target-feature +fp64 -target-feature +d32 -target-feature +neon -target-feature -crypto -target-feature -fp16fml -target-abi aapcs-linux -mfloat-abi soft -fallow-half-arguments-and-returns -dwarf-column-info -debug-info-kind=standalone -dwarf-version=4 -debugger-tuning=gdb -v -ffunction-sections -fdata-sections -coverage-notes-file /home/alex/StudioProjects/GostLib/StreeLib/.cxx/Debug/17395b63/armeabi-v7a/CMakeFiles/streelib.dir/streelib.cpp.gcno -resource-dir /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.9 -dependency-file CMakeFiles/streelib.dir/streelib.cpp.o.d -sys-header-deps -MT CMakeFiles/streelib.dir/streelib.cpp.o -D streelib_EXPORTS -D ANDROID -D _FORTIFY_SOURCE=2 -isysroot /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot -internal-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1 -internal-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/include -internal-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.9/include -internal-externc-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arm-linux-androideabi -internal-externc-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/include -internal-externc-isystem /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include -O0 -Wformat -Werror=format-security -fdeprecated-macro -fdebug-compilation-dir /home/alex/StudioProjects/GostLib/StreeLib/.cxx/Debug/17395b63/armeabi-v7a -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fno-signed-char -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -o CMakeFiles/streelib.dir/streelib.cpp.o -x c++ /home/alex/StudioProjects/GostLib/StreeLib/src/main/cpp/streelib.cpp
clang -cc1 version 9.0.9 based upon LLVM 9.0.9svn default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1
 /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/local/include
 /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/9.0.9/include
 /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/arm-linux-androideabi
 /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include
End of search list.
[4/4] Linking CXX shared library /home/alex/StudioProjects/GostLib/StreeLib/build/intermediates/cxx/Debug/17395b63/obj/armeabi-v7a/libstreelib.so
FAILED: /home/alex/StudioProjects/GostLib/StreeLib/build/intermediates/cxx/Debug/17395b63/obj/armeabi-v7a/libstreelib.so 
: && /home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi22 --gcc-toolchain=/home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/alex/Android/Sdk/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-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   -O0 -fno-limit-debug-info  -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,libstreelib.so -o /home/alex/StudioProjects/GostLib/StreeLib/build/intermediates/cxx/Debug/17395b63/obj/armeabi-v7a/libstreelib.so CMakeFiles/streelib.dir/streelib.cpp.o  /home/alex/StudioProjects/GostLib/StreeLib/build/intermediates/cxx/Debug/17395b63/obj/armeabi-v7a/libstreebog.a -latomic -lm && :
/home/alex/StudioProjects/GostLib/StreeLib/src/main/cpp/streelib.cpp:16: error: undefined reference to 'GOST34112012Init(GOST34112012Context*, unsigned int)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
AlexDem
  • 11
  • 3
  • What's the full compiler command and output? – Alan Birtles Apr 24 '23 at 06:38
  • Hi. Attached. If I use static variables, even references to functions, linker does recognize it (i.e. static library is built and found), but direct function calls -- does not. – AlexDem Apr 24 '23 at 07:41
  • Please also show the source code that matches this error – Alan Birtles Apr 24 '23 at 07:49
  • Where is this `GOST34112012Init` function _defined_? – Botje Apr 24 '23 at 08:12
  • Here are library sources: https://github.com/adegtyarev/streebog (but there are config-created code, therefore we could try any example instead of it) – AlexDem Apr 24 '23 at 08:15
  • Hmm... If I just replace the library with a pure example (method...) -- it compiles well. So, the problem is in the library itself - maybe there are any redefinitions... – AlexDem Apr 24 '23 at 08:22
  • the key detail missing in your question is that the library is written in c, not c++ therefore you need an `extern "C"` wrapper round any inclusion of its headers in c++ code: https://stackoverflow.com/a/12574420/5494370 – Alan Birtles Apr 24 '23 at 12:57

0 Answers0