【问题标题】:Undefined Reference when using GCC and Windows.h in vscode在 vscode 中使用 GCC 和 Windows.h 时的未定义参考
【发布时间】:2021-02-27 14:54:13
【问题描述】:

我正在尝试在 vscode 中构建这个简单的代码,但是链接器报告了对 windows.h 中库函数的未定义引用。我不确定为什么会发生这种情况,我通常使用 Visual Studio 并且不需要外部链接 Windows 库文件,但如果我必须链接它们,我应该如何在 vscode 中进行操作。以下是我正在尝试构建的代码:

  #include <iostream>
  #include <Windows.h>

  int main() {    

    /* hide console window */
    ShowWindow(FindWindowA("ConsoleWindowClass", NULL), false);

    /* Calling GetDC with argument 0 retrieves the desktop's DC */
    HDC hDC_Desktop = GetDC(0);

    /* Draw a simple blue rectangle on the desktop */
    RECT rect = { 20, 20, 200, 200 };
    HBRUSH blueBrush=CreateSolidBrush(RGB(0,0,255));
    FillRect(hDC_Desktop, &rect, blueBrush);

    Sleep(10);
    return 0;
}

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": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

Tasks.json 文件的内容是:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\MinGW\\bin\\g++.exe",
            "miDebuggerPath":"C:\\MinGW\\bin",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:\\MinGW\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C:\\MinGW\\bin\\g++.exe",
        }
    ]
}

构建代码时返回的输出是:

Starting build...
Build finished with error:

C:\Users\Work\AppData\Local\Temp\ccnmSnMZ.o: In function `main':
c:/Users/Work/Desktop/so/code.cpp:14: undefined reference to `CreateSolidBrush@4'
collect2.exe: error: ld returned 1 exit status

The terminal process terminated with exit code: -1.

Terminal will be reused by tasks, press any key to close it.

【问题讨论】:

    标签: c++ visual-studio-code undefined-reference


    【解决方案1】:

    CreateSolidBrush 函数属于 gdi32.lib,所以你必须将它添加到你的链接器中。 希望它有效:)

    【讨论】:

    • 我知道这个库,但我如何在 vscode 中链接它。我试图通过在“args”行中将参数传递给tasks.json文件中的链接器来链接它:[“-g”,“${file}”,“-o”,“${fileDirname}\\${ fileBasenameNoExtension}.exe", "-l", "gdi32" ],但错误仍然存​​在。
    • 我从未使用过 C++ 的 VS Code,但可能类似于:"args": [ "-g", "${file}", "-o", "${fileDirname}\ \${fileBasenameNoExtension}.exe", "-L \\C:\\MinGW\\lib", "-l gdi32" ],。使用您当前的 MinGW/lib 路径
    • 包含或不包含路径的天气相同,但无论哪种方式都不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2021-02-04
    • 2020-02-03
    • 2017-11-29
    • 2013-05-27
    相关资源
    最近更新 更多