本来我用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
这两个给理解好。
来源:oschina
链接:https://my.oschina.net/u/4389765/blog/4237057