수색…


소개

configure_file은 파일을 다른 위치로 복사하고 그 내용을 수정하는 CMake 함수입니다. 이 함수는 경로, 사용자 정의 변수, 일반 템플릿을 사용하여 구성 파일을 생성하는 데 매우 유용합니다.

비고

파일을 다른 위치로 복사하고 내용을 수정하십시오.

configure_file(<input> <output>
           [COPYONLY] [ESCAPE_QUOTES] [@ONLY]
           [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])

파일을 파일에 복사하고 파일 내용에서 참조 된 변수 값으로 대체합니다. if는 상대 경로이며 현재 소스 디렉토리를 기준으로 평가됩니다. 디렉토리가 아닌 파일이어야합니다. if는 상대 경로이며 현재 바이너리 디렉토리와 관련하여 평가됩니다. 이름을 기존 디렉토리로 지정하면 입력 파일은 해당 디렉토리에 원래 이름으로 배치됩니다.

파일이 수정되면 빌드 시스템은 CMake를 다시 실행하여 파일을 재구성하고 빌드 시스템을 다시 생성합니다.

이 명령은 $ {VAR} 또는 @ VAR @으로 참조되는 입력 파일의 모든 변수를 CMake가 결정한 값으로 바꿉니다. 변수가 정의되어 있지 않으면 변수는 아무것도 대체되지 않습니다. COPYONLY가 지정되면 변수 확장이 수행되지 않습니다. ESCAPE_QUOTES가 지정되면 치환 된 따옴표는 C 스타일로 이스케이프됩니다. 파일은 CMake 변수의 현재 값으로 구성됩니다. @ONLY를 지정하면 @ VAR @ 형식의 변수 만 대체되고 $ {VAR}은 무시됩니다. $ {VAR}을 사용하는 스크립트를 구성 할 때 유용합니다.

"#cmakedefine VAR ..."형식의 입력 파일 행은 CMake에서 VAR이 false로 간주되지 않는 값으로 설정되었는지 여부에 따라 "#define VAR ..."또는 / * #undef VAR * /로 대체됩니다 if () 명령으로 상수. ( "..."의 내용이있는 경우 위와 같이 처리됩니다.) "#cmakedefine01 VAR"형식의 입력 파일 행은 마찬가지로 "#define VAR 1"또는 "#define VAR 0"으로 대체됩니다.

NEWLINE_STYLE을 사용하면 줄의 끝을 조정할 수 있습니다.

'UNIX' or 'LF' for \n, 'DOS', 'WIN32' or 'CRLF' for \r\n.

NEWLINE_STYLE에는 COPYONLY을 사용하지 않아야합니다.

CMake로 c ++ 설정 파일 생성하기

우리가 커스텀 경로 나 변수와 함께 config.h 설정 파일을 사용하는 c ++ 프로젝트를 가지고 있다면, CMake와 일반적인 파일 config.h.in을 사용하여 생성 할 수 있습니다.

config.h.in은 자식 저장소의 일부가 될 수 있지만 생성 된 파일 config.h는 현재 환경에서 생성되므로 절대 추가되지 않습니다.

#CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)

SET(PROJ_NAME "myproject")
PROJECT(${PROJ_NAME})

SET(${PROJ_NAME}_DATA     ""     CACHE PATH "This directory contains all DATA and RESOURCES")
SET(THIRDPARTIES_PATH    ${CMAKE_CURRENT_SOURCE_DIR}/../thirdparties      CACHE PATH "This directory contains thirdparties")

configure_file ("${CMAKE_CURRENT_SOURCE_DIR}/common/config.h.in"
            "${CMAKE_CURRENT_SOURCE_DIR}/include/config.h" )

우리가 config.h.in을 가지고 있다면 :

cmakedefine PATH_DATA "@myproject_DATA@"
cmakedefine THIRDPARTIES_PATH "@THIRDPARTIES_PATH@"

이전의 CMakeList는 다음과 같은 c ++ 헤더를 생성합니다 :

#define PATH_DATA "/home/user/projects/myproject/data"
#define THIRDPARTIES_PATH "/home/user/projects/myproject/thirdparties"

SDL2 제어 버전을 기반으로 한 Examble

cmake 모듈이 있다면. 모든 config 파일을 저장하기 위해 in 이라는 폴더를 만들 수 있습니다.

예를 들어 FOO 라는 프로젝트가 FOO_config.h.in 과 같이 FOO_config.h.in 파일을 만들 수 있습니다.

//===================================================================================
//  CMake configuration file, based on SDL 2 version header
// ===================================================================================

#pragma once

#include <string>
#include <sstream>

namespace yournamespace
{
  /**
 *  \brief Information the version of FOO_PROJECT in use.
 *
 *  Represents the library's version as three levels: major revision
 *  (increments with massive changes, additions, and enhancements),
 *  minor revision (increments with backwards-compatible changes to the
 *  major revision), and patchlevel (increments with fixes to the minor
 *  revision).
 *
 *  \sa FOO_VERSION
 *  \sa FOO_GetVersion
 */
typedef struct FOO_version
{
    int major;        /**< major version */
    int minor;        /**< minor version */
    int patch;        /**< update version */
} FOO_version;

/* Printable format: "%d.%d.%d", MAJOR, MINOR, PATCHLEVEL
*/
#define FOO_MAJOR_VERSION   0
#define FOO_MINOR_VERSION   1
#define FOO_PATCHLEVEL      0

/**
 *  \brief Macro to determine FOO version program was compiled against.
 *
 *  This macro fills in a FOO_version structure with the version of the
 *  library you compiled against. This is determined by what header the
 *  compiler uses. Note that if you dynamically linked the library, you might
 *  have a slightly newer or older version at runtime. That version can be
 *  determined with GUCpp_GetVersion(), which, unlike GUCpp_VERSION(),
 *  is not a macro.
 *
 *  \param x A pointer to a FOO_version struct to initialize.
 *
 *  \sa FOO_version
 *  \sa FOO_GetVersion
 */
#define FOO_VERSION(x)                          \
{                                   \
    (x)->major = FOO_MAJOR_VERSION;                 \
    (x)->minor = FOO_MINOR_VERSION;                 \
    (x)->patch = FOO_PATCHLEVEL;                    \
}

/**
 *  This macro turns the version numbers into a numeric value:
 *  \verbatim
    (1,2,3) -> (1203)
    \endverbatim
 *
 *  This assumes that there will never be more than 100 patchlevels.
 */
#define FOO_VERSIONNUM(X, Y, Z)                     \
    ((X)*1000 + (Y)*100 + (Z))

/**
 *  This is the version number macro for the current GUCpp version.
 */
#define FOO_COMPILEDVERSION \
    FOO_VERSIONNUM(FOO_MAJOR_VERSION, FOO_MINOR_VERSION, FOO_PATCHLEVEL)

/**
 *  This macro will evaluate to true if compiled with FOO at least X.Y.Z.
 */
#define FOO_VERSION_ATLEAST(X, Y, Z) \
    (FOO_COMPILEDVERSION >= FOO_VERSIONNUM(X, Y, Z))

}

// Paths
#cmakedefine FOO_PATH_MAIN "@FOO_PATH_MAIN@"

이 파일은 cmake 변수의 FOO_PATH_MAIN 정의 된 변수와 함께 설치 경로에 FOO_config.h 를 작성합니다. 당신이 포함 할 필요를 생성하려면 in (설정 경로 및 변수)과 같이, 당신의 CMakeLists.txt에 파일 :

MESSAGE("Configuring FOO_config.h ...")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/common/in/FOO_config.h.in"
"${FOO_PATH_INSTALL}/common/include/FOO_config.h" )

이 파일에는 템플릿의 데이터와 실제 경로의 변수가 포함됩니다 (예 :

// Paths
#define FOO_PATH_MAIN "/home/YOUR_USER/Respositories/git/foo_project"


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow