ld:尝试运行C++库时出现未知符号错误-CMakeLists.txt设置不正确[重复]

cld4siwp  于 2023-01-20  发布在  其他
关注(0)|答案(1)|浏览(142)
    • 此问题在此处已有答案**:

(39个答案)
21小时前关门了。
我已经下载了一个外部C++库,通过FFI在Flutter程序中使用。我已经正确设置了FFI,但是每当我从外部库使用函数时,我都会收到一个错误。函数如下:

#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;
    
}

当我编译并运行到Android设备时,我得到这个错误:

* 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

根据我的研究,我认为问题一定是来自我设置编译设置的方式,所以下面是我的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)

CMakeLists.txt有什么问题?我是否正确地包含了用于编译的文件?

mwg9r5ms

mwg9r5ms1#

最基本的问题是aoo不是内置的,你需要注意这一点。
我假设您的程序是目标“add”,上面的源代码位于cpp/add.cpp中
基本上需要正确地设置aoo库,然后告诉CMake链接你的程序。

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)

你将需要找出如何aoo需要建立虽然。
然而,如果你设置的很好,你可以#include <aoo/aoo.h>,因为CMake也会告诉编译器在哪里可以找到include。
如果aoo提供了CMake集成,也可以直接使用它。

相关问题