这里拿一段代码来作为例子:
cmake_minimum_required(VERSION 3.5)
project(ji)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_INSTALL_PREFIX "/usr/local/ev_sdk")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
SET(CMAKE_BUILD_TYPE "Release")
#先编译三方库中的源码,并包含头文件路径
#jsoncpp相关头文件路径和源文件
include_directories(${CMAKE_SOURCE_DIR}/3rd/jsoncpp_simple/include)
add_subdirectory(${CMAKE_SOURCE_DIR}/3rd/jsoncpp_simple)
#配置点线框解析相关头文件路径和源文件
include_directories(${CMAKE_SOURCE_DIR}/3rd/wkt_parser/include)
include_directories(${CMAKE_SOURCE_DIR}/3rd/licenseAuth/include)
add_subdirectory(${CMAKE_SOURCE_DIR}/3rd/wkt_parser)
# 使用pkg-config查找必要的依赖包
find_package(OpenCV REQUIRED)
#find_library(glog REQUIRED)
find_library(LICENSE_LIB licenseAuth ${CMAKE_SOURCE_DIR}/3rd/licenseAuth/lib)
set(ENV{PKG_CONFIG_PATH} "ENV{PKG_CONFIG_PATH}")
find_package(PkgConfig REQUIRED)
include_directories(/usr/local/cuda-11.1/targets/x86_64-linux/include/)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}:/usr/local/ev_sdk/lib")
# 编译libji.so
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRCS)
list(APPEND libji_SOURCES ${SRCS})
add_library (${PROJECT_NAME} SHARED ${libji_SOURCES})
target_include_directories(${PROJECT_NAME}
PUBLIC ${OpenCV_INCLUDE_DIR}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_link_libraries(${PROJECT_NAME}
${OpenCV_LIBS}
${LICENSE_LIB}
glog
WKTParser
jsoncpp
nvinfer
nvonnxparser
-L/usr/local/cuda/lib64/ -lcudart
)
# Install libji.so
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
install(FILES ${CMAKE_SOURCE_DIR}/3rd/licenseAuth/lib/liblicenseAuth.so DESTINATION lib)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/3rd/fonts DESTINATION lib)
# --- END EV_SDK DEPENDENCIES ---
cmake_minimum_required(VERSION 3.5)
:这行代码指定了CMake的最低版本要求为3.5。project(ji)
:定义了一个名为"ji"的项目。set(CMAKE_CXX_STANDARD 11)
:设置C++标准为11。set(CMAKE_INSTALL_PREFIX "/usr/local/ev_sdk")
:设置安装路径的前缀为"/usr/local/ev_sdk"。set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
:设置运行时库搜索路径为安装路径的lib目录。SET(CMAKE_BUILD_TYPE "Release")
:设置构建类型为Release(通常表示没有调试信息的版本)。include_directories(${CMAKE_SOURCE_DIR}/3rd/jsoncpp_simple/include)
:添加jsoncpp_simple库的头文件搜索路径。add_subdirectory(${CMAKE_SOURCE_DIR}/3rd/jsoncpp_simple)
:添加jsoncpp_simple库的源代码目录到构建目录。include_directories(${CMAKE_SOURCE_DIR}/3rd/wkt_parser/include)
:添加wkt_parser库的头文件搜索路径。include_directories(${CMAKE_SOURCE_DIR}/3rd/licenseAuth/include)
:添加licenseAuth库的头文件搜索路径。add_subdirectory(${CMAKE_SOURCE_DIR}/3rd/wkt_parser)
:添加wkt_parser库的源代码目录到构建目录。find_package(OpenCV REQUIRED)
:查找OpenCV库(一个计算机视觉库),如果找不到则报错。find_library(LICENSE_LIB licenseAuth ${CMAKE_SOURCE_DIR}/3rd/licenseAuth/lib)
:查找licenseAuth库并添加到LICENSE_LIB变量中。set(ENV{PKG_CONFIG_PATH} "ENV{PKG_CONFIG_PATH}")
:这行代码看起来有些问题,可能是复制粘贴时出现了错误。一般来说,我们不需要在这里设置PKG_CONFIG_PATH环境变量,因为CMake会自动处理环境变量。find_package(PkgConfig REQUIRED)
:查找PkgConfig库(用于解析其他库的配置文件),如果找不到则报错。include_directories(/usr/local/cuda-11.1/targets/x86_64-linux/include/)
:添加CUDA的头文件搜索路径(如果你要使用CUDA的话)。set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}:/usr/local/ev_sdk/lib")
:更新运行时库搜索路径,添加额外的路径。aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRCS)
:获取当前源代码目录下的所有源文件。list(APPEND libji_SOURCES ${SRCS})
:将上面的源文件列表添加到libji库的源文件列表中。add_library (${PROJECT_NAME} SHARED ${libji_SOURCES})
:添加共享库,名称为上面的PROJECT_NAME(通常为项目名称),源文件为上面添加的libji_SOURCES列表。
- target_include_directories(PROJECTNAMEPUBLIC{OpenCV_INCLUDE_DIR}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
)
21.添加了两个公共的头文件目录到你的库。第一个是OpenCV的头文件目录,第二个是你当前源代码的include目录。
- target_link_libraries(PROJECTNAME{OpenCV_LIBS}
${LICENSE_LIB}
glog
WKTParser
jsoncpp
nvinfer
nvonnxparser
-L/usr/local/cuda/lib64/ -lcudart
)
22.这一行指定了你的库需要链接到的其他库。比如OpenCV的库(OpenCV_LIBS),licenseAuth库(LICENSE_LIB),以及其他一些你可能需要的库,如glog、WKTParser、jsoncpp、nvinfer、nvonnxparser等。-L指定了CUDA的库路径,-lcudart指定了链接到CUDA的运行时库。
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
23.这行代码将你的库安装到lib目录下。
install(FILES ${CMAKE_SOURCE_DIR}/3rd/licenseAuth/lib/liblicenseAuth.so DESTINATION lib)
24.这行代码将licenseAuth库的liblicenseAuth.so文件也安装到lib目录下。
install(DIRECTORY ${CMAKE_SOURCE_DIR}/3rd/fonts DESTINATION lib)
25.这行代码将3rd/fonts目录下的所有文件(假设是字体文件)安装到lib目录下。
在结束这个CMakeLists.txt文件之前,通常会使用endif
来关闭if(NOT CMAKE_BUILD_TYPE MATCHES Debug)
的条件语句,但在这个代码中并没有看到对应的endif
。如果这部分代码是从一个更大的文件中截取的,那么可能是被删掉了。如果这是完整的文件,那么可能是一个错误,因为条件语句没有正确的关闭。
[100%] Linking CXX shared library libji.so /usr/bin/ld: cannot find -levdeploy collect2: error: ld returned 1 exit status CMakeFiles/ji.dir/build.make:150: recipe for target 'libji.so' failed make[2]: *** [libji.so] Error 1 CMakeFiles/Makefile2:99: recipe for target 'CMakeFiles/ji.dir/all' failed make[1]: *** [CMakeFiles/ji.dir/all] Error 2 Makefile:135: recipe for target 'all' failed make: *** [all] Error 2