dyld: Library not loaded...reason image not found? - c++

I'm new to c++ and XCode, I'm using sdl2 to create a window but when i compile it, it crashes giving me a thread.I have included opengl.h , stdio.h and SDL2.h. There are questions about
dlyd:library not loaded but their different.
Error Message:
dyld: Library not loaded: #rpath/SDL2.framework/Versions/A/SDL2 Referenced from:
/Users/shayanrazavi/Library/Developer/Xcode/DerivedData/c++_code-bbdyozxqxxdxosbxuyhcrqobxrkd/Build/Products/Debug/c++
code
Reason: image not found
This is the code I used i couldn't get int main to be inside the code block for some reason but anyway and I got this code from https://wiki.libsdl.org/SDL_CreateWindow.
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully made
if (window == NULL) {
// In the event that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// The window is open: enter program loop (see SDL_PollEvent)
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}

I figured out why this was happening I was meant to put the framework in /Library/Frameworks folder before using it in XCode because when you download SDL it gives you a read me file and the read me file says to put it in that folder.
I should try reading all the text in read me files next time I guess. But if I try running this in XCode it will crash for some reason. (Makes sense because it says dyld: Library not loaded and we just put it in /Library/Frameworks)

Related

Test program for SDL - "No such file or directory"

I searched but could not find an issue similar to mine. Thanks for any help!
I am using SDL in Code Blocks on a Mac.
I installed SDL according to this tutorial:
https://www.youtube.com/watch?v=Bi9BPEwEMDU&t=5s
Here is how I set up the compiler and linker in C::B according to the video:
Compiler Settings:
+Search directories+
/usr/local/Cellar/sdl2/2.0.5/include/SDL2
+Linker+
/usr/local/lib
Linker Settings
+Link Libraires+
/usr/local/lib/libSDL2_test.a
/usr/local/lib/libSDL2-2.0.0.dylib
/usr/local/lib/libSDL2.a
/usr/local/lib/libSDL2main.a
The test program builds, but the terminal window states:
~ Buckwheat$ /Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Buckwheat/Documents/Code Blocks Projects/o/bin/Debug/o
sh: /Users/Buckwheat/Documents/Code: No such file or directory
Process returned 127 (0x7F) execution time : 0.002 s
Here is the test program:
// Example program:
// Using SDL2 to create an application window
#include "SDL.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
// The window is open: could enter program loop here (see SDL_PollEvent())
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
Your path contains spaces :
/Users/Buckwheat/Documents/Code Blocks Projects/o/bin/Debug/o
And your shell takes the part of the path before the space as one separate argument :
sh: /Users/Buckwheat/Documents/Code: No such file or directory
You have to escape the spaces characters like this :
/Users/Buckwheat/Documents/Code\ Blocks\ Projects/o/bin/Debug/o

Including SDL.h causes program to terminate immediately after building, and not run, but not give an error

This test program should create a blank window that stays open until you x-it-out. I copied it from SDL's documentation to make sure it is correct. It can be found here.
// Example program:
// Using SDL2 to create an application window
#include "SDL.h"
#include <stdio.h>
int main(int argc, char* argv[]) {
SDL_Window *window; // Declare a pointer
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
//game loop, quitGame to quit
bool quitGame = false;
//var for checking events
SDL_Event event;
while(!quitGame) {
//Update particles
//Draw particles
//Check for events
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT)
quitGame = true;
}
}
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
It doesn't create a window and terminates immediately, but gives no errors.
I'm using Eclipse, mingw32, and the latest stable release of SDL2. SDL2's libraries and headers are within a file in my C drive. I am using a 64 bit system. I include the entire folder of SDL2's header files. The only library folder I have linked is the one within the 64 bit part of the SDL2 folder. The libraries I have linked are the ones suggested by HolyBlackCat, (in this order) mingw32, SDL2main, and SDL2. Any help is greatly appreciated. Thanks!

Allegro 5 al_create_display(x, y) not working

I have set up my allegro 5.0.7 project in MSVC 2010 properly and the code executes. I am able to compile and run programs that will display an error dialog or something. However, whenever I run a program that draws a window, the window is not shown on my screen. I see it minimized with a broken file icon. The code runs with no errors, however. Here is an example of some code that gives me this problem. Thanks!
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
This code even exits after 10 seconds, as it should. The only problem is that the window is not drawn to the screen. It is only minimized, with a broken file icon. I have Windows 7 64-bit.
This is a known bug affecting certain configurations that may be fixed in a more recent version.
Use al_set_window_position() to move the window onscreen.

SDL_BlitSurface() not displaying image?

So I'm trying to display a simply image with the SDL library, but when I use the function SDL_BlitSurface() nothing happens, and all I get is a black screen. I should also note that I have the .bmp file, the source, and the executable file all in the same directory.
//SDL Header
#include "SDL/SDL.h"
int main(int argc, char* args[])
{
//Starts SDL
SDL_Init(SDL_INIT_EVERYTHING);
//SDL Surfaces are images that are going to be displayed.
SDL_Surface* Hello = NULL;
SDL_Surface* Screen = NULL;
//Sets the size of the window (Length, Height, Color(bits), Sets the Surface in Software Memory)
Screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
//Loads a .bmp image
Hello = SDL_LoadBMP("Hello.bmp");
//Applies the loaded image to the screen
SDL_BlitSurface(Hello, NULL, Screen, NULL);
//Update Screen
SDL_Flip(Screen);
//Pause
SDL_Delay(2000);
//Deletes the loaded image from memory
SDL_FreeSurface(Hello);
//Quits SDL
SDL_Quit();
return 0;
}
LoadBMP() is crap. Install SDL_image library
sudo apt-get install SDL_image SDL_image_dev
(not sure about the names of the packages. Just use aptitude or synaptic or whatever to find them)
and include it with
#include "SDL_image.h"
You load your image then with
SDL_Surface* Hello = IMG_Load("Hello.bmp");
if (!Hello){
printf("Ooops, something went wrong: %s\n", IMG_GetError());
exit(0);
}
Important: Note that you should always do an error check and print out the error.
if (!Hello) is the same as if (Hello == NULL)
have you tried blitting any other types of images? when I first started SDL I remember having issues with .bmp files. Try a .jpg or .png and get back to me whether your code works or not.
I had similar "Problems"; maybe it's a pre-Version, or version incompatible to your graphic-driver; let's figure out. SWSurface and Flip; as I remember, the Flip-functionality only works with double-buffered HW_Surface.
Screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
Please try Update instead of Flip.
SDL_Update(surface);
And nexttime :)
Hello = SDL_LoadBMP("Hello.bmp");
if(Hello != NULL) {
//Applies the loaded image to the screen
SDL_BlitSurface(Hello, NULL, Screen, NULL);
//Update Screen
...
//Deletes the loaded image from memory
SDL_FreeSurface(Hello);
}
because SDL_FreeSurface(NULL) will crash your programm.

How to use SDL with OGRE?

When I go to use OGRE with SDL (as described in this article), I seem to be having trouble with a second window that appears behind my main render window. Basically, the code I'm using is this:
SDL_init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
Ogre::Root *root = new Ogre::Root();
root->restoreConfig();
root->initialise(false);
Ogre::NameValuePairList windowSettings;
windowSettings["currentGLContext"] = Ogre::String("True");
Ogre::RenderWindow *window = root->createRenderWindow("MainRenderWindow", 640, 480, false, &windowSettings);
window->setVisible(true);
The question is, how do I get rid of the extra window?
Just for posterity, I'm using OGRE 1.6.4, Mac OS X 10.6.2, and SDL 1.2.14.
I ended up figuring this out on my own. The problem ends up being that OGRE's Mac GL backend does not honor the currentGLContext option, so the best solution is to change to SDL 1.3 (directly from Subversion, as of time of writing) and use the SDL_CreateWindowFrom call to start getting events from a window created by OGRE. It should also be noted that the OGRE window needs to have the macAPI set to cocoa, or else SDL won't recognize the window handle.
I see that you already solved your problem, but not all users will be content with downgrading SDL to 1.3. You can use SDL2 and an SDL2 window created via SDL_CreateWindow with OGRE. The code would look something like this:
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR, "Cannot initialize SDL2!",
"BaseApplication::setup");
}
Ogre::Root *root = new Ogre::Root();
root->restoreConfig();
root->initialise(false);
Ogre::NameValuePairList params; // ogre window / render system params
SDL_Window *sdlWindow = SDL_CreateWindow("myWindow", posX, posY, width, height, vflags);
// see SDL_CreateWindow docs / examples for how to populate posX, posY, width, height, and vflags according to your needs
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
if (SDL_GetWindowWMInfo(sdlWindow, &wmInfo) == SDL_FALSE) {
OGRE_EXCEPT(Ogre::Exception::ERR_INTERNAL_ERROR,
"Couldn't get WM Info! (SDL2)",
"BaseApplication::setup");
}
params.insert(std::make_pair("macAPI", "cocoa"));
params.insert(std::make_pair("macAPICocoaUseNSView", "true"));
// grab a string representing the NSWindow pointer
Ogre::String winHandle = Ogre::StringConverter::toString((unsigned long)wmInfo.info.cocoa.window);
// assign the NSWindow pointer to the parentWindowHandle parameter
params.insert(std::make_pair("parentWindowHandle", winHandle));
Ogre::RenderWindow *ogreWindow = root->createRenderWindow("myWindowTitle", width, height, isFullscreen, &params);
// see OGRE documentation on how to populate width, height, and isFullscreen to suit your needs
// create OGRE scene manager, camera, viewports, etc