Search code examples
c++makefilelinker-errorssdl-2

C++ SDL2_gfx - "SDL2_gfxPrimitives.h: No such file or directory" error


I want to use SDL2_gfx in my project (VSCode on Linux Mint), but I get a "SDL2_gfxPrimitives.h: No such file or directory" error. I already have SDL2 and SDL2_ttf installed and working.

I installed SDL2_gfx using the instructions on the website (https://www.ferzkopp.net/Software/SDL2_gfx/Docs/html/index.html). Here is my Makefile:

CXX       := g++
CXX_FLAGS := -std=c++17 -ggdb

BIN     := bin
SRC     := src
INCLUDE := include

LIBRARIES   := -lSDL2main -lSDL2 -lSDL2_ttf -lSDL2_gfx
EXECUTABLE  := main


all: $(BIN)/$(EXECUTABLE)

run: clean all
    clear
    ./$(BIN)/$(EXECUTABLE)

$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp
    $(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBRARIES)

clean:
    -rm $(BIN)/*

Here are the includes I'm using:

#pragma once

#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <algorithm>

#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2_gfxPrimitives.h>

using namespace std;

I thought that this error might be caused by SDL2_gfx being installed in /usr/local/include/SDL2 and the other two SDL libraries in /usr/include/SDL2 , so I added /usr/local/include/SDL2 into c_cpp_properties.json includePath:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/include/SDL2",
                "/usr/include/SDL2"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

This did not help. There is likely something extremely obvious that I'm missing here, but I can't find the mistake. There is already a post with this issue (Cannot use SDL_gfxPrimitive), but it does not have an answer besides simply checking the compiler flags.


Solution

  • You are compiling with make so c_cpp_properties has nothing to do with it. You need to add -I/usr/local/include to CXX_FLAGS and a corresponding -L flag to LIBRARIES:

    CXX_FLAGS := -std=c++17 -ggdb -I/usr/local/include
    LIBRARIES := -L/usr/local/lib -lSDL2main -lSDL2 -lSDL2_ttf -lSDL2_gfx
    

    Upon further searching I noticed that SDL2_gfx comes with a pkg-config file so you can just ask pkg-config: (you may need to fiddle with PKG_CONFIG_PATH to get it to look at /usr/local)

    CXX_FLAGS := -std=c++17 -ggdb $(shell pkg-config --cflags SDL2_gfx)
    LIBRARIES := -lSDL2main -lSDL2 -lSDL2_ttf $(shell pkg-config --libs SDL2_gfx)
    

    and since SDL2_gfx depends on sdl2 you get the compile flags for SDL2 for free, but I would add sdl2 to the invocation of pkg-config explicitly.