CMakeLists.txt

CMakeLists.txt

后附带 几个典型例子可供参考

配置工程

cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(main)
set(CMAKE_CXX_STANDARD 11)

|----------------------- 配置路径 -----------------|

set 各大root路径
set_ifndef(TORCH_HOME /Volumes/CO_OS/Mac/libtorch)
set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}set_ifndef(OPENCV_HOME /Volumes/CO_OS/Mac/libopencv)
set_ifndef(CUDA_HOME /user/local/CUDA)

add_subdirectory(FC) # 添加子目录 include cmake file
aux_source_directory(. DIR_SRCS)
aux_source_directory(. TORCH_HOME) # 搜索该目录下的所有.cpp文件

set 变量

set(TORCH_INC_DIR /Volumes/CO_OS/Mac/libtorch/include)
set(TRT_INCLUDE /Volumes/CO_OS/Mac/TensorRT/include)
set(CUDA_INC_DIR /user/local/CUDA/include)
set(PYBind /Users/home/pybing/include)

头文件 .h

**# include** 
include_directories(
    ${CUDA_INC_DIR}
    ${PYBind}

    ${TRT_INCLUDE} 
    ${TORCH_INC_DIR}
)

库文件 lib

set(TORCH_LIB /Volumes/CO_OS/Mac/libtorch/lib/torch.so)
SET{PYBind_LIB /Volumes/CO_OS/Mac/libtorch/lib/torch.so}
file(GLOB CV_LIBS "${OPENCV_HOME}/lib*.a")

LINK_DIRECTORIES(CUDA_HOME) #添加默认路径留fing_library使用
LINK_DIRECTORIES(OPENCV_HOME)

find_library(_NVINFER_LIB nvinfer # 将下面的路径中寻找 nvinfer.lib文件 将绝对全路径地址返回到 _NVINFER_LIB
                HINTS 
                ${TORCH_HOME} 
                PATH_SUFFIXES 
                lib 
                lib64
    )  #获得全路径 "/opt/MATLAB/R2012a/bin/glnxa64/libeng.so"
set_ifndef(NVINFER_LIB ${_NVINFER_LIB})
link_libraries(_NVINFER_LIB) # 好像直接连接到 主工程 而且不用使用  target_link_libraries()

|-------- 编译 lib so a、执行文件exe -------|

用于生成库文件 libxxx.so/a 然后保存或者直接给工程使用
message(” Begin to generate libs !”)
add_library(FC SHARED
    #${CMAKE_CURRENT_SOURCE_DIR}/plugin/clipKernel.cu
    #${CMAKE_SOURCE_DIR}/plugin/customClipPlugin.cpp
    #${CMAKE_SOURCE_DIR}/plugin/clipKernel.h
    #${CMAKE_SOURCE_DIR}/plugin/customClipPlugin.h
    ${CMAKE_CURRENT_SOURCE_DIR}/FC/FC.h
    ${CMAKE_CURRENT_SOURCE_DIR}/FC/FC.cpp)
# or
cuda_add_library(FC SHARED 
    #${CMAKE_CURRENT_SOURCE_DIR}/plugin/clipKernel.cu
    #${CMAKE_SOURCE_DIR}/plugin/customClipPlugin.cu
    ${CMAKE_SOURCE_DIR}/plugin/clipKernel.cuh)

# final
target_link_libraries(FC PRIVATE ${NVINFER_LIB})
target_link_libraries(FC PRIVATE ${TORCH_LIB})
target_link_libraries(FC PRIVATE ${PYBind_LIB})


# python inference scripts 用 于 混 编
set_target_properties(FC PROPERTIES
  CUDA_SEPARABLE_COMPILATION ON
)
生成可执行文件 : libeng.so = eng = -leng
add_executable(main main.cpp)
target_link_libraries(main FC)
target_link_libraries(main ${TORCH_HOME}/lib/torch.so)
target_include_directories(main PRIVATE ${TORCH_INC_DIR})
target_compile_features(clipplugin PUBLIC cxx_std_11)# Use C++11

Example 1:杂项 非全 可参考

使用cmake链接 qt 、libtorch 、tensorrt、cuda 、opencv、extra_include、extra_lib (openGL)
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)

# 设置各大路径   qt 、libtorch 、tensorrt、cuda 、opencv、extra_include、extra_lib
set(target_name "myproject")
set(OpenCV_DIR /Users/macos/opencv-3.4.4/build_xcode)
set(Torch_DIR /Users/macos/anaconda3/lib/python3.7/site-packages/torch/share/cmake/Torch) 
set(CUDA_HOME /usr/local/cuda) 
set(RT_DIR /home/macos/TensorRT-7.0.0.11/) 

#begin to build
project(${target_name})

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

set(CUDA_NVCC_PLAGS ${CUDA_NVCC_PLAGS};-std=c++11;-g;-G;-gencode;arch=compute_61;code=sm_61)

# Try to set qt auto generater
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

# Try to find OpenCV
find_package(OpenCV REQUIRED)
find_package(CUDA REQUIRED)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets core REQUIRED)
find_package(Torch PATHS ${Torch_DIR} NO_DEFAULT REQUIRED)

include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${CUDA_HOME}/include)
include_directories(${RT_DIR}/include)
include_directories(OpenCV_INCLUDE_DIRS)
include_directories(${extra_include})

link_directories((${PROJECT_SOURCE_DIR}/include)
link_directories(${CUDA_HOME}/lib64)
link_directories(${RT_DIR}/lib64)
link_directories(OpenCV_INCLUDE_DIRS)
link_directories(${extra_lib})

# cuda_add_library()
# add_executable()
add_executable(target_name 
		main.cpp         
		main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui)
target_link_libraries(target_name nvinfer nvinfer_plugin nvparsers cudart cublas stdc++fs )
target_link_libraries(target_name PRIVATE Qt${QT_VERSION_MAJOR}::Widgets ${OpenCV_LIBS})
target_link_libraries(target_name PRIVATE ${OpenCV_LIBS})

add_definitions(-O2 -pthread)

Example 2 : libtorch + opencv

cmake_minimum_required(VERSION 3.5.1)
project(libtorch-yolov5)

set(CMAKE_CXX_STANDARD 14)
# It prevents the decay to C++98 when the compiler does not support C++14
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# It disables the use of compiler-specific extensions
# e.g. -std=c++14 rather than -std=gnu++14
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")

# Try to find OpenCV
#. set(OpenCV_DIR /Users/cxu/opencv-3.4.4/build_xcode)
find_package(OpenCV REQUIRED)
if (OpenCV_FOUND)
    message(STATUS "OpenCV library status:")
    message(STATUS "    version: ${OpenCV_VERSION}")
    message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}" \n)
else ()
    message(FATAL_ERROR "Could not locate OpenCV" \n)
endif()

set(Torch_DIR /Users/cxu/anaconda3/lib/python3.7/site-packages/torch/share/cmake/Torch)
# /home/cx/anaconda3/envs/torch17/lib/python3.7/site-packages/torch/share/cmake/Torch
find_package(Torch PATHS ${Torch_DIR} NO_DEFAULT REQUIRED)
if (Torch_FOUND)
    message(STATUS "Torch library found!")
    message(STATUS "    include path: ${TORCH_INCLUDE_DIRS}" \n)
else ()
    message(FATAL_ERROR "Could not locate Torch" \n)
endif()

include_directories( ${PROJECT_SOURCE_DIR}/models ${PROJECT_SOURCE_DIR}/utils ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/dependency/yaml-cpp)
link_directories()
link_libraries( ${CMAKE_HOME_DIRECTORY}/dependency/yaml-cpp/libyaml-cpp.a )

file(GLOB SOURCE_FILE models/*.cpp utils/*.cpp)

add_library(common ${SOURCE_FILE})
add_executable(train train.cpp )
add_executable(test test.cpp )
add_executable(detect detect.cpp )

target_link_libraries (
    common
    ${OpenCV_LIBS}
    ${TORCH_LIBRARIES}
)

target_link_libraries (train
        ${OpenCV_LIBS}
        ${TORCH_LIBRARIES}
)
target_link_libraries (test
        ${OpenCV_LIBS}
        ${TORCH_LIBRARIES}
)
target_link_libraries (detect
    common
)

Example 3 : QT + opencv

需要安装了 moc uic rcc 工具qt依赖,python包自带或安装 qt-opensources design工程
之后可用 qmake 或者 cmake ,qmake简单,cmake强大

cmake_minimum_required(VERSION 3.5)
project(dark LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# for QT ,important
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

# qt
find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets core REQUIRED)

# opencv
set(OpenCV_DIR /Users/cxu/opencv-3.4.4/build_xcode)
find_package(OpenCV)
include_directories(OpenCV_INCLUDE_DIRS)
message(find_package(OpenCV))


# libtorch
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/ /Users/cxu/libtorch/include ) 
link_directories(${CMAKE_CURRENT_SOURCE_DIR} /Users/cxu/Desktop/dark/qt/darknet/)
message(${CMAKE_CURRENT_SOURCE_DIR})


# merge the files
set(PROJECT_SOURCES
        main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
        yolo_v2_class.hpp
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(dark
        ${PROJECT_SOURCES}
    )
else()
    if(ANDROID)
        add_library(dark SHARED
            ${PROJECT_SOURCES}
        )
    else()
        add_executable(dark 
            ${PROJECT_SOURCES} 
        )
    endif()
endif()

target_link_libraries(dark PRIVATE 
		Qt${QT_VERSION_MAJOR}::Widgets 
		libdarknet.so 
		${OpenCV_LIBS}
)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code-keys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值