cmake 간단명료 튜 토리 얼 - 30 분 입문 부터 정통 까지

8486 단어 공구.
참고 문헌:
입문 우선:http://www.hahack.com/codes/cmake/#
공식 튜 토리 얼:https://cmake.org/cmake-tutorial/
공식 튜 토리 얼 번역문:https://juejin.im/post/5a72775d6fb9a01cac187e96
간단 한 조작 문법:https://learnxinyminutes.com/docs/cmake/
공식 cmake, ctest, cpack 소개:https://cmake.org/cmake/help/v3.11/
원본 코드 루틴:https://gitee.com/qccz123456/learn_cmake, git log 를 통 해 모든 단계 의 절 차 를 볼 수 있 습 니 다.
책:http://sewm.pku.edu.cn/src/paradise/reference/CMake%20Practice.pdf
/*
step1:
    cmake -DUSE_MYSQRT=ON -DCMAKE_INSTALL_PREFIX=/home/eagle/Desktop/cmake_learn/build/output ..
step2:
    make
step3:
    make test
step4:
    make install
step5:
    cpack -C xxxx

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./build/output/lib
*/
/*
LearnCmake
  |__CMakeLists.txt
  |__LearnCmake_config.h.in
  |__main.cpp
  |__License.txt
  |__README
  |__feature
       |__CMakeLists.txt
       |__mysqrt.h
       |__mysqrt.cpp
*/ 
[LearnCmake/CMakeLists.txt]
/*       
cmake_minimum_required (VERSION 2.6)
project (LearnCmake)

# the version number, like define,        
set (LearnCmake_VERSION_MAJOR 1)
set (PROJECT_VERSION_MINOR 0)
set (CMAKE_VERSION_PATCH 0)

# Set the output folder where your program will be created
set (CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set (EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set (LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

# Debug/Release settings
set (CMAKE_BUILD_TYPE "Debug")
set (CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set (CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

message (STATUS "src dir = ${PROJECT_SOURCE_DIR}")
message (STATUS "bin dir = ${PROJECT_BINARY_DIR}")
#message (WARNING "This is Warning")   # continue processing 
#message (SEND_ERROR "This is error")  # continue processing, but skip generation
#message (FATAL_ERROR "This is fatal") # stop processing and generation

# open make log
#set (CMAKE_VERBOSE_MAKEFILE ON)

# check system environment
include (CheckFunctionExists)
check_function_exists (pow HAVE_POW)
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)

# configure a header file to pass CMake settings to source code
# compile config.h.in to config.h
configure_file (
    "${PROJECT_SOURCE_DIR}/LearnCmake_config.h.in"
    "${PROJECT_BINARY_DIR}/LearnCmake_config.h"
)

# add the binary tree to the search path for include files
# so that we will find LearnCmake_config.h, like gcc -I
include_directories ("${PROJECT_BINARY_DIR}")

# set if USE_MYSQRT is defined in LearnCmake_config.h
# LearnCmake_config.h.in must use "#cmakedefine"
# ON/OFF value is related to cmake cache, is not initial value
# if define, must use "cmake -DUSE_MYSQRT=ON" 
option (USE_MYSQRT "use mysqrt library" ON)

if (USE_MYSQRT)
    # compile other source code files to libraries
    include_directories ("${PROJECT_SOURCE_DIR}/feature")
    add_subdirectory ("feature") #   feature    CMakeLists.txt
    set (EXTRA_LIBS ${EXTRA_LIBS} MySqrt)
endif (USE_MYSQRT)

# add the executable to run "./LearnCmake"
add_executable (LearnCmake main.cpp)
# add the executable, like gcc -L
target_link_libraries (LearnCmake ${EXTRA_LIBS})

# add install
install (TARGETS LearnCmake DESTINATION bin)
install (FILES ${PROJECT_BINARY_DIR}/LearnCmake_config.h DESTINATION include)

# add simple test
include (CTest)
add_test (LearnCmake LearnCmake)
add_test (LearnCmake2 LearnCmake)
set_tests_properties (LearnCmake2 PROPERTIES PASS_REGULAR_EXPRESSION "4")

# add macro test
macro (do_test arg result)
  add_test (test${arg} LearnCmake ${arg})
  set_tests_properties (test${arg} PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)
# do a bunch of result based tests
do_test (0 "4")
do_test (2 "4")

# add cpack to pack binary/source code
include (InstallRequiredSystemLibraries)
set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set (CPACK_PACKAGE_VERSION_MAJOR "${LearnCmake_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${LearnCmake_VERSION_MINOR}")
set (CPACK_PACKAGE_VERSION_PATCH "${LearnCmake_VERSION_PATCH}")
include (CPack)
# pack binary package: cpack -C CPackConfig.cmake
# pack source package: cpack -C CPackSourceConfig.cmake
*/
[LearnCmake_config.h.in]
/*
//the configured options and settings for LearnCmake and direct copy
#define LearnCmake_VERSION_MAJOR @LearnCmake_VERSION_MAJOR@
#define LearnCmake_VERSION_MINOR @LearnCmake_VERSION_MINOR@

//cmake`s option to compile
#cmakedefine USE_MYSQRT
#cmakedefine HAVE_POW
*/
[main.cpp]

#include 
#include 
#include "LearnCmake_config.h"

#ifdef USE_MYSQRT
#include "mysqrt.h"
#endif

int main(void)
{
    std::cout << "learn cmake" << std::endl;
    std::cout << LearnCmake_VERSION_MAJOR << "." << LearnCmake_VERSION_MINOR << std::endl;
#ifdef USE_MYSQRT
    std::cout << mysqrt(2) << std::endl;
#endif
#ifdef HAVE_POW
    std::cout << pow(3, 2) << std::endl;
#endif

    return 0;
}
[LearnCmake/feature/CMakeLists.txt]
/*
#add_library (MySqrt mysqrt.cpp) # It is equal to the following :

# aux_source_directory find all src files of current directory 
aux_source_directory (. SRCS_LIB_DIR)
add_library (MySqrt SHARED ${SRCS_LIB_DIR})
add_library (MySqrt_static STATIC ${SRCS_LIB_DIR})

# add install : export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./build/output/lib
install (TARGETS MySqrt DESTINATION lib)
install (TARGETS MySqrt_static DESTINATION lib)
install (FILES mysqrt.h DESTINATION include)
*/
[mysqrt.h]

#ifndef __MYSQRT__
#define __MYSQRT__

int mysqrt(int num);

#endif


[mysqrt.cpp]

#include "mysqrt.h"

int mysqrt(int num)
{
    return num * num;
}

VS 의 NuGet 다운로드 의존 패 키 지 를 사용 합 니 다. 이 부분 은 packages. config 파일 복사 와 링크 에 필요 한 파일 의존 관계 만 진행 합 니 다.
  # use NuGet to manage 3-rd party libs for VS project
  configure_file(packages.config ${CMAKE_CURRENT_BINARY_DIR} COPYONLY)
  function(GET_TARGET id version targets)
    set(${targets} ${CMAKE_BINARY_DIR}/packages/${id}.${version}/build/native/${id}.targets PARENT_SCOPE)
  endfunction()
  # make sure the version number should align with packages.config
  GET_TARGET(boost 1.64.0.0 BOOST)
  GET_TARGET(boost_program_options-vc140 1.64.0.0 BOOST_PROGRAM_OPTIONS)
  GET_TARGET(boost_filesystem-vc140 1.64.0.0 BOOST_FILE_SYSTEM)
  GET_TARGET(boost_system-vc140 1.64.0.0 BOOST_SYSTEM)
  set(Boost_LIBRARIES ${BOOST} ${BOOST_PROGRAM_OPTIONS} ${BOOST_FILE_SYSTEM} ${BOOST_SYSTEM})

list 명령, REMOVEITEM 은 목록 에서 어떤 요 소 를 삭제 합 니 다:
aux_source_directory(. SRC_LIST)

if(NOT USE_MYX)
  list(REMOVE_ITEM SRC_LIST ./MXDevice.cpp)
endif()

configure_file 과 file 명령 의 차이 점:
You may consider using configure_file with the  COPYONLY  option:
configure_file(  COPYONLY)

Unlike  file(COPY ...)  it creates a file-level dependency between input and output, that is:
If the input file is modified the build system will re-run CMake to re-configure the file and generate the build system again.
find_패키지 명령:https://www.jianshu.com/p/46e9b8a6cb6a
include_directories(/home/wjg/projects/caffe/build/install/include)add_executable(useSSD ssd_detect.cpp)target_link_libraries(useSSD /home/wjg/projects/caffe/build/install/lib/libcaffe.so)
위 에 의존 하 는 라 이브 러 리 가 많 으 면 하나씩 추가 하 는 것 이 번 거 롭 습 니 다. find 를 사용 할 수 있 습 니 다.package () 간소화 작업:
set (Cafe DIR / home / lcq / projects / caffe / build) \ # CafeConfig. make 의 검색 경 로 를 추가 합 니 다 findpackage(Caffe REQUIRED)if (NOT Caffe_FOUND)    message(FATAL_ERROR "Caffe Not Found!")endif (NOT Caffe_FOUND)include_directories(${Caffe_INCLUDE_DIRS})add_executable(useSSD ssd_detect.cpp)target_link_libraries(useSSD ${Caffe_LIBRARIES})
다음 두 가지 방법 은 같다. \ # method 1
include_directories("./include")link_directories("./lib")target_link_libraries(hello libeng.so)target_link_libraries(hello eng)target_link_libraries(hello -leng)
# method 2
include_directories("./include")link_libraries("./lib/libeng.so")

좋은 웹페이지 즐겨찾기