Search code examples
c++visual-studio-codesdlsdl-2

Undefined identifiers from SDL graphic library


I am trying to install on my VS Code the SDL (https://www.libsdl.org), cause I am going through book "Will_Briggs_C++20_for_Lazy_Programmers_Quick,_Easy,_and_Fun_C++" which uses SDL. However in book dont use vs code, so I am trying to manage how to install by myself. I followed Youtube Tutorials (example: https://youtu.be/jUZZC9UXyFs), but got trouble - I included library, added SDL2.dll file, tried to write in Makefile commands, but compiler doesnt recognise every SDL command. Whats wrong? (screenshot in the end) Source files are from Chapter1, folder "ch1\test-setup": https://github.com/Apress/cpp20-for-lazy-programmers

My main file:

#include <iostream>
#include "include\SDL2\SDL.h"

using namespace std;

int main(int argc, char **argv)
{
    // Initial window setup
    SSDL_SetWindowTitle("Simple tester program");
    SSDL_SetWindowSize(500, 375);

    // Load and play music
    SSDL_Music music = SSDL_LoadMUS("media/457729__razor5__boss-battle-2-0.wav");
    SSDL_VolumeMusic(int(MIX_MAX_VOLUME * 0.50));
    SSDL_PlayMusic(music, SSDL_FOREVER);

    // Show image
    const SSDL_Image BULLSEYE = SSDL_LoadImage("media/bullseye.jpg");
    SSDL_RenderImage(BULLSEYE, 0, 0);

    // Set font and color
    const SSDL_Font FONT = SSDL_OpenSystemFont("georgiab.ttf", 28);
    SSDL_SetFont(FONT);
    SSDL_SetRenderDrawColor(BLACK);

    // Make a label in the middle, centered
    sout << " Success! You should see\n  the bullseye,\n";
    sout << "  read this message,\n";
    sout << "  and hear music.\n\n\n\n";
    sout << " Hit any key to end.\n";

    // End program
    SSDL_WaitKey();
    return 0;
}

and Makefile:

# A makefile that should work for any SSDL projects
# Adapt as needed
#  -- from the SDL (Simple SDL) library, but no copyright is claimed

EXECUTABLE      = a.out                     #Yes, you can call it this and it should run, even in MinGW

C_SOURCES       = $(wildcard *.c)           #We'll try to compile all .c, .cpp, and .cc files
CPP_SOURCES     = $(wildcard *.cpp)
CC_SOURCES      = $(wildcard *.cc)

C_OBJECTS       = ${C_SOURCES:      .c  =.o}#Now we know what .o files we'll need to create
CPP_OBJECTS     = ${CPP_SOURCES:   .cpp=.o}
CC_OBJECTS      = ${CC_SOURCES: .cc =.o}
ALL_OBJECTS     = $(notdir $(C_OBJECTS) $(CPP_OBJECTS) $(CC_OBJECTS))

#       Tell g++ where to find include files for SDL2, SSDL
INCLUDES        =   ../../external/SDL2-MinGW/SDL2/i686-w64-mingw32/include/SDL2        \
                    ../../external/SDL2-MinGW/SDL2_image/i686-w64-mingw32/include/SDL2  \
                    ../../external/SDL2-MinGW/SDL2_mixer/i686-w64-mingw32/include/SDL2  \
                    ../../external/SDL2-MinGW/SDL2_ttf/i686-w64-mingw32/include/SDL2    \
                    ../../external/SSDL/include                                         \
                    ../../external/fmt-master/include
                    
INCLUDE_FLAGS   = $(foreach dir,    $(INCLUDES),     -I$(dir))

LANGUAGE_FLAGS  = -std=gnu++2a              #Specify language standard: C++20
#                                           MinGW can't do the fmt library
#                                           unless we specify gnu++ to add gnu
#                                           extensions, so I'll just use it throughout.

#       Tell g++ where to find library files for SDL2, SSDL
#       Sometimes library order matters.  If this is a problem
#        in the future -- if it says it can't find something that's in one of the 
#        listed libraries -- try putting libraries in different order 

LIBRARIES       := mingw32 ssdl SDL2main SDL2 SDL2_image SDL2_TTF SDL2_Mixer 
LIBRARY_DIRS    :=  ../../external/SDL2-MinGW/SDL2/i686-w64-mingw32/lib         \
                    ../../external/SDL2-MinGW/SDL2_image/i686-w64-mingw32/lib   \
                    ../../external/SDL2-MinGW/SDL2_mixer/i686-w64-mingw32/lib   \
                    ../../external/SDL2-MinGW/SDL2_ttf/i686-w64-mingw32/lib     \
                    ../../external/SSDL/mingw
LIB_FLAGS       := $(foreach library,$(LIBRARIES),    -l$(library))
LIB_DIR_FLAGS   := $(foreach libdir, $(LIBRARY_DIRS), -L$(libdir))

ALL_FLAGS       := $(INCLUDE_FLAGS) $(LANGUAGE_FLAGS) $(LIB_FLAGS) $(LIB_DIR_FLAGS)

##########################################################################

all: $(EXECUTABLE)
    g++ -I src/include -L src/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2 
#changes from Youtube

$(EXECUTABLE): $(ALL_OBJECTS)
#   echo 'ALL_FLAGS :' $(ALL_FLAGS)
    g++ -o $@ $^ -g $(ALL_FLAGS) 
#   -g means:    support debugging
#   $^ means:    use all the objects
#   -o $@ means: let the output be called $(EXECUTABLE)

clean:
    rm -f $(EXECUTABLE)
    rm -f *.o
    rm -f core
    rm -f *.ncb *.sdf
    rm -r -f Debug Release .vs Backup


Tried to find info in StackOver and in SDL issues (https://github.com/libsdl-org/SDL/issues), but no similar questions. Probably Its an easy fix, but I dont understand it.

Errors


Solution

  • Citing from the book Getting Started chapter:

    Most programs in the first half of this book use the SDL and SSDL libraries.

    Identifiers starting with SSDL_ come from another library called SSDL (which is seemingly wrapper over SDL). You need to install that as well in order to compile these examples.

    According to Notes (I don't have the book, just looked at preview in Springer) in the Appendix A there are instructions how to setup your environment and install both libraries. Seems that library source is in the repo itself, in external/SSDL directory here.