Visual studio code 错误:-未定义对`\u imp的引用__GetStockObject@4'和对`\u imp的未定义引用__SetBkMode@8'

Visual studio code 错误:-未定义对`\u imp的引用__GetStockObject@4'和对`\u imp的未定义引用__SetBkMode@8',visual-studio-code,c++17,Visual Studio Code,C++17,我在Visual Studio代码中在C++中编写了一个代码,以获得简单的窗口,但是无法构建和运行 提供了用于调试的所有文件,如task.json、launch.json和cpp-properties.json 请告诉我如何在VisualStudio代码中创建windows项目 错误是 > Executing task: g++ -g -lgdi32 main.cpp < C:\Users\abhi\AppData\Local\Temp\cc0d0bc2.o: In functio

我在Visual Studio代码中在C++中编写了一个代码,以获得简单的窗口,但是无法构建和运行 提供了用于调试的所有文件,如task.json、launch.json和cpp-properties.json 请告诉我如何在VisualStudio代码中创建windows项目

错误是

> Executing task: g++ -g -lgdi32 main.cpp <

C:\Users\abhi\AppData\Local\Temp\cc0d0bc2.o: In function `WinMain@16':
C:\Users\abhi\Desktop\abs/main.cpp:38: undefined reference to `_imp__GetStockObject@4'
C:\Users\abhi\AppData\Local\Temp\cc0d0bc2.o: In function `Z10WindowProcP6HWND__jjl@16':
C:\Users\abhi\Desktop\abs/main.cpp:91: undefined reference to `_imp__SetBkMode@8'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
            "preLaunchTask": "echo",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
cpp properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0\\include\\c++"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "8.1",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "compilerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gcc.exe"
        }
    ],
    "version": 4
}
您正在使用Visual Studio代码,但这个问题实际上与VSCode无关,因为在命令提示符Windows cmd.exe或Cygwin bash下运行第一个命令可以重现此问题:

由于您正在尝试,因此需要传递-mwindows选项:


将-mwindows添加到tasks.json命令中,它也应该在VSCode中工作。

还有更多内容。我看到的谜团是链接器如何找到库,即使搜索路径从未指定,为什么从user32导入的内容没有生成相同的错误,它如何找到链接user32(即使未指定),为什么丢失的符号具有32位名称装饰(即使他使用64位编译器)。我所能猜到的是,OP决定任意复制文件来解决问题,把它变成了一个大蜡球。祝你好运。@HansPassant使用mingw64-5.4.0 g++-std=c++11,我得到的错误与OP报告中OP代码和命令行的错误几乎完全相同。这些库来自链接器的默认搜索路径,该路径在g++-v中可见。我不知道你提出的其他问题。
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
            "preLaunchTask": "echo",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0\\include\\c++"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "8.1",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64",
            "compilerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gcc.exe"
        }
    ],
    "version": 4
}
  > g++ -g -lgdi32 main.cpp
  (errors as above)
  > g++ -g -mwindows main.cpp
  (works)
  > ./a.exe
  (runs)