首页 > 解决方案 > 使用 VSCode MACOS 构建时 GLFW 链接器命令失败(退出代码 1)

问题描述

在使用 Visual Studio Code 的 C++/C 扩展启动并运行后,我尝试使用 GLFW 创建一个窗口。我已经下载了GLFW,我的项目的文件结构如下:

引擎(工作区文件夹)-> 源文件-> test.cpp

引擎(工作区文件夹)-> 依赖项-> GLFW 库

这是我生成窗口的 C++ 代码:

#include <iostream>

#include <GLFW/glfw3.h>

using namespace std;

int main() {
   GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
    if (!window)
    {
        // Window or OpenGL context creation failed
    }
}

这是我使用 clang++ 构建的 tasks.json 配置:

"tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-I/Users/mynamew/Desktop/Engine/Dependencies/glfw-3.3.4.bin.MACOS/include"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        },

这是我的 c_cpp_properties.json 配置:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "/Users/myname/Desktop/Engine/Dependencies/glfw-3.3.4.bin.MACOS/include"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang++",
            "cStandard": "c11",
            "cppStandard": "c++98",
            "intelliSenseMode": "macos-clang-x64"
        }
    ],
    "version": 4
}

当我尝试构建项目时,我收到以下错误消息:

Undefined symbols for architecture x86_64:
  "_glfwCreateWindow", referenced from:
      _main in test-9301bf.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1

我的 Mac 架构和 GLFW 之间是否存在导致链接问题的兼容性问题?如果是这样,有什么解决办法还是我运气不好?任何帮助将不胜感激。

另外,一个元问题,但链接器错误是否表明编译成功但只有链接失败?据我了解,链接器是在编译之后出现的。

标签: c++macoscompilationlinkerglfw

解决方案


推荐阅读