Cmake's file directory planning and settings

cmake sets the location of the generated files - Programmer Sought

This article mainly analyzes the difference between the four variables in cmake:

  1. CMAKE_ARCHIVE_OUTPUT_DIRECTORY: The folder location where the static library is stored by default;
  2. CMAKE_LIBRARY_OUTPUT_DIRECTORY: The folder location where the dynamic library is stored by default;
  3. LIBRARY_OUTPUT_PATH: The location where the library files are stored by default. If a static library is generated and CMAKE_ARCHIVE_OUTPUT_DIRECTORY is not specified, it will be stored in this directory, and the dynamic library is similar;
  4. CMAKE_RUNTIME_OUTPUT_DIRECTORY: directory for storing executable software;

The above is the conclusion. If you understand it, you don't need to read the following experiments. If you don't understand it, understand it based on experiments.

Three types of useful files generated by linux during program compilation are: .astatic library files at the end, .sodynamic library files at the end, and executable files. Below we use a stand to illustrate the use of the above four variables.

Build a folder with the following structure:
.
├── CMakeLists.txt ├──
archive
├── fun
│ ├── CMakeLists.txt
│ └── fun.cpp
├── library
├── output
└── runtime The content of is:
./CMakeListLists.txt

cmake_minimum_required(VERSION 3.1) 
project(TEST)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/archive)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/library)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/runtime)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/path)
add_subdirectory(fun)

fun/CMakeLists.txtThe content of is:

cmake_minimum_required(VERSION 3.1) 
project(fun)
add_library(fun fun.cpp)

fun/fun.cppThe content of is:

int add(int a, int b){return a+b;}
int main(){return 0;}

testCMAKE_ARCHIVE_OUTPUT_DIRECTORY

  • Execute cmake .; make, you can see that the static library file is generated archiveunder libfun.a.

  • If you ./CMakeLists.txtcomment out the third line in and execute it make, you can find that it is generated pathunder libfun.a;

  • If you ./CMakeLists.txtcomment out the 3rd and 6th lines in and execute it make, you can find that it is generated fununder libfun.a.

testCMAKE_LIBRARY_OUTPUT_DIRECTORY

  • First fun/CMakeLists.txtchange the third line of to:add_library(fun SHARED fun.cpp)
  • Execute make, you can librarysee libfun.sothe file in the directory (if it is a mac os system, you can see it libfun.dylib)
  • If you ./CMakeLists.txtcomment out the 4th line in and execute it make, you can find that the file is generated pathunder libfun.so;
  • If you ./CMakeLists.txtcomment out the 4th and 6th lines in and execute it make, you can find that the file is generated fununder libfun.so;

testCMAKE_RUNTIME_OUTPUT_DIRECTORY

  • First fun/CMakeLists.txtchange the third line of to:add_executable(exe_file fun.cpp)
  • Execute make, created the executable runtimeunderexe_file
  • Comment ./CMakeLists.txtout line 5 in , execute make, and create it exe_fileunder funthe folder.

Through the above three experiments, we should be able to understand the role of the four variables.



Author: FoolishFlyFox
Link: https://www.jianshu.com/p/599550afd28d
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

Guess you like

Origin blog.csdn.net/u013590327/article/details/123045977