使用Cmake生成makefile


CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性。只是 CMake 的组态档取名为 CmakeLists.txt。Cmake 并不直接建构出最终的软件,而是产生标准的建构档(如 linux 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),然后再依一般的建构方式使用。

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

  1. 编写 CmakeLists.txt
  2. 执行命令 cmake PATH 或者 ccmake PATH 生成 Makefile ( PATH  CMakeLists.txt 所在的目录 )
  3. 使用 make 命令进行编译
工程实例:
一. 编写各层CMakeLists.txt
主目录的主程序main.cpp
#include "hello.h"
extern Hello hello;
int main()
{
  hello.Print();
  return 0;
}

主目录的CMakeLists.txt
# to the root binary directory of the project as ${MAIN_BINARY_DIR}.
project (MAIN)

#version support
cmake_minimum_required(VERSION 2.8)

# Recurse into the "Hello" and "Demo" subdirectories.  This does not actually
# cause another cmake executable to run.  The same process will walk through
# the project's entire directory structure.
add_subdirectory (Hello)
add_subdirectory (Demo)

# Make sure the compiler can find include files from our Hello library.
include_directories (${MAIN_SOURCE_DIR}/Hello)

# Make sure the linker can find the Hello Demo library once it is built.
link_directories (${HELLO_BINARY_DIR}/Hello)
link_directories (${HELLO_BINARY_DIR}/Demo)

#define the source coedes of current directory as DIR_SRCS
AUX_SOURCE_DIRECTORY(. DIR_SRCS)

# Add executable called "MAIN" that is built from the source files
add_executable (Main ${DIR_SRCS})

# Link the executable to the Hello Demo library.
target_link_libraries (Main Hello Demo)

定义项目名project(MAIN),使得当前目录可以用 ${MAIN_SOURCE_DIR},由于有2个子目录,所以需要 add_subdirectory它们。由于主程序会使用到其他库,因而也需要指定连接库所在目录。
主目录下的作用是利用 add_executable将当前目录下的源文件编译成Main程序,然后通过 target_link_libraries链接Hello和Demo库。由于主程序文件使用了hello.h文件, 所以要 include_directories 该目录。
---------------------------------------------------------------------------------------------------
子目录Demo的子程序demo.c
#include "hello.h"
Hello hello;

子目录Demo的CMakeLists.txt
# Make sure the compiler can find include files from our Hello library.
include_directories (${MAIN_SOURCE_DIR}/Hello)

#define the source coedes of current directory as DIR_DEMO_SRCS
AUX_SOURCE_DIRECTORY(. DIR_DEMO_SRCS)

# Add library called "Demo" that is built from the source files
add_library  (Demo ${DIR_DEMO_SRCS})

Demo目录下的 CMakeLists主要作用是利用add_library将当前目录源码编译成Demo库,由于该库使用到hello.h文件,所以要 include_directories 该目录。
---------------------------------------------------------------------------------------------------
子目录Hello的子程序hello.h
#ifndef _hello_h
#define _hello_h
class Hello
{
public:
  void Print();
};
#endif

子目录Hello的子程序hello.c
#include "hello.h"
#include <stdio.h>
void Hello::Print()
{
  printf("Hello, World!\n");
}

子目录Hello的CMakeLists.txt
#define the source coedes of current directory as DIR_HELLO_SRCS
AUX_SOURCE_DIRECTORY(. DIR_HELLO_SRCS)

# Add library called "hello" that is built from the source files
add_library (Hello ${DIR_HELLO_SRCS})

Hello目录下的CMakeLists主要作用是利用add_library将当前目录源码编译成Hello库。
---------------------------------------------------------------------------------------------------
二. 执行cmake命令
至此我们完成了项目中所有 CMakeLists.txt 文件的编写,进入目录 step2 中依次执行命令 
#cmake  .  
默认当前目录,生产makefile
#make 
最后编译程序

转载于:https://www.cnblogs.com/nafio/p/9137649.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CMake 生成 Makefile 的过程分为两个步骤: 1. 编写 CMakeLists.txt 文件 CMakeLists.txt 文件是 CMake 的配置文件,用于描述工程的源文件、编译选项、链接选项等信息。你需要在 CMakeLists.txt 文件中定义工程名称、源文件、头文件路径、库文件路径等信息。 以下是一个简单的 CMakeLists.txt 文件示例: ``` cmake_minimum_required(VERSION 3.10) project(MyProject) set(CMAKE_CXX_STANDARD 11) include_directories(include) add_executable(MyProgram src/main.cpp src/func.cpp) target_link_libraries(MyProgram m) ``` 在这个 CMakeLists.txt 文件中,首先指定了 CMake 的最低版本要求。然后定义了工程名称为 MyProject,并设置了 C++ 标准为 C++11。接着指定了头文件路径为 include 目录。然后定义了 MyProgram 目标,该目标依赖于 src/main.cpp 和 src/func.cpp 两个源文件,用于生成可执行文件 MyProgram。最后指定了链接库为数学库 libm。 2. 生成 Makefile 文件 在编写好 CMakeLists.txt 文件后,你可以使用 cmake 命令来生成 Makefile 文件。在命令行中进入工程目录,执行以下命令: ``` cmake -S . -B build ``` 其中,-S 参数指定了 CMakeLists.txt 文件所在的目录,-B 参数指定了构建目录。上述命令表示将 CMakeLists.txt 文件所在的当前目录作为源目录,将 build 目录作为构建目录。 执行完上述命令后,CMake生成 Makefile 文件并保存到 build 目录中。你可以进入 build 目录,执行 make 命令来编译工程。 ``` cd build make ``` 这样,就可以使用 CMake 生成 Makefile 文件,并编译生成工程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值