如何使用CMAKE生成makefile文件
展开全部
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 <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
最后编译程序
在 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 <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
最后编译程序
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询