说明
系统:Windows
编译运行:cmake+MinGW-W64
运行文件
main.cpp主函数:
#include <iostream>
#include "function.h"
int main(){
printhello();
return 0;
}
printhello.cpp文件:
#include <iostream>
#include "function.h"
void printhello(){
int i;
std::cout<< "Hello World!" <<std::endl;
}
function.h头文件:
_Pragma("once")
#ifndef __FUNCTION_H_
#define __FUNCTION_H_
void printhello();
#endif
在程序目录下建立CMakeLists.txt文件:
cmake_minimum_required(VERSION 3.15) %cmake最小版本要求,根据需要确定
project(hello)
add_executable(hello main.cpp printhello.cpp)
在程序目录下打开终端,建立bulid目录:
mkdir bulid
进入bulid目录下:
cd bulid
运行命令:
cmake -G "MinGW Makefiles" ..
运行结果:
-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/Language_env/mingw64/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Desktop/Temprary_Files/temp/cpp_tmp/bulid
运行:
make
运行结果:
[ 33%] Building CXX object CMakeFiles/hello.dir/main.cpp.obj
[ 66%] Building CXX object CMakeFiles/hello.dir/printhello.cpp.obj
[100%] Linking CXX executable hello.exe
[100%] Built target hello
产生可执行文件hello.exe,直接运行:
./hello
运行结果:
Hello World!
本文介绍如何使用CMake配合MinGW-W64在Windows环境下搭建C/C++开发环境,并通过一个简单的Hello World示例程序演示整个编译流程。


被折叠的 条评论
为什么被折叠?



