cmake sets the location of the generated files - Programmer Sought
This article mainly analyzes the difference between the four variables in cmake:
CMAKE_ARCHIVE_OUTPUT_DIRECTORY
: The folder location where the static library is stored by default;CMAKE_LIBRARY_OUTPUT_DIRECTORY
: The folder location where the dynamic library is stored by default;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;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: .a
static library files at the end, .so
dynamic 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.txt
The content of is:
cmake_minimum_required(VERSION 3.1)
project(fun)
add_library(fun fun.cpp)
fun/fun.cpp
The 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 generatedarchive
underlibfun.a
. -
If you
./CMakeLists.txt
comment out the third line in and execute itmake
, you can find that it is generatedpath
underlibfun.a
; -
If you
./CMakeLists.txt
comment out the 3rd and 6th lines in and execute itmake
, you can find that it is generatedfun
underlibfun.a
.
testCMAKE_LIBRARY_OUTPUT_DIRECTORY
- First
fun/CMakeLists.txt
change the third line of to:add_library(fun SHARED fun.cpp)
- Execute
make
, you canlibrary
seelibfun.so
the file in the directory (if it is a mac os system, you can see itlibfun.dylib
) - If you
./CMakeLists.txt
comment out the 4th line in and execute itmake
, you can find that the file is generatedpath
underlibfun.so
; - If you
./CMakeLists.txt
comment out the 4th and 6th lines in and execute itmake
, you can find that the file is generatedfun
underlibfun.so
;
testCMAKE_RUNTIME_OUTPUT_DIRECTORY
- First
fun/CMakeLists.txt
change the third line of to:add_executable(exe_file fun.cpp)
- Execute
make
, created the executableruntime
underexe_file
- Comment
./CMakeLists.txt
out line 5 in , executemake
, and create itexe_file
underfun
the 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.