本來我用codebooks交叉編譯一些程序,后來我發現vscode,好用很多。
所以又開始准備折騰了。
首先先跟着官方教程來
1建立一個 tutorial.cxx
// A simple program that computes the square root of a number #include <stdio.h> #include <stdlib.h> #include <math.h> #include "TutorialConfig.h" int main (int argc, char *argv[]) { if (argc < 2) { fprintf(stdout,"%s Version %d.%d\n", argv[0], Tutorial_VERSION_MAJOR, Tutorial_VERSION_MINOR); fprintf(stdout,"Usage: %s number\n",argv[0]); return 1; } double inputValue = atof(argv[1]); double outputValue = sqrt(inputValue); fprintf(stdout,"The square root of %g is %g\n", inputValue, outputValue); return 0; }
2建立TutorialConfig.h.in
// the configured options and settings for Tutorial #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
3建立CMakeLists.txt 注意,這里官方說cmakelists不區分大小寫,但是我小寫了,在cmake的時候告訴我找不到文件。所以還是大寫。
# 這里是最基本的 cmake_minimum_required (VERSION 2.6) project (Tutorial) # 設置版本號 set (Tutorial_VERSION_MAJOR 1) set (Tutorial_VERSION_MINOR 0) # configure_file 可以復制一個文件到另一個地方,並對內容進行修改 # 這里吧PROJECT_SOURCE_DIR中的TutorialConfig.h.in復制到PROJECT_BINARY_DIR # 並變成TutorialConfig.h configure_file ( "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in" "${PROJECT_BINARY_DIR}/TutorialConfig.h" ) # 將項目二進制構建路徑添加到頭文件的搜索路徑中 # 然后我們可以在二進制構建路徑找到TutorialConfig.h # 這個二進制構建路徑也就是存放一些make過程中產生的文件以及可執行文件的路徑 # 我在build文件夾中cmake ../ build文件夾的路徑就是PROJECT_BINARY_DIR # PROJECT_SOURCE_DIR就是 build的上一級路徑 include_directories("${PROJECT_BINARY_DIR}") # 添加可執行文件(可執行文件名 [配置] 源文件) add_executable(Tutorial tutorial.cxx)
4建立build目錄,文件構成是這樣的
zqh@linux:~/cmake study/cmake1$ tree . ├── build ├── CMakeLists.txt ├── TutorialConfig.h.in └── tutorial.cxx
5 進入build文件夾執行
zqh@linux:~/cmake study/cmake1$ cd build/ zqh@linux:~/cmake study/cmake1/build$ cmake ../ -- The C compiler identification is GNU 5.4.0 -- The CXX compiler identification is GNU 5.4.0 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Detecting C compile features -- Detecting C compile features - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Detecting CXX compile features -- Detecting CXX compile features - done -- Configuring done -- Generating done -- Build files have been written to: /home/zqh/cmake study/cmake1/build
zqh@linux:~/cmake study/cmake1$ tree . ├── build │ ├── CMakeCache.txt │ ├── CMakeFiles │ │ ├── 3.5.1 │ │ │ ├── CMakeCCompiler.cmake │ │ │ ├── CMakeCXXCompiler.cmake │ │ │ ├── CMakeDetermineCompilerABI_C.bin │ │ │ ├── CMakeDetermineCompilerABI_CXX.bin │ │ │ ├── CMakeSystem.cmake │ │ │ ├── CompilerIdC │ │ │ │ ├── a.out │ │ │ │ └── CMakeCCompilerId.c │ │ │ └── CompilerIdCXX │ │ │ ├── a.out │ │ │ └── CMakeCXXCompilerId.cpp │ │ ├── cmake.check_cache │ │ ├── CMakeDirectoryInformation.cmake │ │ ├── CMakeOutput.log │ │ ├── CMakeTmp │ │ ├── feature_tests.bin │ │ ├── feature_tests.c │ │ ├── feature_tests.cxx │ │ ├── Makefile2 │ │ ├── Makefile.cmake │ │ ├── progress.marks │ │ ├── TargetDirectories.txt │ │ └── Tutorial.dir │ │ ├── build.make │ │ ├── cmake_clean.cmake │ │ ├── DependInfo.cmake │ │ ├── depend.make │ │ ├── flags.make │ │ ├── link.txt │ │ └── progress.make │ ├── cmake_install.cmake │ ├── Makefile │ └── TutorialConfig.h ├── CMakeLists.txt ├── TutorialConfig.h.in └── tutorial.cxx 7 directories, 34 files
這個時候我們就可以看到,在cmake ../ 的時候我們所在的文件夾是build ,所以build文件夾中就出現很多編譯過程中的文件,當然還有一個TutorialConfig.h 使用configure_file所復制過去的,現在makefile也生成了,可以進行編譯了。
zqh@linux:~/cmake study/cmake1/build$ make Scanning dependencies of target Tutorial [ 50%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cxx.o [100%] Linking CXX executable Tutorial [100%] Built target Tutorial zqh@linux:~/cmake study/cmake1/build$ ls CMakeCache.txt CMakeFiles cmake_install.cmake Makefile Tutorial TutorialConfig.h
這個時候就在build文件夾中產生了Tutorial。
第一個實驗,重要的就是吧
PROJECT_SOURCE_DIR #在這個實驗中 cmake ../ 所以這個目錄是 ~/cmake study/cmake1/
PROJECT_BINARY_DIR #在這個實驗中 cmake ../ 所以是 ~/cmake study/cmake1/build
這兩個給理解好。
