Overall, the process is relatively simple. It can be done with patience. Other systems can also refer to the official wiki to complete the compilation process.
Compilation process (RaspberryPi 4b)
- Open your project folder, and pull
ncnn
the source code, in addition you need to pullglslang
the code; cd ncnn && git clone https://github.com/KhronosGroup/glslang.git
mkdir build && cd build
- Install some required dependent packages; run
sudo apt install build-essential git cmake libprotobuf-dev protobuf-compiler libvulkan-dev vulkan-utils libopencv-dev
cmake -DCMAKE_BUILD_TYPE=Release -DNCNN_VULKAN=ON -DNCNN_BUILD_EXAMPLES=ON ..
make -j8
- After compiling, some packages and libraries will be generated; so you need to create a folder to save them; here I create
ncnn/build
a new lib folder below to store all the generated static library.a
files, as shown in the figure below, find the generated Yeslibxxx.a
,lib
just copy it below; - What you also need to copy is the and
ncnn/build/src
under the folder , find them, and copy them to the directory. When referring to the header file later, just refer to the following directly. Otherwise, it will report that the two header files cannot be found.platform.h
ncnn_export.h
ncnn/src
ncnn/src
- This way we can use it as a library.
use
During the use, I encountered many problems that the library would not be able to connect, for example
undefined reference to `glslang::InitGlobalLock()'
/usr/bin/ld: ShaderLang.cpp:(.text+0x2430): undefined reference to `glslang::GetGlobalLock()'
/usr/bin/ld: ShaderLang.cpp:(.text+0x2454): undefined reference to `glslang::ReleaseGlobalLock()'
The official issue gives a solution; g++ remembers to link the following libraries when compiling:
-lncnn -lvulkan -lglslang -lOSDependent -lSPIRV -lOGLCompiler -lGenericCodeGen -lMachineIndependent
# 文件夹层级结构
.
├── Makefile
├── src
│ └── main.cpp
└── workspace
├── 3.jpg
├── xxxx
├── xxxx.bin
├── xxxx.onnx
├── xxxx.param
└── result.jpg
Here I post the makefile for your reference only
cc := g++
name := fastdet
workdir := workspace
srcdir := src
objdir := objs
stdcpp := c++11
syslib := /usr/lib
cpp_srcs := $(shell find $(srcdir) -name "*.cpp")
cpp_objs := $(cpp_srcs:.cpp=.cpp.o)
cpp_objs := $(cpp_objs:$(srcdir)/%=$(objdir)/%)
cpp_mk := $(cpp_objs:.cpp.o=.cpp.mk)
link_opencv := opencv_core opencv_imgproc opencv_imgcodecs
link_ncnn := ncnn glslang OSDependent OGLCompiler SPIRV GenericCodeGen MachineIndependent
link_vulkan := vulkan
link_sys := stdc++ dl
link_librarys := $(link_sys) $(link_opencv) $(link_vulkan) $(link_ncnn)
include_opencv := /usr/include/opencv4
lib_opencv := /lib/aarch64-linux-gnu/lib
# ncnn config
include_ncnn := /home/pi/Documents/ncnn/src
lib_ncnn := /home/pi/Documents/ncnn/build/lib
include_paths := src \
$(include_protobuf) \
$(include_opencv) \
$(include_ncnn)
library_paths := $(lib_all) $(lib_opencv) $(lib_ncnn)
empty :=
library_path_export := $(subst $(empty) $(empty),:,$(library_paths))
run_paths := $(foreach item,$(library_paths),-Wl,-rpath=$(item))
include_paths := $(foreach item,$(include_paths),-I$(item))
library_paths := $(foreach item,$(library_paths),-L$(item))
link_librarys := $(foreach item,$(link_librarys),-l$(item))
defined := $(foreach item,$(defined),-D$(item))
cpp_compile_flags := -std=$(stdcpp) -w -g -O0 -fPIC -fopenmp -pthread $(defined)
link_flags := -pthread -fopenmp -Wl,-rpath='$$ORIGIN'
cpp_compile_flags += $(include_paths)
link_flags += $(library_paths) $(link_librarys) $(run_paths)
ifneq ($(MAKECMDGOALS), clean)
-include $(cpp_mk) $(cu_mk)
endif
$(name) : $(workdir)/$(name)
all : $(name)
run : $(name)
@cd $(workdir) && ./$(name) $(run_args)
$(workdir)/$(name) : $(cpp_objs)
@echo Link $@
@mkdir -p $(dir $@)
@$(cc) $^ -o $@ $(link_flags) -lncnn -lvulkan -lglslang -lOSDependent -lSPIRV -lOGLCompiler -lGenericCodeGen -lMachineIndependent
$(objdir)/%.cpp.o : $(srcdir)/%.cpp
@echo Compile CXX $<
@mkdir -p $(dir $@)
@$(cc) -c $< -o $@ $(cpp_compile_flags)
$(objdir)/%.cpp.mk : $(srcdir)/%.cpp
@echo Compile depends C++ $<
@mkdir -p $(dir $@)
@$(cc) -M $< -MF $@ -MT $(@:.cpp.mk=.cpp.o) $(cpp_compile_flags)
clean :
@rm -rf $(objdir) $(workdir)/$(name)
.PHONY : clean run $(name)
export LD_LIBRARY_PATH:=$(library_path_export)