【CPP】CMake

Examples

main.cpp

#include 
#include "functions.h"
using namespace std;

int main()
{
    printhello();

    cout << "This is main:" << endl;
    cout << "The factorial of 5 is: " << factorial(5) << endl;
    return 0;
}

factorial.cpp

#include "functions.h"

int factorial(int n)
{
    if (n == 1)
        return 1;
    else
        return n * factorial(n - 1);
}

printhello.cpp

#include 
#include "functions.h"

using namespace std;

void printhello()
{
    int i;
    cout << "Hello World!" << endl;
}

functions.h

#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_
void printhello();
int factorial(int n);
#endif

CMake

What is CMake

CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.

CMake needs CMakeLists.txt to run properly.
A CMakeLists.txt consists of commands , comments and spaces.

  • The commands include command name, brackets and parameters ,
    the parameters are separated by spaces. Commands are not case sensitive.
  • Comments begins with ‘#’.

A single source file in a project

CMakeList.txt

cmake_minimum_required(VERSION 3.10)

project(hello)

add_executable(hello main.cpp factorial.cpp printhello.cpp)

【CPP】CMake_第1张图片

cmake .

【CPP】CMake_第2张图片【CPP】CMake_第3张图片
生成了cmake_install.cmake, CMakeCache.txt, Makefile 三个文件
以及一个文件夹 CMakeFiles

编译:

make

在这里插入图片描述

生成hello可执行文件

./hello

在这里插入图片描述

make clean

删掉了hello文件

但是还有其他文件没有删掉,需手动删除。

所以建议在CMake之前就新建一个build文件夹,在build文件夹里进行cmake

mkdir build
cd build
cmake ..

【CPP】CMake_第4张图片

附录

生成的Makefile是这样的:

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.27

# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target

# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:

#=============================================================================
# Special targets provided by cmake.

# Disable implicit rules so canonical targets will work.
.SUFFIXES:

# Disable VCS-based implicit rules.
% : %,v

# Disable VCS-based implicit rules.
% : RCS/%

# Disable VCS-based implicit rules.
% : RCS/%,v

# Disable VCS-based implicit rules.
% : SCCS/s.%

# Disable VCS-based implicit rules.
% : s.%

.SUFFIXES: .hpux_make_needs_suffix_list

# Command-line flag to silence nested $(MAKE).
$(VERBOSE)MAKESILENT = -s

#Suppress display of executed commands.
$(VERBOSE).SILENT:

# A target that is always out of date.
cmake_force:
.PHONY : cmake_force

#=============================================================================
# Set environment variables for the build.

# The shell in which to execute make rules.
SHELL = /bin/sh

# The CMake executable.
CMAKE_COMMAND = /opt/homebrew/Cellar/cmake/3.27.7/bin/cmake

# The command to remove a file.
RM = /opt/homebrew/Cellar/cmake/3.27.7/bin/cmake -E rm -f

# Escaping for special characters.
EQUALS = =

# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /Users/jiaweiluo/luojw/Shiqiyu/CPP/week05/examples/lab

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /Users/jiaweiluo/luojw/Shiqiyu/CPP/week05/examples/lab

#=============================================================================
# Targets provided globally by CMake.

# Special rule for the target edit_cache
edit_cache:
	@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake cache editor..."
	/opt/homebrew/Cellar/cmake/3.27.7/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache

# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast

# Special rule for the target rebuild_cache
rebuild_cache:
	@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..."
	/opt/homebrew/Cellar/cmake/3.27.7/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache

# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast

# The main all target
all: cmake_check_build_system
	$(CMAKE_COMMAND) -E cmake_progress_start /Users/jiaweiluo/luojw/Shiqiyu/CPP/week05/examples/lab/CMakeFiles /Users/jiaweiluo/luojw/Shiqiyu/CPP/week05/examples/lab//CMakeFiles/progress.marks
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
	$(CMAKE_COMMAND) -E cmake_progress_start /Users/jiaweiluo/luojw/Shiqiyu/CPP/week05/examples/lab/CMakeFiles 0
.PHONY : all

# The main clean target
clean:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean
.PHONY : clean

# The main clean target
clean/fast: clean
.PHONY : clean/fast

# Prepare targets for installation.
preinstall: all
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall

# Prepare targets for installation.
preinstall/fast:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast

# clear depends
depend:
	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend

#=============================================================================
# Target rules for targets named hello

# Build rule for target.
hello: cmake_check_build_system
	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 hello
.PHONY : hello

# fast build rule for target.
hello/fast:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/build
.PHONY : hello/fast

factorial.o: factorial.cpp.o
.PHONY : factorial.o

# target to build an object file
factorial.cpp.o:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/factorial.cpp.o
.PHONY : factorial.cpp.o

factorial.i: factorial.cpp.i
.PHONY : factorial.i

# target to preprocess a source file
factorial.cpp.i:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/factorial.cpp.i
.PHONY : factorial.cpp.i

factorial.s: factorial.cpp.s
.PHONY : factorial.s

# target to generate assembly for a file
factorial.cpp.s:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/factorial.cpp.s
.PHONY : factorial.cpp.s

main.o: main.cpp.o
.PHONY : main.o

# target to build an object file
main.cpp.o:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/main.cpp.o
.PHONY : main.cpp.o

main.i: main.cpp.i
.PHONY : main.i

# target to preprocess a source file
main.cpp.i:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/main.cpp.i
.PHONY : main.cpp.i

main.s: main.cpp.s
.PHONY : main.s

# target to generate assembly for a file
main.cpp.s:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/main.cpp.s
.PHONY : main.cpp.s

printhello.o: printhello.cpp.o
.PHONY : printhello.o

# target to build an object file
printhello.cpp.o:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/printhello.cpp.o
.PHONY : printhello.cpp.o

printhello.i: printhello.cpp.i
.PHONY : printhello.i

# target to preprocess a source file
printhello.cpp.i:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/printhello.cpp.i
.PHONY : printhello.cpp.i

printhello.s: printhello.cpp.s
.PHONY : printhello.s

# target to generate assembly for a file
printhello.cpp.s:
	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/printhello.cpp.s
.PHONY : printhello.cpp.s

# Help Target
help:
	@echo "The following are some of the valid targets for this Makefile:"
	@echo "... all (the default if no target is provided)"
	@echo "... clean"
	@echo "... depend"
	@echo "... edit_cache"
	@echo "... rebuild_cache"
	@echo "... hello"
	@echo "... factorial.o"
	@echo "... factorial.i"
	@echo "... factorial.s"
	@echo "... main.o"
	@echo "... main.i"
	@echo "... main.s"
	@echo "... printhello.o"
	@echo "... printhello.i"
	@echo "... printhello.s"
.PHONY : help



#=============================================================================
# Special targets to cleanup operation of make.

# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system


你可能感兴趣的:(CPP,c++)