当前位置:首页 > 知识百科 > 正文内容

linux makefile_linux makefile文件编写

fpodo2个月前 (08-14)知识百科5

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

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

  编写 CmakeLists.txt。

  执行命令 “cmake PATH” 或者 “ccmake PATH” 生成 Makefile ( PATH 是 CMakeLists.txt 所在的目录 )。

  使用 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

  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

  最后编译程序

相关文章

开不了机的电脑怎么处理(开不了机的电脑怎么处理掉)

可以按照以下的步骤排查解决:  1、拆开主机机箱  主机机箱的打开的方法不一样,所以根据自己的情况拆卸,这里就不说废话了。  2、找到主板上的电池纽扣  在主板上有一个很小的电池,把他取下来。在电池的...

欧司朗夜行者2代(欧司朗夜行者四代激光版)

我换过欧司朗夜行者三代,H4的,光比较白,比原车的普通卤素灯泡亮一点,但是过了不到一年近光灯丝就烧断了欧司朗的夜行者(Night Walker)和极光者(Aurora)是两种不同的产品系列,主要用于户...

局域网连接打印机(局域网连接打印机怎么连接)

局域网连接打印机(局域网连接打印机怎么连接)

1.打开电脑左下角【开始】2.选择【设备和打印机】3.在弹出的界面选中【添加打印机】4.在打印机向导里面选中第二项【网络打印机或连接到其他计算机的打印机】5.在弹出的界面选中【浏览打印机】6.搜索过后...

小米备份bak如何恢复

设置→更多设置→备份和重置→自动备份→关闭。就不会产生新的备份文件了。手机里面原有已经备份的文件可以手动删除,不影响使用。...

手机照片恢复大师免费版(免费恢复照片不收费)

先直接把照片删了,然后用电脑把磁盘整理一下即可,电脑自带的磁盘整理程序就行,一小会就完成了,或是直接往里面下东西,直到装满,然后照片就彻底删除了。其实照片无法彻底删除的原因是:当你只把文件删除后,只是...

怎么一键还原(怎么一键还原电脑系统还原)

怎么一键还原(怎么一键还原电脑系统还原)

1.WindowsXP系统:开机按 F11键,进入一键还原页面。如未安装还原软件,开机系统选项页—按F8进入高级选项,选择一键还原。2.Windows7系统:开机按F11,选择高级—将系统恢复到过去某...