直接上例子:存在三个文件
example.cpp中的文件
#include<iostream>
#include"function.h"
using namespace std;
int fun(int a,int b);
int main()
{
cout<<fun(2,3)<<endl;
cout<<"hello world"<<endl;
return 0;
}
function.cpp
#include<iostream>
#include "function.h"
int fun(int a,int b)
{
return a+b;
}
function.h
#include<iostream>
int fun(int a,int b);
linux下用生成静态库的命令 ar 处理 function.o 文件生成静态库文件
有关详细知识可以参看官方手册,我们举个人脸的识别的例子,看如何独立编译的静态库(需要opencv支持)
ubuntu@ubuntu:~$git clone https://github.com/ShiqiYu/libfacedetection.git
ubuntu@ubuntu:~$ cd libfacedetection/
ubuntu@ubuntu:~/libfacedetection$
修改一下cmkelists.txt文件,打开demo的设置
ubuntu@ubuntu:~/libfacedetection$ mkdir -p build
ubuntu@ubuntu:~/libfacedetection$ cd build/
ubuntu@ubuntu:~/libfacedetection/build$ cmake ..
fatal: No names found, cannot describe anything.
BUILD_VERSION:v0.0.1
AVX512 = OFF
AVX2 = ON
NEON = OFF
OpenMP = TRUE
DEMO = ON
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/libfacedetection/build
ubuntu@ubuntu:~/libfacedetection/build$ make
[ 40%] Built target facedetection
[ 60%] Built target benchmark
[ 80%] Built target detect-image-demo
[100%] Built target detect-camera-demo
生成可执行文件,运行一下官方生成的可执行程序
如果个人想把作者的工程集成到自己的项目中的话,发现作者已经提供了静态库libfacedetection.a
ubuntu@ubuntu:~/libfacedetection/example$ g++ detect-image.cpp -I ../src/ ../build/libfacedetection.a `pkg-config --cflags --libs opencv` -fopenmp -o test
这样基本完成了调用静态库的使用,我写了个cmakelist.txt文件进行了规整,使用Clion
代码稍微修改一下
整个clion的目录如:(直接将libfacedetection对应的代码拷贝到新建立的文件夹即可)
ubuntu@ubuntu:~/CLionProjects$ tree
.
└── untitled
├── cmake-build-debug
├── CMakeLists.txt
├── detect-image.cpp
├── lib
│ └── libfacedetection.a
└── src
├── facedetectcnn.cpp
├── facedetectcnn.h
├── facedetectcnn-int8data.cpp
├── facedetectcnn-model.cpp
└── facedetection_export.h
15 directories, 52 files
cmakelist.txt文件内容为:
# cmake needs this line
cmake_minimum_required(VERSION 3.17)
project(opencvTest)
set( CMAKE_CXX_FLAGS "-fopenmp -O3" )
set( CMAKE_CXX_FLAGS "-fopenmp -O3" )
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${CMAKE_SOURCE_DIR}/src/)
add_library(libfacedetection STATIC IMPORTED)
set_target_properties(libfacedetection PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/lib/libfacedetection.a)
# Declare the executable target built from your sources
add_executable(main detect-image.cpp)
# Link your application with OpenCV libraries
target_link_libraries(main ${OpenCV_LIBS} libfacedetection)
标签:人脸识别,target,int,C++,include,ubuntu,cpp,libfacedetection From: https://blog.51cto.com/u_12504263/5719084