【问题标题】:How can I link SDL2 to the compiler?如何将 SDL2 链接到编译器?
【发布时间】:2021-08-22 08:01:56
【问题描述】:

我正在尝试使用 SDL2 显示 2D 图形以进行模拟。我正在尝试简单的 SDL2 函数,但我得到了一个叫做“链接器错误”的东西?

根据 YouTube 教程,这是我的 SDL2 测试代码,它应该只打开一个窗口 3 秒:

#include <SDL2/SDL.h>
#include <iostream>
#include <stdio.h>

int main()
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Window *window = SDL_CreateWindow("Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
    
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

    SDL_RenderPresent(renderer);

    SDL_Delay(3000);
}

这是我尝试运行时收到的错误消息:

[Running] cd "/Users/gabriel/Desktop/ideal gas simulation/VS Code/" && g++ SDLploxwork.cpp -o SDLploxwork && "/Users/gabriel/Desktop/ideal gas simulation/VS Code/"SDLploxwork
Undefined symbols for architecture x86_64:
  "_SDL_CreateRenderer", referenced from:
      _main in SDLploxwork-fa78ca.o
  "_SDL_CreateWindow", referenced from:
      _main in SDLploxwork-fa78ca.o
  "_SDL_Delay", referenced from:
      _main in SDLploxwork-fa78ca.o
  "_SDL_Init", referenced from:
      _main in SDLploxwork-fa78ca.o
  "_SDL_RenderPresent", referenced from:
      _main in SDLploxwork-fa78ca.o
  "_SDL_SetRenderDrawColor", referenced from:
      _main in SDLploxwork-fa78ca.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

[Done] exited with code=1 in 1.037 seconds

我在网上看到的最常见的修复方法是查看一个名为 tasks.json 的 VS Code 设置文件,特别是“args”列表,所以这是……除了"-framework", "SDL2",这些都是默认设置@HolyBlackCat 建议的台词。

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-framework",
                "SDL2"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

如果我编写一个顶部有#include &lt;SDL2/SDL.h&gt; 的hello world 程序,就没有问题。似乎这东西知道库在哪里,只是显然不知道它的功能在哪里。我也没有。

我在 Mac OS 10.13.6 上使用 VS Code 1.56.2 编写 C++98,尝试使用来自 here 的 SDL 2.0.16“开发库”和 clang-1000.10.44.4。如果有更多设置文本文件或我可以发布的任何内容来帮助找到问题,我会这样做。

我怎样才能让这个库工作?有人有什么想法吗?

【问题讨论】:

  • 您使用的是什么构建系统? CMAKE?
  • 先阅读this
  • @MarcinPoloczek 似乎他们直接调用编译器。
  • @HolyBlackCat 会很痛苦的。
  • 嗯。尝试查找一些适用于 Mac 的 SDL 教程,并逐字逐句学习。首先了解如何直接从终端编译。否则不知道。

标签: c++ visual-studio-code linker-errors sdl-2 macos-sierra


【解决方案1】:

您还需要更改 c_cpp_properties.json 文件。它应该看起来像这样。

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "Library/Frameworks/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/**"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "${default}",
            "compilerArgs": [
                "-framework SDL2"
            ]
        }
    ],
    "version": 4
}

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 2018-07-11
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多