Compiling Basic SDL Program

  • Thread starter Thread starter dcole
  • Start date Start date
  • Views Views 666
  • Replies Replies 6

dcole

New Member
Newbie
Joined
Oct 13, 2025
Messages
4
Reaction score
0
Trophies
0
Age
60
XP
23
Country
Canada
Hello,

I am trying to compile a basic SDL2 program using the SDL2 libraries installed by devKitpro's pacman. When I compile it, I get the following error which i can't figure out:

linking ... sdl-display-image.elf
/opt/devkitpro/devkitPPC/bin/../lib/gcc/powerpc-eabi/15.1.0/../../../../powerpc-eabi/bin/ld: /opt/devkitpro/devkitPPC/bin/../lib/gcc/powerpc-eabi/15.1.0/../../../../powerpc-eabi/lib/crtmain.o: in function `__crtmain':
crtmain.c:(.text.__crtmain+0x38): undefined reference to `main'
collect2: error: ld returned 1 exit status
make[1]: *** [/opt/devkitpro/devkitPPC/wii_rules:31: /home/dcole/Documents/wii_dev/sdl-display-image/sdl-display-image.elf] Error 1
make: *** [Makefile:101: build] Error 2

Has anyone experienced this before or know a dirrection to point me in? I wonder if it is something in my Makefile, I am not good at understanding these Makefiles:

#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif
include $(DEVKITPPC)/wii_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := source
DATA := data
INCLUDES :=
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
CFLAGS = -g -O2 -Wall $(MACHDEP) $(INCLUDE)
CXXFLAGS = $(CFLAGS)
LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS := -lSDL2 -lwiiuse -lbte -logc -lm
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(DEVKITPRO)/portlibs/wii
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(TARGET)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
#---------------------------------------------------------------------------------
# automatically build a list of object files for our project
#---------------------------------------------------------------------------------
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
export LD := $(CC)
else
export LD := $(CXX)
endif
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES)
export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))
#---------------------------------------------------------------------------------
# build a list of include paths
#---------------------------------------------------------------------------------
export INCLUDE := $(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD) \
-I$(LIBOGC_INC)
#---------------------------------------------------------------------------------
# build a list of library paths
#---------------------------------------------------------------------------------
export LIBPATHS := -L$(LIBOGC_LIB) $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
export OUTPUT := $(CURDIR)/$(TARGET)
.PHONY: $(BUILD) clean
#---------------------------------------------------------------------------------
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol
#---------------------------------------------------------------------------------
run:
wiiload $(TARGET).dol

#---------------------------------------------------------------------------------
else
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).dol: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)
$(OFILES_SOURCES) : $(HFILES)
#---------------------------------------------------------------------------------
# This rule links in binary data with the .jpg extension
#---------------------------------------------------------------------------------
%.jpg.o %_jpg.h : %.jpg
#---------------------------------------------------------------------------------
@echo $(notdir $<)
$(bin2o)
-include $(DEPENDS)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

Here is the main code itself:

#include <wiiuse/wpad.h>
#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
int main(int argc, char* argv[]) {
WPAD_Init();
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) {
//std::cout << "Error SDL2 Initialization : " << SDL_GetError();
return 1;
}

if (IMG_Init(IMG_INIT_PNG) == 0) {
//std::cout << "Error SDL2_image Initialization";
return 2;
}
SDL_Window* window = SDL_CreateWindow("First program", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
if (window == NULL) {
//std::cout << "Error window creation";
return 3;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
//std::cout << "Error renderer creation";
return 4;
}
SDL_Surface* lettuce_sur = IMG_Load("lettuce.png");
if (lettuce_sur == NULL) {
//std::cout << "Error loading image: " << IMG_GetError();
return 5;
}
SDL_Texture* lettuce_tex = SDL_CreateTextureFromSurface(renderer, lettuce_sur);
if (lettuce_tex == NULL) {
//std::cout << "Error creating texture";
return 6;
}
SDL_FreeSurface(lettuce_sur);
bool done = false;
while (!done){
// scans if a button was pressed
WPAD_ScanPads();
u32 held = WPAD_ButtonsHeld(0);

// if the homebutton is pressed it will set done = true and it will fill the screen
// with a black background
if(held & WPAD_BUTTON_HOME){
done = true;
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, lettuce_tex, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_DestroyTexture(lettuce_tex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return 0;
}
 
Use the following:

Makefile:
CFLAGS  = -g -O2 -Wall $(MACHDEP) $(INCLUDE) `sdl2-config --cflags`

LIBS    :=  `sdl2-config --libs` -lwiiuse -lbte -logc -lm

LIBDIRS := $(PORTLIBS)
 
Use the following:

Makefile:
CFLAGS  = -g -O2 -Wall $(MACHDEP) $(INCLUDE) `sdl2-config --cflags`

LIBS    :=  `sdl2-config --libs` -lwiiuse -lbte -logc -lm

LIBDIRS := $(PORTLIBS)

Then I get this:

template.c
linking ... sdl-display-image.elf
/opt/devkitpro/devkitPPC/bin/../lib/gcc/powerpc-eabi/15.1.0/../../../../powerpc-eabi/bin/ld: template.o: in function `SDL_main':
/home/dcole/Documents/wii_dev/sdl-display-image/source/template.c:15:(.text.SDL_main+0x28): undefined reference to `IMG_Init'
/opt/devkitpro/devkitPPC/bin/../lib/gcc/powerpc-eabi/15.1.0/../../../../powerpc-eabi/bin/ld: /home/dcole/Documents/wii_dev/sdl-display-image/source/template.c:32:(.text.SDL_main+0x8c): undefined reference to `IMG_Load'
/opt/devkitpro/devkitPPC/bin/../lib/gcc/powerpc-eabi/15.1.0/../../../../powerpc-eabi/bin/ld: /home/dcole/Documents/wii_dev/sdl-display-image/source/template.c:67:(.text.SDL_main+0x110): undefined reference to `IMG_Quit'
collect2: error: ld returned 1 exit status
make[1]: *** [/opt/devkitpro/devkitPPC/wii_rules:31: /home/dcole/Documents/wii_dev/sdl-display-image/sdl-display-image.elf] Error 1
make: *** [Makefile:102: build] Error 2

Seems its upset at the SDL2/SDL_image.h include.
 
Then I get this:

template.c
linking ... sdl-display-image.elf
/opt/devkitpro/devkitPPC/bin/../lib/gcc/powerpc-eabi/15.1.0/../../../../powerpc-eabi/bin/ld: template.o: in function `SDL_main':
/home/dcole/Documents/wii_dev/sdl-display-image/source/template.c:15:(.text.SDL_main+0x28): undefined reference to `IMG_Init'
/opt/devkitpro/devkitPPC/bin/../lib/gcc/powerpc-eabi/15.1.0/../../../../powerpc-eabi/bin/ld: /home/dcole/Documents/wii_dev/sdl-display-image/source/template.c:32:(.text.SDL_main+0x8c): undefined reference to `IMG_Load'
/opt/devkitpro/devkitPPC/bin/../lib/gcc/powerpc-eabi/15.1.0/../../../../powerpc-eabi/bin/ld: /home/dcole/Documents/wii_dev/sdl-display-image/source/template.c:67:(.text.SDL_main+0x110): undefined reference to `IMG_Quit'
collect2: error: ld returned 1 exit status
make[1]: *** [/opt/devkitpro/devkitPPC/wii_rules:31: /home/dcole/Documents/wii_dev/sdl-display-image/sdl-display-image.elf] Error 1
make: *** [Makefile:102: build] Error 2

Seems its upset at the SDL2/SDL_image.h include.
Makefile:
CFLAGS  = -g -O2 -Wall $(MACHDEP) $(INCLUDE) `sdl2-config --cflags` `$(PREFIX)pkg-config SDL2_image --cflags`

LIBS    :=  `$(PREFIX)pkg-config SDL2_image --libs` `sdl2-config --libs` -lwiiuse -lbte -logc -lm
 
  • Love
Reactions: impeeza
Makefile:
CFLAGS  = -g -O2 -Wall $(MACHDEP) $(INCLUDE) `sdl2-config --cflags` `$(PREFIX)pkg-config SDL2_image --cflags`

LIBS    :=  `$(PREFIX)pkg-config SDL2_image --libs` `sdl2-config --libs` -lwiiuse -lbte -logc -lm

That totally did it, I am so grateful for your help! I'll have to study this now to figure out why it worked.
 
That totally did it, I am so grateful for your help! I'll have to study this now to figure out why it worked.
The first error happened because libSDL plays a trick with your main() function and renames it (if does a "#define main SDL_main"), and provides its own main() function in the "SDL2main" library. You need to link to it too, in order for the build to succeed.
Using sdl2_config or pkg-config makes it so that
Code:
-lSDL2main
is added to the linker command line.

You can further simplify your makefile by using pkg-config for SDL, too:
Code:
CFLAGS  = -g -O2 -Wall $(MACHDEP) $(INCLUDE) `$(PREFIX)pkg-config sdl2 SDL2_image --cflags`

LIBS    :=  `$(PREFIX)pkg-config sdl2 SDL2_image --libs` -lwiiuse -lbte -logc -lm
 
  • Like
Reactions: jimble_racetrack
The first error happened because libSDL plays a trick with your main() function and renames it (if does a "#define main SDL_main"), and provides its own main() function in the "SDL2main" library. You need to link to it too, in order for the build to succeed.
Using sdl2_config or pkg-config makes it so that
Code:
-lSDL2main
is added to the linker command line.

You can further simplify your makefile by using pkg-config for SDL, too:
Code:
CFLAGS  = -g -O2 -Wall $(MACHDEP) $(INCLUDE) `$(PREFIX)pkg-config sdl2 SDL2_image --cflags`

LIBS    :=  `$(PREFIX)pkg-config sdl2 SDL2_image --libs` -lwiiuse -lbte -logc -lm

Thanks for the additional info mudrik!
 

Site & Scene News

Popular threads in this forum