i'm trying to use the SDL1.2 librairie with c++ files but, i can't compile.
i'm on windows and when i want to compile, the linking cannot be done.
i made a makefile :
CC = gcc
CFLAGS = -Wall -I include
LDFLAGS = -L/lib -lmingw32 -lSDLmain -lSDL -lSDL_image -mwindows
Programme : main.o
$(CC) main.o -o Programme $(LDFLAGS)
main.o : main.cpp
$(CC) $(CFLAGS) -c main.cpp -o main.o
clean :
del -rf *.o
mrproper : clean
del Programme
and here is my main.cpp :
#include <stdio.h>
#include <SDL/SDL.h>
void pause();
int main(int argc, char *argv[])
{
SDL_Surface *ecran = NULL, *imageDeFond = NULL;
SDL_Rect positionFond;
positionFond.x = 0;
positionFond.y = 0;
SDL_Init(SDL_INIT_VIDEO);
ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
SDL_WM_SetCaption("Chargement d'images en SDL", NULL);
imageDeFond = SDL_LoadBMP("lac_en_montagne.bmp");
SDL_BlitSurface(imageDeFond, NULL, ecran, &positionFond);
SDL_Flip(ecran);
pause();
SDL_FreeSurface(imageDeFond);
SDL_Quit();
return EXIT_SUCCESS;
}
void pause()
{
int continuer = 1;
SDL_Event event;
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
}
}
}
and when i run the makefile with the command :
mingw32-make
here is the output :
C:\Users\summire\Desktop\a day in hell>mingw32-make
gcc main.o -o Programme -L/lib -lmingw32 -lSDLmain -lSDL -lSDL_image -mwindows
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDLmain
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL_image
collect2.exe: error: ld returned 1 exit status
mingw32-make: *** [makefile:6: Programme] Error 1
can someone please explain me what am i doing wrong ?
also, here is my arborescence :
/
img/
example1.png
include/
SDL/
all the .h files for SDL
lib/
libSDL.a
libSDLmain.a
src/
(nothing yet)
Related
When i Call SDL_Init passing in SDL_AUDIO_INIT, the function returns a failure.
I called SDL_GetError() but there does not seem to be any message.
I am not having problems getting VIDEO to initialize, only audio. I don't get any errors when compiling. I have linked the SDL mixer library in my makefile.
Here is a minimum reproducible example code:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string>
int main( int argc, char* args[] )
{
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO < 0))
{
printf( "SDL VIDEO could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else if ( SDL_Init( SDL_INIT_AUDIO) < 0)
{
printf( "SDL AUDIO could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else
{
printf("video and audio initialized");
}
return 0;
}
output: SDL AUDIO could not initialize! SDL Error:
(there is no message for the error)
Here is the makefile
#OBJS specifies which files to compile as part of the project
OBJS = 21_sound_effects_and_music.cpp
#CC specifies which compiler we're using
CC = g++
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -g -Wall
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf -lSDL2_mixer
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = sdl_program
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
I am using Ubuntu 20.04
install libasound2-dev libpulse-dev
and recompyling SDL2
Audio drivers : disk dummy oss alsa(dynamic) pulse(dynamic) <--- IS OK
On macOS, if I compile with
clang -I. -framework OpenGL main.cpp glad.c /usr/local/Cellar/glfw/3.3.1/lib/libglfw.3.3.dylib
I can build and run the generated executable from the following code:
#define GL_SILENCE_DEPRECATION
#include <stdio.h>
#include "glad/glad.h"
#include "/usr/local/Cellar/glfw/3.3.1/include/GLFW/glfw3.h"
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClearColor(0.9, 0.1, 0.1, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
But I would like to build using a Makefile, so I prepared
CC = clang
XXFLAGS = -g -Wall -v
CPPFLAGS = -I.
LDLIBS = -lm -L/usr/local/Cellar/glfw/3.3.1/lib -lglfw
LDFLAGS = -framework OpenGL
OBJS = main.o glad.o
PROGRAM = main
$(PROGRAM): $(OBJS)
$(CXX) $^ $(XXFLAGS) $(CPPFLAGS) $(LDLIBS) $(LDFLAGS) -o $#
clean:
-rm -f *.o
distclean: clean
-rm -Rf $(PROGRAM)
It actually makes the executable but when I run it I get
$ ./main
Segmentation fault: 11
So I guess there is something wrong in the Makefile, right?
$ make -n
c++ -I. -c -o main.o main.cpp
clang -I. -c -o glad.o glad.c
c++ main.o glad.o -g -Wall -v -I. -lm -L/usr/local/Cellar/glfw/3.3.1/lib -lglfw -framework OpenGL -o main
You didn't define the rule to build OBSJ, so make used its built-in rules. The PROGRAM rule only links object files, and there's no point specifying CPPFLAGS there.
.PHONNY: all
all: clean a.out
a.out:
clang++ -I glad/include -F /Library/frameworks -framework OpenGL main.cpp glad/src/glad.c /usr/local/Cellar/glfw/3.3.1/lib/libglfw.3.3.dylib
clean:
rm a.out
References:
https://www.gnu.org/software/make/manual/make.html
https://opensource.com/article/18/8/what-how-makefile
I'm trying to link to SDL2 statically on Arch Linux in order to produce single, cross-platform executable for a game. SDL2 headers are installed in /usr/include/SDL2, and libraries in /usr/lib. Compiling dynamically with g++ -I/usr/include/SDL2 -lSDL2 hello.cpp works, as ./a.out produces a blank window, but I am unable to link SDL2 to the executable statically.
Contents of hello.cpp:
#include <iostream>
#include "SDL.h"
int main() {
bool quit{false};
SDL_Event inputEvent;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("Hello", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 1280, 0);
while (!quit) {
SDL_WaitEvent(&inputEvent);
switch (inputEvent.type) {
case SDL_QUIT:
quit = true;
break;
}
}
SDL_Quit();
std::cout << "Bye!";
return 0;
}
g++ -I/usr/include/SDL2 -static -lSDL2 hello.cpp returns /usr/bin/ld: cannot find -lSDL2, while g++ -I/usr/include/SDL2 -l:libSDL2 hello.cpp returns /usr/bin/ld: cannot find -l:libSDL2.
It turns out that PKGBUILD of sdl2-hg in the AUR was renaming libSDL2.a to libSDL2main.a, hence the libSDL2.a not found error. Removing the problematic line from the PKGBUILD and recompiling SDL2 alleviated the issue.
I just started learning c++ and makefiles. Now I'm stuck.
There seem to be dozens of questions like this, but I can't figure out which answer applies to my situation. This answer is probably obvious. Some clues for where to look and reasons for what I am doing is wrong would be greatly appreciated!
This is my error:
Undefined symbols for architecture x86_64:
"App::init(char const*, int, int, int, int, bool)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [../out/MYAPP] Error 1
This is my code:
main.cpp
#include "App.h"
// our App object
App* a_app = 0;
int main(int argc, char* argv[])
{
a_app = new App();
a_app->init("Hello World", 100, 100, 640, 480, true);
//...etc more code
return 0;
}
App.h
#ifndef __App__
#define __App__
#include <SDL2/SDL.h>
class App
{
public:
App(){}
~App(){}
bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
//..etc
private:
};
#endif /* defined(__App__) */
App.cpp
#include "App.h"
#include <iostream>
using namespace std;
bool App::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
{
// attempt to initialize SDL
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
cout << "SDL init success \n";
int flags = 0;
if(fullscreen)
{
flags = SDL_WINDOW_FULLSCREEN;
}
// init the window
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
//...etc
}
else
{
cout << "SDL init fail \n";
return false; // SDL init fail
}
cout << "SDL init success \n";
m_bRunning = true; // everything inited successfully, start the main loop
return true;
}
And finally my makefile
CXX = clang++
SDL = -framework SDL2 -framework SDL2_image
INCLUDES = -I ~/Library/Frameworks/SDL2.framework/Headers -I ~/Library/Frameworks/SDL2_image.framework/Headers
CXXFLAGS = -Wall -c -std=c++11 $(INCLUDES)
LDFLAGS = $(SDL) -F ~/Library/Frameworks/
SRC = src
BUILD = build
OUT = ../out
EXE = $(OUT)/MYAPP
OBJECTS = $(BUILD)/main.o $(BUILD)/App.o
all: $(EXE)
$(EXE): $(OBJECTS) | $(OUT)
$(CXX) $(LDFLAGS) $< -o $#
$(BUILD)/%.o : $(SRC)/%.cpp | $(BUILD)
$(CXX) $(CXXFLAGS) $< -o $#
$(BUILD):
mkdir -p $(BUILD)
$(OUT):
mkdir -p $(OUT)
clean:
rm $(BUILD)/*.o && rm $(EXE)
You are not linking all the object files.
$(CXX) $(LDFLAGS) $< -o $#
should be
$(CXX) $(LDFLAGS) $^ -o $#
since the $< pseudo-variable expands to only the first dependency, which is main.o. That matches with the linker error.
In fact, making this modification alone makes the error go away on my machine.
simple code the image pointer are NULL why?:
// defined in the header files
SDL_Surface* m_imageIcon;
SDL_Surface* m_CharMainTile;
bool SDLmanager::loadFiles()
{
m_imageIcon = loadImage("D:\\java\\workspace\\SDL_test1\\Debug\\robot1.bmp",type_bmp);
/*
mem print as you see empty
Name : m_imageIcon
Details:0x0
Default:0x0
Decimal:0
Hex:0x0
Binary:0
Octal:0
*/
m_CharMainTile = loadImage("D:\\java\\workspace\\SDL_test1\\Debug\\vx_chara09_d.png",type_png);
bool b = false;
if(m_imageIcon == NULL) // this is null
b = true;
if(m_CharMainTile == NULL) // this is null
b = true;
if(!b)
return false;
return true;
}
this is where i set the files there all vaild and in the given path
SDL_Surface *SDLmanager::loadImage( string filename , imgType t)
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
if(t== type_bmp)
{
loadedImage = SDL_LoadBMP( filename.c_str() );
}
else
{
loadedImage = IMG_Load( filename.c_str() );
}
//If nothing went wrong in loading the image
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
//Return the optimized image
return optimizedImage;
}
optimizedImage has vaild address and value
Name : optimizedImage
Details:0x4d6ea8
Default:0x4d6ea8
Decimal:5074600
Hex:0x4d6ea8
Binary:10011010110111010101000
Octal:023267250
compilation is fine and clean :
make all
Building file: ../Main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g -Wall -c -fmessage-length=0 -MMD -MP -MF"Main.d" -MT"Main.d" -o "Main.o" "../Main.cpp"
Finished building: ../Main.cpp
Building file: ../SDLmanager.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g -Wall -c -fmessage-length=0 -MMD -MP -MF"SDLmanager.d" -MT"SDLmanager.d" -o "SDLmanager.o" "../SDLmanager.cpp"
Finished building: ../SDLmanager.cpp
Building target: SDL_test1.exe
Invoking: MinGW C++ Linker
g++ -o "SDL_test1.exe" ./Main.o ./SDLmanager.o -lmingw32 -lSDL_image -lSDLmain -lSDL
Finished building target: SDL_test1.ex