【cmake】windows下的cmake 中make install安装到指定目录用法

问题:大部分教程是类uinx下,cmake文件INSTALL安装。但是因为默认路径前缀也是类Unix的。

INSTALL(FILES COPYRIGHT README DESTINATION share/doc/cmake/)

DESTINATION,安装文件的路径实际上是 /usr/local/share/doc/cmake/

但是windows下并没有这个路径。所以要修改默认的路径前缀。

解决:

常见步骤(linux下):

mkdir build && cd build
cmake ..
make
make install

1.最开始文件树

PS D:\Code\cmaketest\cmake_test03> tree /F
卷 Data 的文件夹 PATH 列表
卷序列号为 6E99-8D8D
D:.
│  CMakeLists.txt
│  COPYRIGHT
│  README
│  runhello.sh
│
├─build
├─doc
│      hello.txt
│
└─src
        CMakeLists.txt
        main.cpp

2.切换到cd .\build\

PS D:\Code\cmaketest\cmake_test03> cd .\build\

3.  cmake输入cmake -S ../ -B ./ -G "MinGW Makefiles"

PS D:\Code\cmaketest\cmake_test03\build> cmake -S ../ -B ./ -G "MinGW Makefiles"
-- The C compiler identification is GNU 11.2.0
-- The CXX compiler identification is GNU 11.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/mingw64/bin/gcc.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:/mingw64/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning (dev) in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.18)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: D:/Code/cmaketest/cmake_test03/build

4.cmake修改默认的安装路径前缀。关键一步

输入cmake -DCMAKE_INSTALL_PREFIX=D:\cmakeinstalltest\ -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -G"MinGW Makefiles" ../

参考:【CMake】cmake的install指令_Yngz_Miao的博客-CSDN博客_cmake install

PS D:\Code\cmaketest\cmake_test03\build> cmake -DCMAKE_INSTALL_PREFIX=D:\cmakeinstalltest\ -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -G"MinGW Makefiles" ../
CMake Warning (dev) in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.18)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: D:/Code/cmaketest/cmake_test03/build

5.make 输入make

PS D:\Code\cmaketest\cmake_test03\build> make
Scanning dependencies of target hello
[ 50%] Building CXX object bin/CMakeFiles/hello.dir/main.obj
[100%] Linking CXX executable hello.exe
[100%] Built target hello

6.安装 成功

PS D:\Code\cmaketest\cmake_test03\build> make install
[100%] Built target hello
Install the project...
-- Install configuration: ""
-- Installing: D:/cmakeinstalltest/share/doc/cmake/COPYRIGHT
-- Installing: D:/cmakeinstalltest/share/doc/cmake/README
-- Installing: D:/cmakeinstalltest/bin/runhello.sh
-- Up-to-date: D:/cmakeinstalltest/share/doc/cmake/
-- Installing: D:/cmakeinstalltest/share/doc/cmake//hello.txt

7.结果。这里安装了脚本,txt文件

PS D:\cmakeinstalltest> tree /F
卷 Data 的文件夹 PATH 列表
卷序列号为 6E99-8D8D
D:.
├─bin
│      runhello.sh
│
└─share
    └─doc
        └─cmake
                COPYRIGHT
                hello.txt
                README

附:工程下CMakeLists内容

PROJECT(HELLO)
ADD_SUBDIRECTORY(src bin)
#cmake -DCMAKE_INSTALL_PREFIX=D:\CloudMusic\ -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -G"MinGW Makefiles" ../
INSTALL(FILES COPYRIGHT README DESTINATION share/doc/cmake/)
INSTALL(PROGRAMS runhello.sh DESTINATION bin)
INSTALL(DIRECTORY doc/ DESTINATION share/doc/cmake/)

猜你喜欢

转载自blog.csdn.net/m0_57168310/article/details/126338451