• CMake使用


    CMake使用

    基础CMakeList.txt

    $ ls
    CMakeLists.txt  main.c
    $ cat CMakeLists.txt
    ### cmake的最低版本
    cmake_minimum_required(VERSION 2.8)
    ### 工程名
    project(helloworld)
    
    ### 设置编译参数
    set(CMAKE_CXX_FLAGS  "-Wall") set(CMAKE_CXX_FLAGS_DEBUG  "-g3") set(CMAKE_CXX_FLAGS_RELEASE  "-O2")
    
    ### 可执行文件名、源文件列表
    add_executable(helloworld main.c)
    

    使用pkg-config的CMakeList.txt

    $ ls
    bird.c  bird.h  CMakeLists.txt  main.c
    
    $ cat CMakeLists.txt
    ### cmake的最低版本
    cmake_minimum_required(VERSION 2.8)
    ###工程名
    project(glib)
    ### 遍历当前目录及子目录获取所有源文件
    file(GLOB SRC "${CMAKE_CURRENT_SOURCE_DIR}/*.c")
    ### 设置源文件列表
    add_executable(glib ${SRC})
    
    ### dependencies
    INCLUDE(FindPkgConfig)
    ### 使用pkg-config寻找glib-2.0和gobject-2.0库的include目录以及库
    pkg_check_modules(GLIB REQUIRED glib-2.0)
    pkg_check_modules(GOBJECT REQUIRED gobject-2.0)
    include_directories(${GLIB_INCLUDE_DIRS} ${GOBJECT_INCLUDE_DIRS})
    link_directories(${GLIB_LIBRARY_DIRS} ${GOBJECT_LIBRARY_DIRS})
    ### 设置链接库
    target_link_libraries(glib ${GLIB_LIBRARIES} ${GOBJECT_LIBRARIES})
    

    动态库CMakeList.txt

    $ ls
    CMakeLists.txt  greet.cpp  greet_wrapper.cpp
    $ cat greet.cpp
    char const* greet()
    {
        return "hello world";
    }
    
    $ cat greet_wrapper.cpp
    #include <boost/python.hpp>
    #include "greet.cpp"
    
    BOOST_PYTHON_MODULE(hello_ext)
    {
        using namespace boost::python;
        def("greet", greet);
    }
    
    $ cat CMakeLists.txt
    cmake_minimum_required(VERSION 2.8)
    project(greet)
    
    set(greetSRC greet_wrapper.cpp)
    ### 生成动态库
    add_library(hello_ext SHARED ${greetSRC})
    ### 去掉库名前缀lib
    set_target_properties(hello_ext PROPERTIES PREFIX "")
    
    #dependencies
    INCLUDE(FindPkgConfig)
    pkg_check_modules(PYTHON REQUIRED python)
    include_directories(/usr/include ${PYTHON_INCLUDE_DIRS})
    target_link_libraries(hello_ext boost_python)
    
    

    执行shell命令

    $ cat ../CMakeLists.txt
    ### cmake的最低版本
    cmake_minimum_required(VERSION 2.8)
    
    ### 新增伪命令'cmd',即新增'make cmd'命令,命令的结果就是输出"exec"字符串,如果添加了'ALL',则会将此命令添加到默认的构建目标,即'make'的时候会自动被执行
    add_custom_target(cmd ALL
            COMMAND echo "exec"
    )
    $ cmake .
    -- The C compiler identification is GNU 5.3.1
    -- The CXX compiler identification is GNU 5.3.1
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/fedora/experiment/test
    $ make
    Scanning dependencies of target cmd
    exec
    Built target cmd
    
  • 相关阅读:
    setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key
    Kinect 骨骼追踪数据的处理方法
    了解与建设有中国特色的Android M&N(Android6.0和7.0新特性分析)
    【计算机视觉】深度相机(一)--TOF总结
    A million requests per second with Python
    buf.fill()
    buf.slice()
    buf.toJSON()
    buf.toString()
    Buffer.compare()
  • 原文地址:https://www.cnblogs.com/silvermagic/p/7665940.html
Copyright © 2020-2023  润新知