Questions in category: CMake (CMake)
软件 >> CMake

1. cmake 教程1

Posted by haifeng on 2021-12-22 10:45:06 last update 2023-03-04 09:37:57 | Answers (0) | 收藏


我们以 MySQL 8.0.23 源代码中 client 文件夹的 echo.cc 为例. echo.cc 的内容如下:

#include

int main(int argc, char **argv) {
  int i;
  for (i = 1; i < argc; i++) {
    fprintf(stdout, "%s", argv[i]);
    if (i < argc - 1) fprintf(stdout, " ");
  }
  fprintf(stdout, "\n");
  return 0;
}

 

将 echo.cc 放到某个目录, 比如 client2. 然后在此目录下新建 CMakeLists.txt 文件.

用文本编辑器打开 CMakeLists.txt, 写入下面几行:

cmake_minimum_required(VERSION 3.10)
project(Echo)
add_executable(echo echo.cc)

然后在该目录(client2)下新建文件夹 build, 并执行 cmake命令. 具体如下:

mkdir build
cd build
cmake ../

 

如果你电脑上安装了 Visual Studio 2019 或其他版本的 VS. 那么 cmake 会生成一些工程文件.

这些都是在 build 目录里的. 双击 Echo.sln , 就可以用 VS2019 编译了.

当然, 可通过 cmake 命令直接编译生成 echo.exe 

cmake --build .

注意有一个点“ . ”, 表示当前目录。


注: 该项目位于

D:\work\cs\cmake\echo

2. cmake 教程

Posted by haifeng on 2021-04-28 14:30:39 last update 2021-04-28 15:25:48 | Answers (0) | 收藏


 

CMake Tutorial — CMake 3.20.1 Documentation

 

 

D:\work\cs\c\cmakeProjects\sqrt\build>where cmake
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe

D:\work\cs\c\cmakeProjects\sqrt\build>cmake ..
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19042.
-- The C compiler identification is MSVC 19.28.29913.0
-- The CXX compiler identification is MSVC 19.28.29913.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/work/cs/c/cmakeProjects/sqrt/build


D:\work\cs\c\cmakeProjects\sqrt\build>dir
 驱动器 D 中的卷是 计算机

 D:\work\cs\c\cmakeProjects\sqrt\build 的目录

2021-04-28  15:03   

          .
2021-04-28  15:03   

          ..
2021-04-28  15:03            72,728 ALL_BUILD.vcxproj
2021-04-28  14:01               286 ALL_BUILD.vcxproj.filters
2021-04-28  14:01             3,126 CalculateSqrt.sln
2021-04-28  14:01            26,587 CalculateSqrt.vcxproj
2021-04-28  14:01               576 CalculateSqrt.vcxproj.filters
2021-04-28  15:03            14,559 CMakeCache.txt
2021-04-28  15:03   

          CMakeFiles
2021-04-28  14:01             1,436 cmake_install.cmake
2021-04-28  15:03             3,116 Tutorial.sln
2021-04-28  15:03            82,169 Tutorial.vcxproj
2021-04-28  15:03               579 Tutorial.vcxproj.filters
2021-04-28  15:03            72,392 ZERO_CHECK.vcxproj
2021-04-28  14:01               529 ZERO_CHECK.vcxproj.filters

 

 


 

D:\work\cs\c\cmakeProjects\sqrt\build>cmake --build .
用于 .NET Framework 的 Microsoft (R) 生成引擎版本 16.9.0+5e4b48a27
版权所有(C) Microsoft Corporation。保留所有权利。

  Checking Build System
  Building Custom Rule D:/work/cs/c/cmakeProjects/sqrt/CMakeLists.txt
  tutorial.cxx
  Tutorial.vcxproj -> D:\work\cs\c\cmakeProjects\sqrt\build\Debug\Tutorial.exe
  Building Custom Rule D:/work/cs/c/cmakeProjects/sqrt/CMakeLists.txt