0

I'm trying to use SDL2 to display 2D graphics for a simulation. I'm trying out simple SDL2 functions, and I'm getting something called a "linker error"?

Here's my SDL2 test code that should just open up a window for 3 seconds, according to a YouTube tutorial:

#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);
}

And here's the error message I get whenever I try to run it:

[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

The most common fix I've seen online involves poking at a VS Code settings file called tasks.json, specifically the list of "args", so here's that... These are the default settings except for the "-framework", "SDL2" lines suggested by @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"
}

If I write a hello world program with #include <SDL2/SDL.h> at the top, there's no trouble. It seems like the thing knows where the library is, it just apparently doesn't know where its functions are. And neither do I.

I'm using VS Code 1.56.2 on Mac OS 10.13.6 to write C++98, trying to use SDL 2.0.16 "development library" from here with clang-1000.10.44.4. If there's any more settings text files or whatever I can post to help find the problem, I'll do it.

How can I get this library to work? Does anyone have any ideas?

  • What build system are you using? CMAKE? – Marcin Poloczek Aug 22 '21 at 08:12
  • Read [this](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) first. – n. m. could be an AI Aug 22 '21 at 08:18
  • @MarcinPoloczek Seemingly they invoke the compiler directly. – HolyBlackCat Aug 22 '21 at 08:18
  • @HolyBlackCat Gonna be pain then. – Marcin Poloczek Aug 22 '21 at 08:19
  • Add `-framework SDL2` to the compiler flags. – HolyBlackCat Aug 22 '21 at 08:19
  • How does c++ compilation process work: https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work What is cmake: https://cmake.org/overview/ How to setup SDL2 on mac with cmake: https://wolfgang-ziegler.com/blog/sdl-cmake-osx – Marcin Poloczek Aug 22 '21 at 08:20
  • @MarcinPoloczek I don't think I installed CMAKE (I don't know what that is). All I do to get the code running is hit the "run code" triangle on the top right. Is using CMAKE worth looking into? – Gabriel Doudna Aug 22 '21 at 15:50
  • @HolyBlackCat re -framework SDL2 : I saw this elsewhere too. Could you be specific on where to find the compiler flags and how to edit them? – Gabriel Doudna Aug 22 '21 at 15:51
  • It's the `"args"` array in `tasks.json`. You would add `,"-framework","SDL2"` to the end of it. – HolyBlackCat Aug 22 '21 at 15:56
  • @HolyBlackCat Thanks for your help... I made the changes you suggested, and copied the changes to the code block in the main post so you can see if I did something wrong. After confirming that the changes were actually made to tasks.json from Finder, restarting VS Code, and restarting my computer - no luck. same error message. Anything else you can think of to try? – Gabriel Doudna Aug 22 '21 at 23:21
  • 1
    Hmm. Try finding some SDL tutorial for Mac, and following it word for word. Figure ou how to compile directly from the terminal first. Otherwise no idea. – HolyBlackCat Aug 23 '21 at 06:21

1 Answers1

0

You also need to change your c_cpp_properties.json file. It should look something like this.

{
    "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
}
DischordDynne
  • 117
  • 15