zoukankan      html  css  js  c++  java
  • cmake语法学习

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

  • 相关阅读:
    如何编写自己的Linux安全检查脚本?
    ubuntu桌面版打开终端Terminal的几种方法
    Docker 学习笔记(CentOS 7.1)
    发送验证码倒计时
    在Thinkphp中使用AJAX实现无刷新分页
    php加密解密
    浏览器用户用滤镜实现网页的翻转效果
    PHP定时执行任务的实现
    PHP GBK UTF8互转
    在php4下可用,简单的php数组转成json格式,
  • 原文地址:https://www.cnblogs.com/alexYuin/p/12773615.html
Copyright © 2011-2022 走看看