【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/)

 

 

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pull_future

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值