cmake语法学习 - 01 Basic - E- installing

Posted alexyuin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cmake语法学习 - 01 Basic - E- installing相关的知识,希望对你有一定的参考价值。

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.cpp
)

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.cpp
)

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

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

message("CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")


# 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)

 

 

Before diving into the code, please make sure you really uderstand what is intalling and why to do that.

Install a libiary (like OpenCV) to system  ( some specific paths ), and after that, you can import them to you project by "find_packge()" in CMake.

 

*

install (TARGETS cmake_examples_inst_bin
    DESTINATION bin)

-TARGETS is followed by the file to stall

-DESTINATION is followed by the folder.

That means, the final output path will be "${CMAKE_BINARY_DIR}/bin"

 

For beginners, the most confused thing will be where is the path to install, when did you set that, and where in the CMakeList.txt.

The answer is, there are some default paths in CMake marked by some kept variables.

So the path settings are quite ambigous. (At least it is my opinion.)

For a better understanding, I recommend you to message() some path-variables in CMake and remember them. We have limited options in this points. Please check the website of CMake.

 

 

*

target_include_directories(cmake_examples_inst

    PUBLIC 
        ${PROJECT_SOURCE_DIR}/include
)

- After setting the include paths, you can open the head files #include in you file. But of course, you need to have an IDE to support importing project from CMkeList.txt, like CLion. But it is no free which only offers me 30 days to use.

Waitting for recommendations :)

 

以上是关于cmake语法学习 - 01 Basic - E- installing的主要内容,如果未能解决你的问题,请参考以下文章

cmake语法学习 - 01 Basic - D - shared - library

cmake语法学习 - 01 Basic - B Hello headers

01-cmake语法-基本

cmake basic

[cmake] Basic Tutorial

cmake 学习笔记