[Windows] Configure C/C++ vs code + CMake + external library environment

For friends who are used to the Ubuntu environment, it is very troublesome to configure the VS CODE and C/C++ environment under windows. Don't worry, let me take you five minutes to set it up.

Environment installation

First, we need to download and install 3 things, vscode, mingw64 (c/c++ compiler), cmake (compilation manager).
Paste the link:
CMAKE download
MINGW64 download
VSCODE download
CMAKE and VSCODE After downloading, you can directly open the installation in the next step. Note, if there is an option to add environment variables, remember to check it. MINGW64 is downloaded as a compressed package, and we put it in the C drive directory.
insert image description here

Configure environment variables

After installation, we also need to configure environment variables so that the windows system can find the three software we installed.
Just do the following, remember to change the path to your own:
insert image description here

VSCODE configuration

Just install the two in the red box in the plug-in store. This is the c++ and cmake collection package. The code Runner is also recommended to install.
insert image description here
Next, create a folder, open it with vscode, and create a new main.cpp
insert image description here

insert image description here

Then follow this step to generate configuration files and folders. Note that this is a hidden folder. You need to enable the display of hidden files to see it. insert image description here
insert image description here
insert image description here
insert image description here
Finally, create these json files and paste the following code into them
insert image description here
c_cpp_properties.json

{
    
    
    "configurations": [

        {
    
    
      
         "name": "win64",
      
         "includePath": [
      
          "${workspaceFolder}/**",
      
          "${workspaceRoot}/include/"
      
         ],
      
         "defines": [
      
          "_DEBUG",
      
          "UNICODE",
      
          "_UNICODE"
      
         ],
      
         "windowsSdkVersion": "8.1",
      
         "compilerPath": "C:/mingw64/bin/g++.exe",
      
         "cStandard": "c11",
      
         "cppStandard": "c++11",
      
         "intelliSenseMode": "gcc-x64",
      
         "configurationProvider": "ms-vscode.cmake-tools"
      
        }
      
       ]
      
      ,
    "version": 4
}

launch.json

{
    
    
    "version": "0.2.0",
    "configurations": []
}

settings.json

{
    
    
    "files.associations": {
    
    
        "iostream": "cpp",
        "ostream": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "cinttypes": "cpp",
        "typeinfo": "cpp"
    }
}

tasks.json

{
    
    
    "tasks": [
        {
    
    
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "C:\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
    
    
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
    
    
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

If the installation path of your mingw64 and the like is different from mine, remember to change it to your own.

CMAKE configuration

Finally, we create a CMakeLists.txt, and then we can happily run the code.

Here is an example, using the npcap library, CMakeLists can refer to mine.

I use include to install header files, Lib to install link libraries, you can configure according to your own, and change it to the corresponding one in CMakeLists
insert image description here

cmake_minimum_required(VERSION 3.0)
project(pcap_parser)
set(CMAKE_CXX_STANDARD 11)
include_directories(./include)
link_directories(./Lib)
add_executable(pcap_parser main.cpp)
target_link_libraries(pcap_parser wpcap)
target_link_libraries(pcap_parser Packet)

Finally, click this to call cmake to compile and run
insert image description here

Guess you like

Origin blog.csdn.net/TU_Dresden/article/details/125687352