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