java - Undefined reference to function with Cmake in Android studio -
i try add c functions java android app.
managed run basic function hello world in c.
in file hello.c call c function contained in file (astart.c) calls function c in file (util.c) calls others functions in others files.
no problem that, not it?
i add libraries cmakelists.txt :
# sets minimum version of cmake required build native # library. should either keep default value or pass # value of 3.4.0 or lower. cmake_minimum_required(version 3.4.1) # creates , names library, sets either static # or shared, , provides relative paths source code. # can define multiple libraries, , cmake builds you. # gradle automatically packages shared libraries apk. add_library( # sets name of library. native-lib # sets library shared library. shared # provides relative path source file(s). # associated headers in same location source # file automatically included. src/main/cpp/native-lib.cpp ) add_library( hello # sets library shared library. shared src/main/mycpp/hello.c ) add_library( astart # sets library shared library. shared src/main/mycpp/astart.c ) add_library( ai # sets library shared library. shared src/main/mycpp/lib/ai.c ) add_library( sha # sets library shared library. shared src/main/mycpp/lib/sha.c ) add_library( util # sets library shared library. shared src/main/mycpp/lib/util.c ) add_library( allnet_log # sets library shared library. shared src/main/mycpp/lib/allnet_log.c ) #add_library( packet # sets library shared library. # shared # src/main/mycpp/lib/packet.c ) add_library( configfiles # sets library shared library. shared src/main/mycpp/lib/configfiles.c ) add_library( allnet_queue # sets library shared library. shared src/main/mycpp/lib/allnet_queue.c ) # add directories cpp header files let cmake find them during compile time include_directories(src/main/mycpp/) include_directories(src/main/mycpp/lib/) # searches specified prebuilt library , stores path # variable. because system libraries included in search path # default, need specify name of public ndk library # want add. cmake verifies library exists before # completing build. find_library( # sets name of path variable. log-lib # specifies name of ndk library # want cmake locate. log ) # specifies libraries cmake should link target library. # can link multiple libraries, such libraries define in # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # specifies target library. native-lib # links target library log library # included in ndk. ${log-lib} )
my gradle file :
apply plugin: 'com.android.application' android { compilesdkversion 25 buildtoolsversion "25.0.0" defaultconfig { applicationid "com.example.hippolytelacassagne.allnet" minsdkversion 19 targetsdkversion 25 versioncode 1 versionname "1.0" testinstrumentationrunner "android.support.test.runner.androidjunitrunner" externalnativebuild { cmake { cppflags "" cflags "-fexceptions" } } } buildtypes { release { minifyenabled false proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro' } } externalnativebuild { cmake { path "cmakelists.txt" } } // if want gradle package prebuilt native libraries // apk, modify default source set configuration // include directory of prebuilt .so files follows. sourcesets { main { jni.srcdirs = [] jnilibs.srcdirs 'imported-lib/src/', 'more-imported- libs/src/' } } } dependencies { compile filetree(include: ['*.jar'], dir: 'libs') androidtestcompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' testcompile 'junit:junit:4.12' }
my hello.c called in home.java :
#include <jni.h> #include "lib/app_util.h" #include "lib/packet.h" #include "astart.c" jniexport jstring jnicall java_com_example_hippolytelacassagne_allnet_home_hello( jnienv *env, jobject callingobject) { char * args [] = { "allnet", "-v", "def", null }; astart_main(3, args); return (*env)->newstringutf(env, "hello world"); }
the includes if astart.c called in hello.c :
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <signal.h> #include <pwd.h> #include <ifaddrs.h> #include <dirent.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/wait.h> #include <sys/socket.h> #include <netinet/in.h> #include <net/if.h> /* iff_loopback, etc */ #include <arpa/inet.h> /* inet_addr */ #include "lib/util.h" #include "lib/allnet_log.h" #include "lib/packet.h" #include "lib/configfiles.h" #include "lib/allnet_queue.h"
and includes of util.c called in astart.c :
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <time.h> #include <assert.h> #include <errno.h> #include <netdb.h> /* h_errno */ #include <dirent.h> /* h_errno */ #include <sys/time.h> #include <sys/stat.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/un.h> #include <netinet/in.h> #include <arpa/inet.h> #include "packet.h" #include "mgmt.h" #include "allnet_log.h" #include "util.h" #include "ai.h" #include "sha.h"
and here error message of gradle console :
failure: build failed exception. * went wrong: execution failed task ':app:externalnativebuilddebug'. > build command failed. error while executing '/users/hippolytelacassagne/library/android/sdk/cmake/3.6.4111459/bin/cmake' arguments {--build /users/hippolytelacassagne/androidstudioprojects/allnet/app/.externalnativebuild/cmake/debug/mips64 --target util} [1/2] building c object cmakefiles/util.dir/src/main/mycpp/lib/util.c.o [2/2] linking c shared library ../../../../build/intermediates/cmake/debug/obj/mips64/libutil.so failed: : && /users/hippolytelacassagne/library/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang --target=mips64el-none-linux-android --gcc-toolchain=/users/hippolytelacassagne/library/android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/users/hippolytelacassagne/library/android/sdk/ndk-bundle/sysroot -fpic -isystem /users/hippolytelacassagne/library/android/sdk/ndk-bundle/sysroot/usr/include/mips64el-linux-android -d__android_api__=21 -g -dandroid -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fintegrated-as -wa,--noexecstack -wformat -werror=format-security -fexceptions -o0 -fno-limit-debug-info -wl,--exclude-libs,libgcc.a --sysroot /users/hippolytelacassagne/library/android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -wl,--build-id -wl,--warn-shared-textrel -wl,--fatal-warnings -wl,--no-undefined -wl,-z,noexecstack -qunused-arguments -wl,-z,relro -wl,-z,now -shared -wl,-soname,libutil.so -o ../../../../build/intermediates/cmake/debug/obj/mips64/libutil.so cmakefiles/util.dir/src/main/mycpp/lib/util.c.o -lm && : cmakefiles/util.dir/src/main/mycpp/lib/util.c.o: in function `mgmt_to_string': /users/hippolytelacassagne/androidstudioprojects/allnet/app/src/main/mycpp/lib/util.c:245: undefined reference `ia_to_string' /users/hippolytelacassagne/androidstudioprojects/allnet/app/src/main/mycpp/lib/util.c:274: undefined reference `ia_to_string' cmakefiles/util.dir/src/main/mycpp/lib/util.c.o: in function `init_packet': /users/hippolytelacassagne/androidstudioprojects/allnet/app/src/main/mycpp/lib/util.c:532: undefined reference `sha512_bytes' cmakefiles/util.dir/src/main/mycpp/lib/util.c.o: in function `print_gethostbyname_error': /users/hippolytelacassagne/androidstudioprojects/allnet/app/src/main/mycpp/lib/util.c:1554: undefined reference `log_print' clang: error: linker command failed exit code 1 (use -v see invocation) ninja: build stopped: subcommand failed. * try: run --stacktrace option stack trace. run --info or --debug option more log output. build failed
it appear errors come functions of util.c.
don't understand because functions supposedly not defined defined in included files.
does have idea? thank you.
i found problem.
android studio needs specify dependencies between files in cmakelists :
target_link_libraries( # specifies target library. util sha ai allnet_log # links target library log library # included in ndk. ${log-lib} )
the #includes in file not enough.
Comments
Post a Comment