Search code examples
androidandroid-ndkgoogle-play-games

Undefined Function outside Header Error Android NDK


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


Solution

  • 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.