工程

ncnn工程编译使用(cpu)

在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)

ncnn工程编译使用(vulkan)

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
  1. 命令行输入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)

如果随着ncnn版本更新,以上方式不可行。则CMakeLists文件改为如下内容,参考cmake_examples

# 最低cmake版本
cmake_minimum_required(VERSION 3.2)
# 工程名
project(ncnnTest)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# --- ncnn
# set函数里的路径是指包含ncnnConfig.cmake的目录
set(ncnn_DIR "{yourpath}/ncnn/build/install/lib/cmake/ncnn" CACHE PATH "folder that contains ncnnConfig.cmake")
find_package(ncnn REQUIRED)

# --- opencv
find_package(OpenCV REQUIRED)

# 生成可执行文件
add_executable(ncnnTest squeezenet.cpp)
target_link_libraries(ncnnTest ncnn ${OpenCV_LIBS})

参考

Logo

更多推荐