学习cmake的运用,就是在clion常看到的CMakeLists.txt

看到一个很好的cmake博客

https://www.cnblogs.com/ningskyer/articles/7158948.html

 

学习到常用的一些小用法,从最简单开始了

在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下:

  1. 编写 CMake 配置文件 CMakeLists.txt 。
  2. 执行命令 cmake PATH 或者 ccmake PATH 生成 Makefile  ccmake 和 cmake 的区别在于前者提供了一个交互式的界面。。其中, PATH 是 CMakeLists.txt 所在的目录。
  3. 使用 make 命令进行编译。

编写 CMakeLists.txt

首先编写 CMakeLists.txt 文件,并保存在与 main.cc 源文件同个目录下:

 

1
2
3
4
5
6
7
8
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
 
# 项目信息
project (Demo1)
 
# 指定生成目标
add_executable(Demo main.cc)

 

CMakeLists.txt 的语法比较简单,由命令、注释和空格组成,其中命令是不区分大小写的。符号 # 后面的内容被认为是注释。命令由命令名称、小括号和参数组成,参数之间使用空格进行间隔。

对于上面的 CMakeLists.txt 文件,依次出现了几个命令:

  1. cmake_minimum_required:指定运行此配置文件所需的 CMake 的最低版本;
  2. project:参数值是 Demo1,该命令表示项目的名称是 Demo1 。
  3. add_executable: 将名为 main.cc 的源文件编译成一个名称为 Demo 的可执行文件。

编译项目

之后,在当前目录执行 cmake . ,得到 Makefile 后再使用 make 命令编译得到 Demo1 可执行文件。

 

     

project(json_example)                                                              #项目名称            
                                                                
add_library (cJson_lib  cJSON.h cJSON.c)                                                    #添加lib            
                                                                
set(main_files main.c)                                                              #设置搭配文件            
add_executable(main_example ${main_files})                                                    #在搭配文件上,新增程序            main_example
target_link_libraries(main_example cJson_lib m)                                                 #在程序上,链接lib            
                                                                
set(create_files create.c)                                                                      #设置搭配文件            
add_executable(create_example ${create_files})                                                          #在搭配文件上,新增程序            create_example
target_link_libraries(create_example cJson_lib m)                                                    #在程序上,链接lib            
                                                                
set(test_files test.c)                                                                      #设置搭配文件            
add_executable(test_example ${test_files})                                                           #在搭配文件上,新增程序            test_example
target_link_libraries(test_example cJson_lib m)                                                    #在程序上,链接lib   

 

#添加外部文件夹
if(NOT OUT-DIR)                
    set( OUT-DIR /home/test/CWork/OutDir/)    #定义OUT-DIR位置
endif()
if( IS_DIRECTORY ${OUT-DIR} )
    add_subdirectory( ${OUT-DIR} OutDir.out)
    message("Add OUT-DIR OK" )
else()
    message(FATAL_ERROR   "INVALID FOLDER 'OUT-DIR'=${OUT-DIR}" )
endif()

 

在Linux上,本来是用上只是用上了C语言的,后来想插入C++,就出现下面这个错误了

错误:CMake can not determine linker language for target:

解决:PROJECT("name" C)  改为  PROJECT("name" C CXX)

if(NOT MqttJson-DIR)
    set( MqttJson-DIR /home/test/CWork/MqttJson/)
endif()
if( IS_DIRECTORY ${MqttJson-DIR} )
    add_subdirectory( ${MqttJson-DIR} MqttJson.out)
    message("MqttJson-DIR OK" )
else()
    message(FATAL_ERROR   "INVALID FOLDER 'MqttJson-DIR'=${MqttJson-DIR}" )
endif()

posted @ 2018-02-01 09:53  橙子好甜  阅读(16481)  评论(0编辑  收藏  举报