0

I've downloaded an external c++ library for use inside a Flutter program via FFI. I've set up the FFI correctly, but I'm getting an error whenever I have the use the function from the external library. Here's the function:

#include <iostream>
#include "aoo/include/aoo/aoo.h"
#include "aoo/include/aoo/aoo_server.h"
#include "aoo/include/aoo/aoo_net.h"


extern "C" void initAoo(){
    aoo_initialize(0);
    std::cout << "Called from init AOO!" << std::endl;
    
}

When I compile and run to an android device, I get this error:


* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
  Error while executing process /Users/zacharyhaslam/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /Users/zacharyhaslam/FlutterApplications/state_management_example/android/app/.cxx/cmake/debug/armeabi-v7a add}
  ninja: Entering directory `/Users/zacharyhaslam/FlutterApplications/state_management_example/android/app/.cxx/cmake/debug/armeabi-v7a'
  [1/2] Building CXX object CMakeFiles/add.dir/cpp/add.cpp.o
  [2/2] Linking CXX shared library /Users/zacharyhaslam/FlutterApplications/state_management_example/build/app/intermediates/cmake/debug/obj/armeabi-v7a/libadd.so
  FAILED: /Users/zacharyhaslam/FlutterApplications/state_management_example/build/app/intermediates/cmake/debug/obj/armeabi-v7a/libadd.so 
  : && /Users/zacharyhaslam/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=armv7-none-linux-androideabi19 --sysroot=/Users/zacharyhaslam/Library/Android/sdk/ndk/25.1.8937393/toolchains/llvm/prebuilt/darwin-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   -fno-limit-debug-info  -static-libstdc++ -Wl,--build-id=sha1 -Wl,--no-rosegment -Wl,--fatal-warnings -Wl,--gc-sections -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libadd.so -o /Users/zacharyhaslam/FlutterApplications/state_management_example/build/app/intermediates/cmake/debug/obj/armeabi-v7a/libadd.so CMakeFiles/add.dir/cpp/add.cpp.o  -latomic -lm && :
  ld: error: undefined symbol: aoo_initialize
  >>> referenced by add.cpp:11 (../../../../../cpp/add.cpp:11)
  >>>               CMakeFiles/add.dir/cpp/add.cpp.o:(initAoo)
  clang++: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed

It says ld: error: undefined symbol: aoo_initialize. From my research, I think the problem must be from the way that I set up the compilation settings, so here's my CMakeLists.txt:

cmake_minimum_required(VERSION 3.10.2)

add_library( add  

            SHARED

            cpp/add.cpp

)
include_directories(cpp/aoo/include/aoo/aoo.h)
add_library(aoo cpp/aoo/include/aoo/aoo.h)
set_target_properties(aoo PROPERTIES LINKER_LANGUAGE CXX)

What is wrong with CMakeLists.txt? Am I properly including the files for compilation?

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Zachary Haslam
  • 103
  • 1
  • 12

1 Answers1

1

The basic problem is that aoo is not built. You'll need to take care of that.

I assume that your program is the target "add" and the source above is in cpp/add.cpp

Basically need to setup the aoo library correctly and then tell CMake to link your program against it.

add_library(aoo)
target_include_directories(aoo PUBLIC aoo/include)
target_sources(aoo PRIVATE aoo/src/library-source1.cc
                           aoo/src/library-source2.cc
                           ...)

add_library(add SHARED)
target_sources(add PRIVATE cpp/add.cpp)
target_link_libraries(add PRIVATE aoo)

You will need to find out how aoo needs to be built though.

However, if you setup it well, you can #include <aoo/aoo.h> as CMake will also tell the compiler where to find the includes.

If aoo provides CMake integration, it may also be possible to just use that.