美文网首页
CMake学习记录

CMake学习记录

作者: 欧德朗 | 来源:发表于2018-10-12 15:54 被阅读0次

    2018-10-12

    CMake是什么?

    个人理解:
    自动判断文件依赖关系生成makefiel,cm文件一般命名为CMakeLists.txt,编写需要手动编写或者使用脚本进行半自动生成,使用时直接在当前目录cmake .或者mkdir build后再build目录下cmake ..,推荐使用第二种方式这样子cmake的生成的中间文件可以很方便的被清理。

    实际应用:

    单一文件情况cm文件编写:

    创建cpp文件,subl hello.cpp

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "hello world!"<<endl;
        return 0;
    }
    

    创建CMakeLists.txt

    add_executable(hello hello.cpp)
    

    只需要这一句,单一文件不需指定版本,亦可直接使用,当前路径下的文件情况:

    .
    ├── CMakeLists.txt
    └── hello.cpp
    

    像上面说的一样可以直接cmak .,然后make
    或者

    mkdir build
    cd build
    cmake ..
    make
    ./hello
    >hello world!
    

    多目录情况下cm文件编写和文件布局

    先看整体文件布局:

    .
    ├── build
    ├── CMakeLists.txt
    └── src
        ├── CMakeLists.txt
        ├── main.cpp
        ├── hello
        │   ├── CMakeLists.txt
        │   ├── hello.cpp
        │   └── hello.h
        └── world
            ├── CMakeLists.txt
            ├── world.cpp
            └── world.h
    

    每个目录下有自己的cm文件,然后通过上层添加资源文件夹的方式进行调用
    之前做二进制对应的受mk文件是直接通过上层mk直接include其他mk的方式进行调用编译
    hello.h add world.h中是hello.cpp 和world.cpp函数声明。cpp中是实现
    1.先创建工程目录

    mkdir build
    mkdir src
    cd src 
    mkdir hello
    mkdir world
    

    2.先coding这个hello模块

    //---------hello.h-------------
    #include <iostream>
    using namespace std;
    void hello_printf();
    
    //-----------hello.cpp-------
    #include "hello.h"
    void hello_printf()
    {
        cout << "hello"<< endl;
    }
    

    hello路径下的cm文件

    aux_source_directory(. src)
    add_library(hello ${src})
    

    aux_source_directory 将当前目录中的源文件名称赋值给变量 DIR_SRCS 。 CMake 手册中对命令 aux_source_directory 的描述如下:

    aux_source_directory(<dir> <variable>)

    add_library的意思是生成库文件,不指定动态库或者静态库的时候默认生成静态库

    待更------------------

    相关文章

      网友评论

          本文标题:CMake学习记录

          本文链接:https://www.haomeiwen.com/subject/fyxgaftx.html