一、创建一个cmake工程
Qt creator:file->New File or Porject...>>Non-Qt Project>>Plain C Project//或者//Plain C++ Project
CMakeLists.txt:
-
cmake_minimum_required(VERSION 3.7.1)
-
-
project(hello-world)
-
-
set(SOURCE_FILES main.c)
-
-
message(STATUS "This is BINARY dir " ${PROJECT_BINARY_DIR})
-
message(STATUS "This is SOURCE dir " ${PROJECT_SOURCE_DIR})
-
-
add_executable(hello-world ${SOURCE_FILES})
main.c:
-
#include <stdio.h>
-
-
int main() {
-
printf("Hello World.\n");
-
-
return 0;
-
}
可以再文件目录下创建build,然后再build目录下执行cmake ..
这样的话创建的cmake和make中间文件就在build目录下了。执行make
然后./hello-world
二、创建子目录文件
helloCMake/CMakeLists.txt:
-
cmake_minimum_required(VERSION 3.2)
-
-
project(helloCMake)
-
ADD_SUBDIRECTORY(src bin)
helloCMake/src/CMakeLists.txt:
-
cmake_minimum_required(VERSION 2.8.4)
-
-
add_executable(helloCMake main.cpp)
main.cpp:
-
#include <iostream>
-
-
using namespace std;
-
-
int main()
-
{
-
cout << "Hello World!" << endl;
-
return 0;
-
}
CMake的语法总结:
PROJECT 指令的语法是:
PROJECT(projectname [CXX] [C] [Java])
SET 指令的语法是:
SET(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]])
-
set(PX4_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
MESSAGE 指令的语法是:
MESSAGE([SEND_ERROR | STATUS | FATAL_ERROR] "message to display"
...)
-
message(FATAL_ERROR "Update CMake and try again" )
ADD_SUBDIRECTORY 指令
ADD_SUBDIRECTORY(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
-
ADD_SUBDIRECTORY(src bin)
阅读(1297) | 评论(0) | 转发(0) |