CMake系列(四) CMake使用install方法的使用

CMake系列(四) CMake使用install方法的使用



目录结构

├── cmake-examples.conf
├── CMakeLists.txt
├── include
│ └── Hello.h
└── src
├── Hello.c
└── main.c

源文件

main.c

#include "Hello.h"

int main(int argc, char *argv[])
{
    Hello_print();
    return 0;
}

Hello.c

#include "Hello.h"

void Hello_print(void)
{
    printf("install hello: cmake\r\n");
}

头文件

Hello.h

#ifndef __HELLO_H__
#define __HELLO_H__
#include <stdio.h>

void Hello_print(void);

#endif

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(cmake_examples_install)

############################################################
# Create a library
############################################################

#Generate the shared library from the library sources
add_library(cmake_examples_inst SHARED
    src/Hello.c
)

target_include_directories(cmake_examples_inst
    PUBLIC 
        ${PROJECT_SOURCE_DIR}/include
)

############################################################
# Create an executable
############################################################

# Add an executable with the above sources
add_executable(cmake_examples_inst_bin
    src/main.c
)

# link the new hello_library target with the hello_binary target
target_link_libraries( cmake_examples_inst_bin
    PRIVATE 
        cmake_examples_inst
)

############################################################
# Install
############################################################

# Binaries
install(TARGETS cmake_examples_inst_bin
    DESTINATION bin)

# Library
# Note: may not work on windows
install(TARGETS cmake_examples_inst
    LIBRARY DESTINATION lib)

# Header files
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ 
    DESTINATION include)

# Config
install(FILES cmake-examples.conf
    DESTINATION etc)

)

编译

	$  mkdir build
	$  cd build/
	$  cmake ..
	$  make
	$  make install

测试

	$  ./hello_cmake
	install hello: cmake
	
	$ cat install_manifest.txt
	/usr/local/bin/cmake_examples_inst_bin
	/usr/local/lib/libcmake_examples_inst.so
	/usr/local/etc/cmake-examples.conf
	
	$  ls /usr/local/bin/
	cmake_examples_inst_bin
	$ ls /usr/local/lib
	libcmake_examples_inst.so
	$ ls /usr/local/etc/
	cmake-examples.conf
	$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib cmake_examples_inst_bin
	install hello: cmake

说明

这样install的目录为 /usr/local 如果想改变install的目录有三种方式。
方式1:
在CMakeLists.txt 加入

set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE STRING "The path to use for make install" FORCE)

方式2:
在cmake构建工程的时候加入参数

cmake -DCMAKE_INSTALL_PREFIX=./install ..

./install可以是任何目录,别忘了最后的两个点。

方式3:
.make install 时指定安装路径

make DESTDIR=./install
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

胖茄子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值