table of Contents
1. Install the software
1. Install Vscode to
support cross-platform, Microsoft products.
Download address: https://code.visualstudio.com/
Required plugins for vscode
C/C++
C++ Intellisense
CMake
CMake tools
CMake Tools Helper
Install and check the options you need, and you can install it by fool.
2. Install the CMake
download link: https://cmake.org/download/
Try to choose the Latest Release version, which is more stable.
As shown in the red box in the figure, download the installation file with the suffix .msi, and then install it directly. Generally choose the default installation path for installation.
Verify after installation:
cmake -version
3. Install MinGW
We can install gcc/g++ directly on the ubuntu system, but cannot directly install g++ on windows. At this time, we need to use MinGW, which is developed from Cygwin (version 1.3.3). Most of the languages supported by GCC are also supported in MinGW, including C, C++, Objective-C, Fortran and Ada. For languages other than C, MinGW uses standard GNU runtime libraries, such as C++ uses GNU libstdc++.
Download link: https://sourceforge.net/projects/mingw-w64/
Configure environment variables after installation:
I installed it by default in: C:\mingw64
Verification:
gcc --version
2. Configure Vscode
1. Use the shortcut key combination [Ctrl+Shift+p] to
select:
CMake:Select a Kit
GCC 6.3.0 XXXX
3. Create a project after the configuration is complete:
1.main.c
#include<stdio.h>
int main() {
printf("hello\n");
}
2.CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(vscode_cmake_Test)
aux_source_directory(. DIR_TOOT_SRCS)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
add_executable(${
PROJECT_NAME} ${
DIR_TOOT_SRCS})
3. Create the build directory.
When the environment configuration is OK, every time you modify the CMakeLists ctrl+s, it will automatically compile, and the compiled files will be placed in the build.
Automatic compilation
每次修改.CMakeLists.txt 程序自动编译执行的命令:
C:\cmake-3.15.0-rc1-win64-x64\bin\cmake.EXE --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=C:\mingw64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\mingw64\bin\g++.exe -Hc:/Users/Administrator/Desktop/cpp/C/CMake/1 -Bc:/Users/Administrator/Desktop/cpp/C/CMake/1/build -G "MinGW Makefiles"
进入build 目录手动执行:
mingw32-make
Manual compilation:
mkdir build
cd build
cmake -G "MinGW Makefiles" ..
mingw32-make