development

CMake 출력 / 빌드 디렉토리

big-blog 2020. 8. 28. 07:54
반응형

CMake 출력 / 빌드 디렉토리


저는 CMake를 처음 접했으며 사용 방법에 대한 몇 가지 자습서를 읽고 3 개의 다른 컴파일러 용 프로그램을 만들기 위해 복잡한 50 줄의 CMake 스크립트를 작성했습니다. 이것은 아마도 CMake에 대한 모든 지식을 마칠 것입니다.

이제 내 문제는 프로그램을 만들 때 폴더를 만지거나 방해하고 싶지 않은 소스 코드가 있다는 것입니다. 모든 CMake 및 make출력 파일과 폴더가에 들어가기를 원 ../Compile/하므로 CMake 스크립트에서 몇 가지 변수를 변경했으며 내 노트북에서 다음과 같은 작업을 수행했을 때 언젠가는 작동했습니다.

Compile$ cmake ../src
Compile$ make

그것으로 내가 지금있는 폴더에 깨끗한 출력이 있었는데, 정확히 내가 찾고있는 것입니다.

이제 다른 컴퓨터로 옮겨서 CMake 2.8.11.2를 다시 컴파일했고 거의 정사각형으로 돌아 왔습니다! 항상 srcmy CMakeLists.txt있는 폴더로 물건을 컴파일합니다 .

내 CMake 스크립트에서 디렉토리를 선택하는 부분은 다음과 같습니다.

set(dir ${CMAKE_CURRENT_SOURCE_DIR}/../Compile/)
set(EXECUTABLE_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE)
set(LIBRARY_OUTPUT_PATH ${dir} CACHE PATH "Build directory" FORCE)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${dir})
set(CMAKE_BUILD_FILES_DIRECTORY ${dir})
set(CMAKE_BUILD_DIRECTORY ${dir})
set(CMAKE_BINARY_DIR  ${dir})
SET(EXECUTABLE_OUTPUT_PATH ${dir})
SET(LIBRARY_OUTPUT_PATH ${dir}lib)
SET(CMAKE_CACHEFILE_DIR ${dir})

그리고 이제는 항상 다음으로 끝납니다.

-- Build files have been written to: /.../src

내가 뭔가를 놓치고 있습니까?


설정하는 모든 변수를 설정할 필요가 거의 없습니다. CMake는 적절한 기본값으로 설정합니다. 당신은 확실히해야 하지 , 수정 CMAKE_BINARY_DIR또는 CMAKE_CACHEFILE_DIR. 읽기 전용으로 취급하십시오.

먼저 src 디렉터리에서 문제가있는 기존 캐시 파일을 제거합니다.

cd src
rm CMakeCache.txt
cd ..

그런 다음 모든 set()명령을 제거하고 다음을 수행하십시오.

cd Compile
rm -rf *
cmake ../src

CMake를 실행할 때 소스 디렉터리 외부에있는 한 CMakeList가 명시 적으로 지시하지 않는 한 소스 디렉터리를 수정하지 않습니다.

이 작업이 완료되면 CMake가 기본 위치에 항목을 배치하고 기본 위치 (예 : 기본값)가 만족스럽지 않은 경우에만 필요한 위치 EXECUTABLE_OUTPUT_PATH만 수정할 수 있습니다. 그리고 상대에게 표현하려고 CMAKE_BINARY_DIR, CMAKE_CURRENT_BINARY_DIR, PROJECT_BINARY_DIR

CMake 문서를 보면 의미 론적 섹션으로 분할 된 변수를 볼 수 있습니다. 매우 특별한 상황을 제외하고는 "정보를 제공하는 변수"에 나열된 모든 항목을 CMakeLists 내에서 읽기 전용으로 처리해야합니다.


아웃 오브 소스 빌드 를 원하는 것 같습니다 . 소스 외부 빌드를 생성 할 수있는 몇 가지 방법이 있습니다.

  1. 하던 일을하고 달려

    cd /path/to/my/build/folder
    cmake /path/to/my/source/folder
    

    이는 cmake가 발생하게됩니다 빌드 트리/path/to/my/build/folder위한 소스 트리 에서 /path/to/my/source/folder.

    일단 생성하면 cmake는 소스 폴더의 위치를 ​​기억하므로 빌드 트리에서 cmake를 다시 실행할 수 있습니다.

    cmake /path/to/my/build/folder
    

    또는

    cmake .
    

    현재 디렉토리가 이미 빌드 폴더 인 경우.

  2. 일부 사용하여 소스와 빌드 폴더를 설정하는 문서화되지 않은 옵션 :

    cmake -B/path/to/my/build/folder -H/path/to/my/source/folder
    

    (1)과 똑같은 작업을 수행하지만 현재 작업 디렉토리에 의존하지 않습니다.

CMake 는 기본적으로 모든 출력을 빌드 트리 에 배치하므로 자유롭게 ${CMAKE_SOURCE_DIR}또는 ${CMAKE_CURRENT_SOURCE_DIR}cmake 파일을 사용하지 않는 한 소스 트리를 건드리지 않아야합니다 .

잘못 될 수있는 가장 큰 문제는 이전에 소스 트리에서 빌드 트리를 생성 한 경우입니다 (즉 , 소스 빌드가있는 경우). 이 작업을 완료하면 위의 (1)의 두 번째 부분이 시작되고 cmake는 소스 또는 빌드 위치를 변경하지 않습니다. 따라서 소스 내 빌드를 사용하여 소스 디렉터리에 대한 소스 외부 빌드를 만들 수 없습니다 . CMakeCache.txt소스 디렉토리에서 ( 최소한) 제거하면이 문제를 상당히 쉽게 해결할 수 있습니다 . CMakeFilesCMake가 생성 하는 몇 가지 다른 파일 (대부분 디렉터리에 있음)도 제거해야하지만 cmake가 소스 트리를 빌드 트리로 취급하지는 않습니다.

Since out-of-source builds are often more desirable than in-source builds, you might want to modify your cmake to require out of source builds:

# Ensures that we do an out of source build

MACRO(MACRO_ENSURE_OUT_OF_SOURCE_BUILD MSG)
     STRING(COMPARE EQUAL "${CMAKE_SOURCE_DIR}"
     "${CMAKE_BINARY_DIR}" insource)
     GET_FILENAME_COMPONENT(PARENTDIR ${CMAKE_SOURCE_DIR} PATH)
     STRING(COMPARE EQUAL "${CMAKE_SOURCE_DIR}"
     "${PARENTDIR}" insourcesubdir)
    IF(insource OR insourcesubdir)
        MESSAGE(FATAL_ERROR "${MSG}")
    ENDIF(insource OR insourcesubdir)
ENDMACRO(MACRO_ENSURE_OUT_OF_SOURCE_BUILD)

MACRO_ENSURE_OUT_OF_SOURCE_BUILD(
    "${CMAKE_PROJECT_NAME} requires an out of source build."
)

The above macro comes from a commonly used module called MacroOutOfSourceBuild. There are numerous sources for MacroOutOfSourceBuild.cmake on google but I can't seem to find the original and it's short enough to include here in full.

Unfortunately cmake has usually written a few files by the time the macro is invoked, so although it will stop you from actually performing the build you will still need to delete CMakeCache.txt and CMakeFiles.

You may find it useful to set the paths that binaries, shared and static libraries are written to - in which case see how do I make cmake output into a 'bin' dir? (disclaimer, I have the top voted answer on that question...but that's how I know about it).


As of CMake Wiki:

CMAKE_BINARY_DIR if you are building in-source, this is the same as CMAKE_SOURCE_DIR, otherwise this is the top level directory of your build tree

Compare these two variables to determine if out-of-source build was started


Turning my comment into an answer:

In case anyone did what I did, which was start by putting all the build files in the source directory:

cd src
cmake .

cmake will put a bunch of build files and cache files (CMakeCache.txt, CMakeFiles, cmake_install.cmake, etc) in the src dir.

To change to an out of source build, I had to remove all of those files. Then I could do what @Angew recommended in his answer:

mkdir -p src/build
cd src/build
cmake ..

First of all you should not relay on build dir name in your script. line with ../Compile must be changed.

It's because it's up to user where to compile.

Instead of that use one of predefined variables: http://www.cmake.org/Wiki/CMake_Useful_Variables (look for CMAKE_BINARY_DIR and CMAKE_CURRENT_BINARY_DIR)

참고URL : https://stackoverflow.com/questions/18826789/cmake-output-build-directory

반응형