zoukankan      html  css  js  c++  java
  • CMakeList_3

    hello.cpp

    #include <iostream>
    #include "hello.h"
    
    #ifdef USE_ADD
    #include "libtest.h"
    #endif
    
    int main(int argc, char** argv)
    {
    #ifdef USE_ADD
      std::cout << add(1, 2) << std::endl;
    #endif
      return 0;
    }
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.10)
    
    #直接使用project定义target或者指定下版本,再通过config_file设定版本宏配置自动写入指定头文件
    #project(helloworld)
    project(helloworld VERSION 1.0)
    
    #option放在configure_file前面否则默认值不生效
    option(USE_ADD "Use ADD provided math implementation" ON)
    
    #cmake执行时将hello.h.in中定义的@${target}_VERSION_MAJOR@和@${target}_VERSION_MINOR@替换为VERSION的1和0
    #并重新重定向到${PROJECT_BINARY_DIR}下的新文件hello.h
    configure_file(hello.h.in hello.h)
    
    #指定CXX的版本
    set(CMAKE_CXX_STANDARD 14)
    set(CMAKE_CXX_STANDARD_REQUIRED True)
    
    #可选宏需要通过配置文件hello.h.in导入
    #配置文件额外添加 #cmakedefine USE_ADD 从CMake导入到hello.h,在工程中可以使用
    if(USE_ADD)
        add_subdirectory(lib)
        list(APPEND EXTRA_LIBS add)
        list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/lib")
    endif()
    
    
    add_executable(helloworld hello.cpp)
    
    #放在add_executable后面,否则找不到target
    #${PROJECT_BINARY_DIR}为CMake的环境变量
    target_include_directories(helloworld PUBLIC
                               "${PROJECT_BINARY_DIR}"
                               ${EXTRA_INCLUDES}
                               )
    target_link_libraries(helloworld PUBLIC ${EXTRA_LIBS})
    

    hello.h.in

    #define HELLO_VERSION_MAJOR @helloworld_VERSION_MAJOR@
    #define HELLO_VERSION_MINOR @helloworld_VERSION_MINOR@
    

    liblibtest.h

    int add(int a, int b);
    

    liblibtest.cpp

    #include "libtest.h"
    int add(int a, int b)
    {
    	return a+b;
    }
    

    libCMakeLists.txt

    cmake_minimum_required(VERSION 3.10)
    
    project(helloworld VERSION 1.0)
    
    set(CMAKE_CXX_STANDARD 14)
    set(CMAKE_CXX_STANDARD_REQUIRED True)
    
    add_library(add libtest.cpp)
    
    target_include_directories(add PUBLIC
                               "."
                               )
    
  • 相关阅读:
    hibernate4.3.10使用注解映射方式样例
    eclipse ssh连接sqlserver express
    window2012 64bit 安装sqlserver2012 64bit调用excel的驱动安装
    SharpZipLib要支持unicode的文件名称
    搜索数据库中的内容
    AIX 添加开机启动项
    oracle 分区表和分区索引
    oracle 临时表学习
    oracle sys sysman system 介绍
    oracle to_date函数(转载)
  • 原文地址:https://www.cnblogs.com/kuikuitage/p/14387408.html
Copyright © 2011-2022 走看看