A beginner's tutorial of cmake under windows (hello world)

I want to learn cmake, based on inertial thinking, I want to run through a "hello world" example first, but there are a lot of online tutorials, and it takes a lot of time to find useful tutorials. Most of the tutorials do not describe the steps from the perspective of a new computer (computer after reinstalling the system).

In order to get a clean computer environment, I studied VMware virtual machines for a period of time and wrote several articles. Interested readers can move to read:

VMware virtual machine installation win10 system tutorial: VMware virtual machine installation win10 system tutorial_Xijin's no1's blog-CSDN Blog

After a few days of hard work, the whole process of cmake's "hello world" was run through from the perspective of a new computer (the computer after reinstalling the system, based on the VMware virtual machine), and the following process records were verified, and the result is feasible.

Some of the following content comes from other blogs. Due to the large number of data queries, it is impossible to correspond to the source, so the source is not listed here. I would like to express my gratitude to the authors of the quoted content.

 

1. Introduction to Cmake

CMake is a cross-platform installation (compilation) tool, which can describe the installation (compilation process) of all platforms with simple sentences. He can output various makefiles or project files. It's just that the configuration file of CMake is named CMakeLists.txt. Cmake does not directly construct the final software, but generates standard construction files (such as Unix Makefile or Windows Visual C++ projects/workspaces), and then uses them in the usual construction mode.

2. Download and install Cmake, configure environment variables, and check whether the installation is normal

1. Download Cmake

Download address: Download | CMake
Click the link to enter the download page of cmake official website, download the latest stable version (Latest Release)

What I downloaded here is cmake-3.26.4-windows-x86_64.msi (PS: download the corresponding version according to the number of digits of the personal computer windows system)

If you don't want to install, you can directly download the compressed package version cmake-3.26.4-windows-x86_64.zip.

2. Install cmake and configure environment variables

The installation of CMake is very simple. The compressed package version can be decompressed directly, and the installation version can also be installed by clicking Next. For ease of use, CMake can be configured into environment variables. Of course, this step can also be set during the installation process. Another thing to note is that in order to avoid unnecessary trouble, it is best not to include Chinese characters in the path. The following briefly describes the installation process.

1. Double-click the downloaded installation package to enter the installation interface, and click [Next].

2. After checking the consent license, click [Next].

3. You can choose to add the CMake path to the environment variable here, and create a desktop shortcut for the CMake GUI program. Then click [Next].

4. You can customize the installation directory here, and click [Next] after setting.

5. Start the installation, and click [Next] after the installation is complete.

6. After the installation is complete, click [Finish] to end the installation process.

3. Check whether the installation is normal

Once installed, CMake is ready to use. Enter "cmake --version" on the command line, and you can see the output as shown in the figure below.

 

3. Download and install MinGW, configure environment variables, and check whether the installation is normal

Cmake cannot complete the compilation of the entire project, cmake will output the makefile, we need to use this to generate the makefile for make compilation. Then you need to install MinGW in the windows environment.

1. Download MinGW

Go to the page: MinGW-w64 - for 32 and 64 bit Windows - Browse Files at SourceForge.net to download the offline installation package

I downloaded the tarball of x86_64-posix-sjlj.

2. Install MinGW

Decompress the downloaded x86_64-posix-sjlj compressed package to get mingw64.

As you can see, this is the version that can be used directly

3. Configure environment variables

Simply add the /bin path to the environment variable.

On the command line, the g++\gcc command works

Since we need to directly input make under the window, the makefile can be recognized and compiled automatically, so we can rename the mingw32-make.exe under the bin file of the MinGW installation path to make.exe.

4. Check whether the installation is normal

Open cmd and enter make -version. If the following information appears, the installation is successful.

4. Use Cmake to compile a simple small project

Create a new directory, and create two files CMakeLists.txt and hello_cmake.c in this directory. The contents of the two files are as follows:

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
project (hello_cmake)
add_executable(hello_cmake hello_cmake.c)

The first line of code specifies the minimum CMake version required to build the project;

The second line specifies the name of the project, and then the output exe executable file will have the same name as it;

The third line indicates that the project needs to build an exe executable file, and it is compiled from main.cpp;

hello_cmake.c

#include <stdio.h>
	 
int main(int argc, char *argv[])
{
	printf("Hello CMake!\n");
	getchar();
	return 0;
}

 

 

Open cmd to enter the current directory, and enter the following command line.

mkdir build
cd build
cmake -G   "MinGW Makefiles" ..
make
hello_cmake.exe

As shown below:

Guess you like

Origin blog.csdn.net/xijinno1/article/details/131199311