[cmake] Use cmake to generate executable file exe under Windows

1. Create a new folder and main.cpp file

#include <iostream>
using namespace std;
int main() {
	std::cout << "hello world" << std::endl;
	system("pause");
	return 0;

}

2. Write the CMakeLists.txt file

cd7f8432c0c5421a889afbe223566f57.png

PROJECT (HELLO)
SET(SRC_LIST main.cpp)
MESSAGE(STATUS "THIS IS BINARY DIR " ${HELLO_BINARY_DIR})
MESSAGE(STATUS "THIS IS SOURCE DIR " ${HELLO_SOURCE_DIR})
ADD_EXECUTABLE(hello ${SRC_LIST})

 The meaning of the content can be found in other articles.

3. Then according to the tutorial, you should enter cmake . to generate the makefile.

3.1 The compiler defaults to vs under windows, which will generate a lot of files.

Problem: cmake does not generate makefiles under windows.

089f30cb67df46db8ea93b06b6848bd2.png

 

d92cae4118a54190aca9257eddbbaafd.png

3.2 No longer continue here, delete, and then only mian.cpp and CMakeLists.txt are left. Re-enter cmake -S ./ -B ./ -G "MinGW Makefiles". (Of course, you need to have MinGW on your computer and configure environment variables here)

d9beea9f85db4bd190c9f20c342cb06a.png

You can see that the makefile is generated.

223b0bc30e9d416f9ec9e095229a98c4.png 3.3 Then enter make in cmd.

The problem  occurs , 'make' is not recognized as an internal or external command, operable program

Solution: Find mingw32-make.exe. You can make a copy and rename it to make.exe.

983d91d470b1460bb10f4e1457e38447.png

 continue. make in cmd.

82cff61034e14af5a93f0ae2f6b57439.png

You can see that hello.exe has been generated

93a04839704c457684cbe2064bd54c92.png

3.4 run hello.exe 

 8d8a9e739a8a4bf38d4c871e9211fdb6.png

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_57168310/article/details/126334044