[深度学习] ncnn编译使用
阅读原文时间:2023年07月09日阅读:1

文章目录

工程

在linux下建立如CMakeLists文件即可编译生成ncnn工程

# 最低cmake版本
cmake_minimum_required(VERSION 3.2)
# 工程名
project(ncnnTest)
# 添加OpenCV支持
find_package(OpenCV REQUIRED)
# 添加OpenMP支持
FIND_PACKAGE(OpenMP REQUIRED)
if(OPENMP_FOUND)
    message("OPENMP FOUND")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
# 头文件目录
include_directories(/home/computerA/ncnn/build/install/include/ncnn)
# 库文件目录
link_directories(/home/computerA/ncnn/build/install/lib)
# 生成可执行文件
add_executable(ncnnTest test.cpp)
# 链接
target_link_libraries(ncnnTest ncnn ${OpenCV_LIBS} /home/computerA/ncnn/build/install/lib/libncnn.a)

vulkan版本要多一些步骤,比如添加vulkan库和glslang库。具体如下:

step1 添加vulkan环境变量

  1. 命令行输入 sudo vim ~/.bashrc

  2. 添加如下vulkan库文件路径

    export VULKAN_SDK={yourpath}/vulkansdk/x86_64 &&
    export PATH=$VULKAN_SDK/bin:$PATH &&
    export LD_LIBRARY_PATH=$VULKAN_SDK/lib:$LD_LIBRARY_PATH &&
    export LIBRARY_PATH=$VULKAN_SDK/lib:$LIBRARY_PATH &&
    export VK_LAYER_PATH=$VULKAN_SDK/etc/vulkan/explicit_layer.d

  3. 命令行输入source ~/.bashrc

step2 编写CMakeLists文件

# 最低cmake版本
cmake_minimum_required(VERSION 3.2)
# 工程名
project(ncnnTest)
# 添加OpenCV支持
find_package(OpenCV REQUIRED)
# 添加OpenMP支持
FIND_PACKAGE(OpenMP REQUIRED)
if(OPENMP_FOUND)
    message("OPENMP FOUND")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()  

# 添加Vulkan支持
FIND_PACKAGE(Vulkan REQUIRED)  

# 头文件目录
include_directories({yourpath}/ncnn/build/install/include/ncnn {yourpath}/ncnn/build/install/include/glslang {yourpath}/ncnn/build/install/include/SPIRV ${Vulkan_INCLUDE_DIRS})
# 库文件目录
link_directories({yourpath}/ncnn/build/install/lib)
# 生成可执行文件
add_executable(ncnnTest squeezenet.cpp)
# 链接
target_link_libraries(ncnnTest ncnn ${OpenCV_LIBS} Vulkan::Vulkan {yourpath}/ncnn/build/install/lib/libglslang.a {yourpath}/ncnn/build/install/lib/libOGLCompiler.a {yourpath}/ncnn/build/install/lib/libOSDependent.a {yourpath}/ncnn/build/install/lib/libSPIRV.a {yourpath}/ncnn/build/install/lib/libncnn.a)

参考

Linux(Ubuntu)上的VULKAN环境配置步骤记录
在Ubuntu上进行ncnn+vulkan的测试

在windows环境下基于Clion的Vulkan配置

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章