菜鸟笔记
提升您的技术认知

cmake install方法-ag真人游戏

阅读 : 259

├── 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 
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
网站地图