[CMake] 기초 템플릿
2023. 3. 1. 14:06ㆍDevelopers 공간 [Shorts]/Software Basic
728x90
반응형
<분류>
A. 수단
- OS/Platform/Tool : Linux, Kubernetes(k8s), Docker, AWS
- Package Manager : node.js, yarn, brew,
- Compiler/Transpillar : React, Nvcc, gcc/g++, Babel, Flutter
- Module Bundler : React, Webpack, Parcel
B. 언어
- C/C++, python, Javacsript, Typescript, Go-Lang, CUDA, Dart, HTML/CSS
C. 라이브러리 및 프레임워크 및 SDK
- OpenCV, OpenCL, FastAPI, PyTorch, Tensorflow, Nsight
1. What? (현상)
Shell Script 기초 템플릿이 필요합니다.
2. Why? (원인)
- 하기에 미리 만들어 놓고자 합니다.
3. How? (해결책)
- 폴더구조
├── CMakeLists.txt : (1)
├── main.cpp
├── FOLDER_NAME1
├── CMakeLists.txt : (2)
├── temp.hpp
└── temp.cpp
└── FOLDER_NAME2
├── CMakeLists.txt : (2)
├── temp.hpp
└── temp.cpp - 가장 상위단의 CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MY_PROJECT)
#---------------------------------------
# Simple Options
#---------------------------------------
add_compile_options(-std=c++11 -Werror=return-type -Wall)
set(CMAKE_BUILD_TYPE Debug)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_FLAGS "-Wall -Wextra -pthread -Wno-deprecated-declarations")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
if(${CMAKE_BUILD_TYPE} MATCHES Debug)
#add_compile_definitions(_DEBUG)
endif()
message(STATUS "Build Status: ${CMAKE_BUILD_TYPE}")
#---------------------------------------
# Dependencies Basic
#---------------------------------------
set(INCLUDES
${CMAKE_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/include
)
include_directories(
${INCLUDE}
/usr/include/x86_64-linux-gnu
)
set(LIBS
${CMAKE_CURRENT_SOURCE_DIR}/abc.so
-L/usr/local/lib
-lpthread
)
link_libraries(${LIBS})
#---------------------------------------
# Compile
#---------------------------------------
#set(BUILD_LIBRARY 1)
file(GLOB_RECURSE TARGET_FILES
${CMAKE_CURRENT_SOURCE_DIR}/app/*.c,
${CMAKE_CURRENT_SOURCE_DIR}/app/*.cpp)
if(DEFINED BUILD_LIBRARY)
add_library(${PROJECT_NAME}
STATIC
${TARGET_FILES}
)
target_link_libraries(${PROJECT_NAME} PRIVATE
${LIBS}
)
else()
add_executable(${PROJECT_NAME}
main.cpp
${TARGET_FILES}
)
set_target_properties(${PROJECT_NAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}/binaryfolder
)
target_link_libraries(${PROJECT_NAME} PRIVATE
${LIBS}
)
endif()
#---------------------------------------
# Cuda Compile(Options)
#---------------------------------------
#[[
find_package(CUDA REQUIRED)
set(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} -gencode arch=compute_30,code=sm_30)
file(GLOB cu *.cu)
cuda_add_executable(${PROJECT_NAME}
main.cpp
${cu}
)
]]
#---------------------------------------
# Dependencies
#---------------------------------------
#[[
# 1. OpenCV
find_package(OpenCV 3.4.0 EXACT REQUIRED)
set(OPENCV_DIR
${CMAKE_CURRENT_SOURCE_DIR}/opencv/include
)
file(GLOB OPENCV_LIBS ${CMAKE_CURRENT_SOURCE_DIR}/opencv-3.4.9/lib/lib*.so)
set(OPENCV_LIBS
${OPENCV_DIR}/lib/libopencv_ml.so
${OPENCV_DIR}/lib/libopencv_dnn.so
${OPENCV_DIR}/lib/libopencv_core.so
${OPENCV_DIR}/lib/libopencv_cudev.so
${OPENCV_DIR}/lib/libopencv_flann.so
${OPENCV_DIR}/lib/libopencv_photo.so
${OPENCV_DIR}/lib/libopencv_shape.so
${OPENCV_DIR}/lib/libopencv_video.so
${OPENCV_DIR}/lib/libopencv_calib3d.so
${OPENCV_DIR}/lib/libopencv_highgui.so
${OPENCV_DIR}/lib/libopencv_imgproc.so
${OPENCV_DIR}/lib/libopencv_videoio.so
${OPENCV_DIR}/lib/libopencv_superres.so
${OPENCV_DIR}/lib/libopencv_imgcodecs.so
${OPENCV_DIR}/lib/libopencv_objdetect.so
${OPENCV_DIR}/lib/libopencv_stitching.so
${OPENCV_DIR}/lib/libopencv_videostab.so
${OPENCV_DIR}/lib/libopencv_cudaarithm.so
${OPENCV_DIR}/lib/libopencv_cudabgsegm.so
${OPENCV_DIR}/lib/libopencv_cudalegacy.so
${OPENCV_DIR}/lib/libopencv_cudastereo.so
${OPENCV_DIR}/lib/libopencv_features2d.so
${OPENCV_DIR}/lib/libopencv_cudafilters.so
${OPENCV_DIR}/lib/libopencv_cudaimgproc.so
${OPENCV_DIR}/lib/libopencv_cudaoptflow.so
${OPENCV_DIR}/lib/libopencv_cudawarping.so
${OPENCV_DIR}/lib/libopencv_cudaobjdetect.so
${OPENCV_DIR}/lib/libopencv_cudafeatures2d.so
)
include_directories(${OPENCV_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE ${OPENCV_LIBS})
]]
# 2.Cuda
#[[
include_directories(/usr/local/cuda-10.2/include)
target_link_libraries(${PROJECT_NAME} PRIVATE /usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudart.so)
]]
# 3.TensorRT
#[[
set(TRT_VERSION 7.0)
set(TRT_DIR
${CMAKE_CURRENT_SOURCE_DIR}/tensorRT
)
if(${TRT_VERSION} EQUAL 7.0)
include_directories(${TRT_DIR}/include)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvinfer.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvparsers.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvonnxparser.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvinfer_plugin.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libmyelin.so)
elseif(${TRT_VERSION} EQUAL 7.2)
include_directories(${TRT_DIR}/include)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvinfer.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvparsers.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvonnxparser.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvinfer_plugin.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libmyelin.so)
elseif(${TRT_VERSION} EQUAL 5.0)
include_directories(${TRT_DIR}/include)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvinfer.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvparsers.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvonnxparser.so)
target_link_libraries(${PROJECT_NAME} PRIVATE ${TRT_DIR}/lib/libnvinfer_plugin.so)
else()
message("ERROR : NO CUDA VERSION SUPPORTED")
endif()
]]
#---------------------------------------
# Sub Compile
#---------------------------------------
add_subdirectory(FOLDER_NAME1)
add_subdirectory(FOLDER_NAME2)
target_link_libraries(${PROJECT_NAME} ${FOLDER_NAME1_PROJECT} ${FOLDER_NAME2_PROJECT})
foreach(SAMPLE ${SAMPLES})
add_subdirectory(SOURCE/${SAMPLE})
message("-----------------\n\n\n${SAMPLE}\n\n\n----------------")
endforeach()
- 아래 단의 CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(MY_PROJECT_SUB)
#---------------------------------------
# Simple Options
#---------------------------------------
add_compile_options(-std=c++11 -Werror=return-type -Wall)
set(CMAKE_BUILD_TYPE Debug)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_FLAGS "-Wall -Wextra -pthread -Wno-deprecated-declarations")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
if(${CMAKE_BUILD_TYPE} MATCHES Debug)
#add_compile_definitions(_DEBUG)
endif()
message(STATUS "Build Status: ${CMAKE_BUILD_TYPE}")
#---------------------------------------
# Compile
#---------------------------------------
if(DEFINED BUILD_LIBRARY)
add_library(MY_PROJECT
MyFile1.cpp
MyFile2.cpp
MyFile3.cpp
)
else()
target_sources(MY_PROJECT PRIVATE
MyFile1.cpp
MyFile2.cpp
MyFile3.cpp
)
endif()
이를 이용해 CMakeLists를 잘 짜보도록하자.
mkdir build
cd build
cmake ..
make
cd ..
./build/binary/myfile
더보기
-----------------------------------------------------------------------------------------------------
<CMake 추가특징>
- ★add_executable
add_executable(${PROJECT_NAME} ${EXE_SOURCES})
ex) EXE_SOURCES : 필요한 소스파일들, A.cpp, B.cpp … - ★add_library(A 옵션 소스.cpp)
<옵션> STATIC : 정적, SHARED : 동적 - find_package
find_package(OpenCV 최소버전)
find_package(OpenCV 3.4.0 EXACT REQUIRED)
find_package(CUDA REQUIRED)
<옵션> EXACT : 정확한 버전, REQUIRED : 안찾아지면 throws an error, QUIET : 아무 메시지 안보이게 - cmake_current_source_dir : 현재 코드 ex)/FOLDER/MYFOLDER
cmake_current_binary_dir : build에서의 현재 ex)/FOLDER/build/MYFOLDER
cmake_current_list dir : 현재코드 ex)/FOLDER
cmake_binary_dir : build ex)/FOLDER/build
cmake_source_dir : 아예시작점 ex)/FOLDER
-----------------------------------------------------------------------------------------------------
728x90
반응형
'Developers 공간 [Shorts] > Software Basic' 카테고리의 다른 글
[Python] 간단한 프로그램 Multiprocessing 만들기 (0) | 2023.03.07 |
---|---|
[Bash] Process 실행 후에 시간이 지난 후 진행이 안될 때 (0) | 2023.03.07 |
[ShellScript] 기초 템플릿 (0) | 2023.02.28 |
[Terminology] WebApp, HybridApp, Responsive App, Platform, Framework... (0) | 2023.02.26 |
[Makefile] 기초 템플릿 (0) | 2023.02.16 |