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

  • 相关阅读:
    HTTP协议
    浏览器兼容性问题
    面试笔试重点总结:操作系统、计算机网络、设计模式
    笔试、面试重点总结:算法基础、数据结构
    笔试、面试重点总结:WIN32、MFC与Linux
    《Java程序员面试笔试宝典》终于在万众期待中出版啦~
    《Java程序员面试笔试宝典》之Static关键字有哪些作用
    《Java程序员面试笔试宝典》之字符串创建与存储的机制是什么
    《Java程序员面试笔试宝典》之为什么需要public static void main(String[] args)这个方法
    《Java程序员面试笔试宝典》之Java变量命名有哪些规则
  • 原文地址:https://www.cnblogs.com/alexYuin/p/12773615.html
Copyright © 2011-2022 走看看