0

I am currently linking a static library to my Android Project which contains the functions I want to use, the problem that occurs is when I call a function from the static library in Android Project, and the function definition is in a .cpp file, my build fails. If that function is defined in a header through a class for example, the project compiles and runs.

The .cpp file is compiled with the definition and the project still outputs an error of no definition.

Edit: I noticed a new error message of the function outputting that it's not an ELF object.

Android Project Calls Function from Static Library:

Example.h void example(); Example.cpp void example() { } //Error 

Example.h

class example { public: void example() { }//Compiles } 

Error:

"C:\Users\User\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin/../lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin\ld" -z noexecstack --warn-shared-textrel -z now -z relro --hash-style=both --enable-new-dtags --eh-frame-hdr -m elf_i386 -shared -o "Debug\\Android-x86\\x86\\libProject.so" "C:\Users\User\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin/../sysroot/usr/lib/i686-linux-android/16\crtbegin_so.o" "-LC:\\ProjectFolder\\ProjectFolder\\Debug\\Android-x86\\x86" "-LC:\\ProjectFolder\\ProjectFolder\\StaticLibrary" "-LC:\\ProjectFolder\\ProjectFolder\\StaticLibrary\\Source" -u ANativeActivity_onCreate "-LC:\Users\User\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\lib64\clang\9.0.9\lib\linux\i386" "-LC:\Users\User\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin/../lib/gcc/i686-linux-android/4.9.x" "-LC:\Users\User\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin/../sysroot/usr/lib/i686-linux-android/16" "-LC:\Users\User\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin/../sysroot/usr/lib/i686-linux-android" "-LC:\Users\User\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin/../lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/lib" "-LC:\Users\User\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin/../sysroot/usr/lib" --exclude-libs libgcc.a --exclude-libs libatomic.a -z noexecstack -z relro --build-id=md5 --no-undefined -z now "Debug\\Android-x86\\Main.o" -lEGL -lGLESv2 -landroid -latomic -lStaticLibrary "C:\\Users\\User\\AppData\\Local\\Android\\Sdk\\ndk\\21.4.7075529\\toolchains\\llvm\\prebuilt\\windows-x86_64\\sysroot\\usr\\lib\\i686-linux-android\\libc++_shared.so" -soname=libProject.so -lc++ -lm -lgcc -ldl -lc -lgcc -ldl "C:\Users\User\AppData\Local\Android\Sdk\ndk\21.4.7075529\toolchains\llvm\prebuilt\windows-x86_64\bin/../sysroot/usr/lib/i686-linux-android/16\crtend_so.o" 1>C:\ProjectFolder\ProjectFolder\ProjectFolder/Main.cpp(10): error : undefined reference to 'FunctionName()' 1>C:\ProjectFolder\ProjectFolder\ProjectFolder/Main.cpp(10): error : clang++: error: linker command failed with exit code 1

2
  • Add compilation/linkage errors you get. Commented Jun 22, 2022 at 11:30
  • 1
    Added the Error Output Commented Jun 22, 2022 at 12:19

2 Answers 2

0

The linker clearly says undefined reference to 'FunctionName()'

In other words it can't find the source file with FunctionName() implementation. Make sure the file is compiled or implementation is placed in the same namespace with the same name (in this particular case without any namespace).

Sign up to request clarification or add additional context in comments.

5 Comments

The file with the definition is in the same directory as the header file, the problem I am finding is the file with the definition being compiled, but still the error of no definition is being outputted.
@user12826193 And I'd insist on double check. There're a few reasons for undefined reference. Try to double check it - on of the ways is to change function name.
I changed the function name, changed the file names, the directory, etc. All .cpp files that define functions from headers result in undefined references in static library.
@user12826193 are you sure you have those files in static library then? nm static_library.a and grep for you function. If not something's wrong with build instructions.
Can you explain where I input those commands, I am new to the android systems, and yes, the files are in the static library folder which contains the header and .cpp files. I assume you may be correct on the build instruction as I believe it's something to do with that, I will have to investigate further. Edit: I am using Visual Studio also
-1

I tried moving all my static library files to a new project and it seems that the previous static library Visual Studio project was corrupted, after moving over to a new clean project everything compiled.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.