Tencent / ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform
Other
20.45k stars 4.16k forks source link

编译链接ncnn出现 undefined reference to `typeinfo for ncnn::Layer' 等问题 #3141

Closed BedRockJie closed 1 year ago

BedRockJie commented 3 years ago

detail | 详细描述 | 詳細な説明

使用ncnn编译的一个小demo,在官方的sample中修改就可以编译成功,代码没有动过,将文件copy出去后,使用Cmake的方法进行编译,Cmake 脚本如下:

cmake_minimum_required(VERSION 3.2)

project(carddetect)

set(name carddetect)
set(SOURCE main.cpp)

add_executable(${name} ${SOURCE})

# 包含ncnn的头文件
include_directories(/home/gaojies/workspace/d1-tina-open/tools/ncnn/build-c906/install/include/ncnn /home/gaojies/workspace/d1-tina-open/tools/ncnn/src)

# 包含ncnn的链接文件
link_directories(/home/gaojies/workspace/d1-tina-open/tools/ncnn/build-c906/install/lib)

if(OpenCV_FOUND)
    target_include_directories(${name} PRIVATE ${OpenCV_INCLUDE_DIRS})
    target_link_libraries(${name} PRIVATE ncnn ${OpenCV_LIBS})
elseif(NCNN_SIMPLEOCV)
    target_compile_definitions(${name} PUBLIC USE_NCNN_SIMPLEOCV)
    target_link_libraries(${name} PRIVATE ncnn)
endif()

在编译的过程中出现了 比较多链接不到 函数的问题。

[ 50%] Linking CXX executable carddetect
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.L125':
main.cpp:(.text+0x940): undefined reference to `ncnn::Net::Net()'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text+0x964): undefined reference to `ncnn::Net::register_custom_layer(char const*, ncnn::Layer* (*)(void*), void (*)(ncnn::Layer*, void*), void*)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text+0x97a): undefined reference to `ncnn::Net::load_param(char const*)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.LEHB6':
main.cpp:(.text+0x990): undefined reference to `ncnn::Net::load_model(char const*)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.LEHB9':
main.cpp:(.text+0xa8c): undefined reference to `ncnn::Mat::from_pixels_resize(unsigned char const*, int, int, int, int, int, ncnn::Allocator*)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.L144':
main.cpp:(.text+0xb5c): undefined reference to `ncnn::Option::Option()'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.LEHE9':
main.cpp:(.text+0xb88): undefined reference to `ncnn::copy_make_border(ncnn::Mat const&, ncnn::Mat&, int, int, int, int, int, float, ncnn::Option const&)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text+0xbc2): undefined reference to `ncnn::Mat::substract_mean_normalize(float const*, float const*)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text+0xbd6): undefined reference to `ncnn::Net::create_extractor() const'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text+0xbf2): undefined reference to `ncnn::Extractor::input(char const*, ncnn::Mat const&)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text+0xc24): undefined reference to `ncnn::Extractor::extract(char const*, ncnn::Mat&, int)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.LEHE12':
main.cpp:(.text+0xd8e): undefined reference to `ncnn::Extractor::extract(char const*, ncnn::Mat&, int)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.LEHE14':
main.cpp:(.text+0xef8): undefined reference to `ncnn::Extractor::extract(char const*, ncnn::Mat&, int)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.L146':
main.cpp:(.text+0x137e): undefined reference to `ncnn::Extractor::~Extractor()'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text+0x13a0): undefined reference to `ncnn::Net::~Net()'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text+0x1442): undefined reference to `ncnn::Extractor::~Extractor()'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text+0x146c): undefined reference to `ncnn::Net::~Net()'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.L145':
main.cpp:(.text+0x15ca): undefined reference to `cv::rectangle(cv::Mat&, cv::Rect_<int>, cv::Scalar_<unsigned char> const&, int)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.L168':
main.cpp:(.text+0x1664): undefined reference to `cv::getTextSize(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, double, int, int*)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.LEHE21':
main.cpp:(.text+0x1764): undefined reference to `cv::rectangle(cv::Mat&, cv::Rect_<int>, cv::Scalar_<unsigned char> const&, int)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.L175':
main.cpp:(.text+0x17dc): undefined reference to `cv::putText(cv::Mat&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::Point_<int>, int, double, cv::Scalar_<unsigned char>, int)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text+0x184e): undefined reference to `cv::imwrite(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::Mat const&, std::vector<int, std::allocator<int> > const&)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.L173':
main.cpp:(.text+0x1986): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.L0 ':
main.cpp:(.text._ZN4ncnn3MatC2EimPNS_9AllocatorE[_ZN4ncnn3MatC5EimPNS_9AllocatorE]+0x7c): undefined reference to `ncnn::Mat::create(int, unsigned long, ncnn::Allocator*)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text._ZN11YoloV5FocusC2Ev[_ZN11YoloV5FocusC5Ev]+0x12): undefined reference to `ncnn::Layer::Layer()'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: main.cpp:(.text._ZNK11YoloV5Focus7forwardERKN4ncnn3MatERS1_RKNS0_6OptionE[_ZNK11YoloV5Focus7forwardERKN4ncnn3MatERS1_RKNS0_6OptionE]+0x82): undefined reference to `ncnn::Mat::create(int, int, int, unsigned long, int, ncnn::Allocator*)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o:(.rodata._ZTV11YoloV5Focus[_ZTV11YoloV5Focus]+0x20): undefined reference to `ncnn::Layer::load_param(ncnn::ParamDict const&)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o:(.rodata._ZTV11YoloV5Focus[_ZTV11YoloV5Focus]+0x28): undefined reference to `ncnn::Layer::load_model(ncnn::ModelBin const&)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o:(.rodata._ZTV11YoloV5Focus[_ZTV11YoloV5Focus]+0x30): undefined reference to `ncnn::Layer::create_pipeline(ncnn::Option const&)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o:(.rodata._ZTV11YoloV5Focus[_ZTV11YoloV5Focus]+0x38): undefined reference to `ncnn::Layer::destroy_pipeline(ncnn::Option const&)'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o:(.rodata._ZTV11YoloV5Focus[_ZTV11YoloV5Focus]+0x40): undefined reference to `ncnn::Layer::forward(std::vector<ncnn::Mat, std::allocator<ncnn::Mat> > const&, std::vector<ncnn::Mat, std::allocator<ncnn::Mat> >&, ncnn::Option const&) const'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o:(.rodata._ZTV11YoloV5Focus[_ZTV11YoloV5Focus]+0x50): undefined reference to `ncnn::Layer::forward_inplace(std::vector<ncnn::Mat, std::allocator<ncnn::Mat> >&, ncnn::Option const&) const'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o:(.rodata._ZTV11YoloV5Focus[_ZTV11YoloV5Focus]+0x58): undefined reference to `ncnn::Layer::forward_inplace(ncnn::Mat&, ncnn::Option const&) const'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o: in function `.L0 ':
main.cpp:(.text._ZN11YoloV5FocusD2Ev[_ZN11YoloV5FocusD5Ev]+0x20): undefined reference to `ncnn::Layer::~Layer()'
/home/gaojies/Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: CMakeFiles/carddetect.dir/main.cpp.o:(.rodata._ZTI11YoloV5Focus[_ZTI11YoloV5Focus]+0x10): undefined reference to `typeinfo for ncnn::Layer'
collect2: error: ld returned 1 exit status
make[2]: *** [carddetect] Error 1
make[1]: *** [CMakeFiles/carddetect.dir/all] Error 2
make: *** [all] Error 2

然后,准备使用 g++ 手动编译一下 使用如下指令: riscv64-unknown-linux-gnu-g++ -o main -I /home/gaojies/workspace/d1-tina-open/tools/ncnn/build-c906/install/include/ncnn -I /home/gaojies/workspace/d1-tina-open/tools/ncnn/src main.cpp /home/gaojies/workspace/d1-tina-open/tools/ncnn/build-c906/install/lib/libncnn.a 出现了: /Tools/riscv64-linux-x86_64-20210512/bin/../lib/gcc/riscv64-unknown-linux-gnu/10.2.0/../../../../riscv64-unknown-linux-gnu/bin/ld: /tmp/cctXu0jW.o:(.rodata._ZTI11YoloV5Focus[_ZTI11YoloV5Focus]+0x10): undefined reference to `typeinfo for ncnn::Layer' collect2: error: ld returned 1 exit status

BedRockJie commented 3 years ago

对了 cmake 的时候 用了 ncnn 本身设置的cmake 变量 cmake -DCMAKE_TOOLCHAIN_FILE=../c906.toolchain.cmake ..

nihui commented 3 years ago

https://github.com/Tencent/ncnn/wiki/use-ncnn-with-own-project#use-ncnn-with-own-project 参考这里

BedRockJie commented 3 years ago

问题解决,谢谢,但我还是有一点点问题,按照wiki设置 ncnndir 之后 也会报错 No such file or directory 31 | #include "layer/riscv/riscv_v_071_fix.h" 因为我看到了 2 个 ncnnConfig.cmake ./build-c906/install/lib/cmake/ncnn/ncnnConfig.cmake ./build-c906/src/ncnnConfig.cmake

第一个已经在 ncnn_DIR 中包括了 所以我设置了第二个 但是我没有看到 ncnn_DIR 中这些 .cmake 文件都是怎么使用的 虽然通过包含ncnn的头文件 include_directories(/home/gaojies/workspace/d1-tina-open/tools/ncnn/build-c906/install/include/ncnn /home/gaojies/workspace/d1-tina-open/tools/ncnn/src) 解决了问题,但还是比较疑惑

moonsheep99 commented 2 years ago

问题解决,谢谢,但我还是有一点点问题,按照wiki设置 ncnndir 之后 也会报错 No such file or directory 31 | #include "layer/riscv/riscv_v_071_fix.h" 因为我看到了 2 个 ncnnConfig.cmake ./build-c906/install/lib/cmake/ncnn/ncnnConfig.cmake ./build-c906/src/ncnnConfig.cmake

第一个已经在 ncnn_DIR 中包括了 所以我设置了第二个 但是我没有看到 ncnn_DIR 中这些 .cmake 文件都是怎么使用的 虽然通过包含ncnn的头文件 include_directories(/home/gaojies/workspace/d1-tina-open/tools/ncnn/build-c906/install/include/ncnn /home/gaojies/workspace/d1-tina-open/tools/ncnn/src) 解决了问题,但还是比较疑惑 您好,请问链接ncnn库的时候,编译问题显示部分ncnn函数未定义的引用,这个问题您是怎么解决的?谢谢

BedRockJie commented 2 years ago

https://github.com/Tencent/ncnn/wiki/use-ncnn-with-own-project#use-ncnn-with-own-project 参考这里

正常情况下是会通过包含ncnn的.cmake来链接到的,手动链接ncnn静态库,我之前尝试也是会出现问题,看看上面的参考是否能帮助到你

moonsheep99 commented 2 years ago

https://github.com/Tencent/ncnn/wiki/use-ncnn-with-own-project#use-ncnn-with-own-project 参考这里

正常情况下是会通过包含ncnn的.cmake来链接到的,手动链接ncnn静态库,我之前尝试也是会出现问题,看看上面的参考是否能帮助到你

非常感谢您能回复,我之前用他的动态库或静态库链接都失败了,总是会有两个函数未定义的引用,之后找到了这个问题,也尝试了您说的链接上的解决办法,但是可能是我使用的不对或者路径不对,至少在编译过程中是提示我 它找不到ncnnConfig.cmake,是这样的 CMake Error at CMakeLists.txt:45 (find_package): By not providing "Findncnn.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "ncnn", but CMake did not find one.

Could not find a package configuration file provided by "ncnn" with any of the following names:

ncnnConfig.cmake
ncnn-config.cmake

但我已经多次确认,我在cmakelists中写的路径应该是没问题的 我在cmake中写的是这样的: set(ncnn_DIR "home/gjx/ORB_SLAM2/ORB_SLAM2/Thirdparty/ncnn/build/install/lib/cmake/ncnn" CACHE PATH "") find_package(ncnn REQUIRED) 是CACHE PATH 后面的引号中要写什么吗 不知能不能加您个联系方式,花费您一点时间帮忙看一下具体问题,真是麻烦了!

BedRockJie commented 2 years ago

https://github.com/BedRockJie/Collector/blob/main/ncnn_card_detect/CMakeLists.txt 参考我曾经编译的cmake看看是否对你有帮助

moonsheep99 commented 2 years ago

https://github.com/BedRockJie/Collector/blob/main/ncnn_card_detect/CMakeLists.txt 参考我曾经编译的cmake看看是否对你有帮助

非常感谢您的回答和及时的回复,这个问题已经困扰我好几天了,根据您提供的您的CMakeLists,我仿照着完成了我的,目前已经完美解决了,不会出现那种问题了,真的非常感谢!

gmx1992 commented 2 years ago

https://github.com/BedRockJie/Collector/blob/main/ncnn_card_detect/CMakeLists.txt 参考我曾经编译的cmake看看是否对你有帮助

https://github.com/Tencent/ncnn/wiki/use-ncnn-with-own-project#use-ncnn-with-own-project 参考这里

感谢大佬们的回答,在此基础上修改之后make时仍然报相同的错误,提示 undefined reference to xxxxxx;这里也参考了nihui大神提供的链接,手动添加了vulkan依赖还是报错。在线求教,谢谢。

maziyi234 commented 1 year ago

https://github.com/BedRockJie/Collector/blob/main/ncnn_card_detect/CMakeLists.txt 参考我曾经编译的cmake看看是否对你有帮助

非常感谢您的回答和及时的回复,这个问题已经困扰我好几天了,根据您提供的您的CMakeLists,我仿照着完成了我的,目前已经完美解决了,不会出现那种问题了,真的非常感谢!

兄弟 你是怎么解决的 我也遇到了相同的问题

Xiaowei-coder commented 1 year ago

https://github.com/BedRockJie/Collector/blob/main/ncnn_card_detect/CMakeLists.txt 参考我曾经编译的cmake看看是否对你有帮助

大佬,请问你遇到过这个问题吗? D:\Download\ncnn-android-squeezenet-master\app\src\main\jni\CMakeLists.txt : C/C++ debug|arm64-v8a : CMake Error at C:/Users/xwf20/AppData/Local/Android/Sdk/cmake/3.10.2.4988404/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:137 (message): Could NOT find Threads (missing: Threads_FOUND) Call Stack (most recent call first): C:/Users/xwf20/AppData/Local/Android/Sdk/cmake/3.10.2.4988404/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE) C:/Users/xwf20/AppData/Local/Android/Sdk/cmake/3.10.2.4988404/share/cmake-3.10/Modules/FindThreads.cmake:205 (FIND_PACKAGE_HANDLE_STANDARD_ARGS) ncnn-20230223-android-vulkan/arm64-v8a/lib/cmake/ncnn/ncnnConfig.cmake:14 (find_package) CMakeLists.txt:6 (find_package)

BedRockJie commented 1 year ago

看报错是因为没有找到你编译链的线程库 Android环境工具链使用NDK试试