Strange error when including Graphics.hpp - SFML - c++

I'm trying to setup an SFML 2.1 64 bit project on Ubuntu 12.04(also 64 bit) using eclipse cdt.
First I made a new project called LearningSFML.
Then I went to Project>Properties>C/C++ Build>Settings
Under GCC C++ Compiler>Includes I added the path to my include folder
And under GCC C++ Linker>Libraries I added sfml-window, sfml-graphics, sfml-system(in that order) to the "Libraries" list
And finally added < SFML_PATH >/lib to the "Library search path" list box
After doing this, I tested it with the following code
#include <SFML/Window.hpp>
int main()
{
sf::Window window(sf::VideoMode(640, 480), "Learning SFML");
return 0;
}
And if flashed a window as you would expect. But changing the code slightly to use sf::RenderWindow instead of sf::Window:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "Learning SFML");
return 0;
}
creates an error saying make: *** [LearningSFML] Error 1. I searched the internet for similar problems. One website I found said that the error means there is no main function, but clearly I do have a main function.
So how can I fix this error?

The console was outputting warning: libjpeg.so.62, needed by lib/libsfml-graphics.so, not found (try using -rpath or -rpath-link) which is where I was an idiot because I didn't look at this until RetiredNinja's comment.
To fix it just install libjpeg:
sudo apt-get install libjpeg62

Related

Using SFML in Code::Blocks

I'm trying to make use of SFML in Code::Blocks. I've followed the guide at https://www.sfml-dev.org/tutorials/2.5/start-cb.php to the letter but it doesn't work. Please help!
In the project's build options I have specified everything:
Errors:
Here is simple code:
int main(){
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
sf::RenderWindow window(sf::VideoMode(800, 600), "Epic RPG");
return 0;
}
The errors seem to indicate that linking went wrong - I don't see how
I used CB's own SFML project type and it works now
I see that you are using static libs, if you want to use them you need to reference SFML source code as well. Instead, try to put the other same libraries but without the "-s" on them.

sdl, sdl2 error: SDL_window (among others) not declared

I have played around with C++ for a while and just recently started to get into SDL and SDL2.
I was able to get the dot demo program to work.
But other programs, such as Lazy Foo' Productions's copied and pasted don't seem to work.
I have both SDL and SDL2 installed (and uninstalled and reinstalled.) I am on Ubuntu 15.04 and I have the IDE CodeBlocks linked (-ISDL2)
The errors are SDL_Window - SDL_WINDOWPOS_UNDEFINED - SDL_WINDOW_SHOWN - SDL_CreateWindow - SDL_GetWindowSurface - SDL_UpdateWindowSurface and finally, SDL_DestroyWindow -- was not declared in this scope.
Also, I include:
#include </usr/include/SDL/SDL.h>
#include </usr/include/SDL2/SDL.h>
#include <stdio.h>
I'm pretty sure that I don't need all of that location, but it didn't work without it either. One other note, when I type the #includes, CodeBlocks will suggest SDL2/SDL.h but not SDL/SDL.h.
What am I missing?
I don't think I can put Lazy Foo' code here - I didn't get permission...
The code you listed;
#include </usr/include/SDL/SDL.h>
#include </usr/include/SDL2/SDL.h>
#include <stdio.h>
Why don't you change it to
#include <SDL2/SDL.h>
#include <stdio.h>
as the first header is where SDL_CreateWindow and other SDL2 functions are declared?
Also you don't need to include both SDL and SDL2 headers. Indeed that could very well be the source of your problem as you would only need to include the version you're using.
If you're following the tutorials from lazyfoo's site, you can check if the ones you're following are using SDL1.2 or SDL2 from their table of contents, as the site actually have the tutorials for both versions.
UPDATE:
I didn't notice that your platform is a Linux platform. Then it is so much easier to solve your problem. The demo that you followed previously was done using SDL-1.2, whereas the gcc error hinted that you're using SDL-2.0, hence SDL_CreateWindow and other undefined errors. You should install SDL-2.0 library and SDL-2.0 development files (which will provide you with the necessary SDL-2.0 headers). You may refer this to SDL-2.0 packages provided by your platform distribution.
As for the compilation, it'll be the same as the tutorial you've followed with a minor change, instead of gcc sdltest.o -lSDL -o sdltest, you'll issue gcc sdltest.o -lSDL2 -o sdltest to indicate that you're linking your code against SDL2 library.
EDIT
A simple SDL program to test your environment. You can use any of the simpler text editor such as nano or gedit or others to edit this, and run the compilation command above to test your setup.
The simplest way to do this is by copy the code, then from your terminal, issue cat > sdltest.cpp and paste the code, then hit [ENTER] and [CTRL-C] to end it. Then you can issue the compilation command as mentioned previously,g++ sdltest.cpp -lSDL2 -o sdltest.
Code;
#include <SDL2/SDL.h>
#include <stdio.h>
int main()
{
SDL_Window *p;
SDL_Renderer *w;
p = SDL_CreateWindow("Game",SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,800,640,SDL_WINDOW_SHOWN);
w = SDL_CreateRenderer(p, -1, 0);
SDL_RenderClear(w);
SDL_SetRenderDrawColor(w,255,0,0,255);
SDL_Rect Rect = {220,140,200,200};
SDL_RenderFillRect(w,&Rect);
SDL_RenderPresent(w);
SDL_Delay(3000);
SDL_DestroyRenderer(w);
SDL_DestroyWindow(p);
SDL_Quit();
return 0;
}
Hope that helps.

Problems running my SFML script

I have just recently installed SFML and am trying to learn it but cannot work out why its not working.
My script is from their tutorials, so its very short:
#include <SFML/Window.hpp>
#include <iostream>
int main()
{
// Create the main window
sf::Window App(sf::VideoMode(800, 600, 32), "SFML Window");
// Start main loop
bool Running = true;
while (Running)
{
App.Display();
}
return EXIT_SUCCESS;
}
The problem i am having is I am getting a couple of errors and am unsure what I have got wrong.... these are my errors:
Link to my error list is here (couldn't format the list nicely on here):
http://www.paste.to/MTYzNzE2Mw==
Hope some one can explain where I went wrong.
Thanks!
I have no experience with SFML itself but looks like an issue installing the library or a compilation issue in the linking stage(sfml library isnt being linked against).
Have you followed the getting started tutorials listed here which setup compilation environment for sfml?
It looks like you didn't install the library correctly.
Why don't you try sfml with CodeBlocks ?

Having trouble using SDL framework on XCode

I am having trouble using the SDL framework in my xcode project, my main (all there is in the project at the moment) currently looks like this:
#include <SDL/SDL.h>
#include <iostream>
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
And the error I am receiving when building is:
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
(maybe you meant: __Z8SDL_mainiPPKc)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
It does not give me an error about the framework not being found. and I have looked around Stackoverflow for this problem and I did find one person who had their framework in the wrong place on their OS. But I have tried all places (/library/frameworks && /~username~/library/frameworks && /system/library/frameworks) and still no luck.
Additional info:
I did however notice after some searching on the internet that the official website http://www.libsdl.org/download-1.2.php
Does not have a OSX version of the Development library. While many tutorials on how to use SDL on OSX say these are required.
Also I am adding my library via Xcode itself ,not through drag'ndrop. Xcode seems to reckognize it.
Would be much appreciated if anyone could help, going crazy over this error.
UPDATE:
Still no luck, I have followed every step provided by solutions here below. perhaps this screenshot is of any help.
The main() function is corrected, but didn't help. I tried changing the path to the header files, im lost on this one. Does anyone perhaps have any alternatives to building this in xcode?
UPDATE2:
The suggestion worked, however now it is giving me this warning, which won't let me run the application.
Fixed! I removed the path in the build settings. Strange how I still don't know what went wrong, either way. thanks a lot for the help! made my day!
I see three possible problems. The first problem is how you're including SDL.h in your code. You should include SDL.h with the following code:
#include "SDL.h"
The second possible problem is you haven't added the files SDLMain.h and SDLMain.m to your project. Those files are necessary to compile SDL code on Mac OS X. The files are in the devel-lite folder on the SDL disk image.
The third possible problem is that your project doesn't link to the Cocoa framework. The Mac version of SDL uses Cocoa so you need the Cocoa framework in your project.
The following article walks you through the setup of an Xcode 4 project for SDL:
Using SDL with Xcode 4
UPDATE
I noticed a possible problem in how you defined the main() function. You have a space between char and * and another space between * and argv, which could be causing the error. The main() function in my SDL code is defined in the following way:
int main(int argc, char *argv[])

Undefined reference to 'SDL_main'

I've recently decided to try working with SDL with CodeBlocks 10.05. I started with the tutorial on http://www.sdltutorials.com/sdl-tutorial-basics and did my best to follow it. Unfortunately, I'm encountering:
..\..\..\..\..\..\SDL\SDL-1.2.15\lib\libSDLmain.a(SDL_win32_main.o):SDL_win32_main.c|| undefined reference to `SDL_main'|
when I try to compile it.
I've searched through many of the questions on this website and other tutorials (mainly the tutorial on LazyFoo and the CodeBlocks wiki) and can't seem to find a solution.
C:\SDL\SDL-1.2.15\include has been added in the Compiler tab (Search Directories)
C:\SDL\SDL-1.2.15\lib has been added in the Linker tab
The libraries libmingw32.a, libSDLmain.a, libSDL.dll.a are linked in that order
libmingw32.a from the MinGW\lib folder in the CodeBlocks installation directory
SDL.dll is in both the System32 folder and in the project folder
When attempting to follow the tutorial on the CodeBlocks wiki, I was told that SDL.h could not be found in the given directory (when making a new SDL project).
CApp.cpp
#include "CApp.h"
#include "SDL\SDL.h"
CApp::CApp(){
Surf_Display=NULL;
Running=true;
}
int CApp::OnExecute(){
if (OnInit()==false){
return -1;
}
SDL_Event Event;
while (Running){
while (SDL_PollEvent(&Event)){
OnEvent(&Event);
}
OnLoop();
OnRender();
}
OnCleanup();
return 0;
}
int main(int argc, char* argv[]){
CApp theApp;
return theApp.OnExecute();
}
CApp.h
#ifndef CAPP_H_INCLUDED
#define CAPP_H_INCLUDED
#include "SDL\SDL.h"
class CApp{
private:
bool Running;
SDL_Surface* Surf_Display;
public:
CApp();
int OnExecute();
public:
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
};
#endif // CAPP_H_INCLUDED
put these arguments to the main function. I had this problem too, and I fixed it few seconds ago.
int main(int argv, char** args)
{
}
Try #undef main after all SDL related headers.
Update. This is not a valid solution!
As pointed out by HolyBlackCat, this is a pretty sloppy fix. SDL replaces the main function in order to perform some initialization and/or cleanup that is otherwise not possible, and then calls back to the actual user function.
The interception works by replacing the name of user's main function to SDL_main, with a simple macro
#define main SDL_main
The user's function then ceases to be the entry point for the application, and an entry point provided by SDL is used. The proposed #undef disables the interception recklessly and one should argue that it is not supposed to work at all. For those who successfully compiled and ran an SDL application after this "fix", it must have simply been a platform-dependent coincidence.
The proper solution to the OP's error is making sure that the file containing main gets compiled and linked, and that the function has correct signature. As already posted by others.
The only plausible reason for your problem I can think of is that when you created the file with main in it, you forgot to add it to build targets.
You should see CApp.cpp in the list where my main.cpp is. Right click on it and click Properties. Click on Build tab in the window that pops up. You should see this:
Click OK, hit Ctrl+F11 (Rebuild).
Good luck.
Recently, I had this problem, watched some YouTube videos and also followed the installation guide by LazyFoo and I kept getting the "Undefined reference to SDL_main" error.
I followed everything said here after successfully linking the minGw files to my project properties, but it didn't work until I added to my main function int main (int argv, char** args) and voila, it worked.
using namespace std;
int main(int argv, char** args) { if(SDL_Init(SDL_INIT_VIDEO)<0) {
cout<<"Endl Init Failed."<<endl;
return 1; }
cout<<"SDL Init succeeded."<<endl;
SDL_Quit(); return 0; }
Why it worked is still not clear to me but it worked anyway.
Define SDL_MAIN_HANDLED before you include the SDL libs:
#define SDL_MAIN_HANDLED
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
More info: I'm using the SDL functions without the SDL_main be defined. Is that fine?
This was the first question that pop out to my search.
None of the solutions worked for me.
Eventually I figured it what worked for me. Will post the my answer here to "any fellow wanderer".
My problem was that I tried to compile some old C game not a C++ one.
One part of the solution for me was to rename the main.c to main.cpp
And then to signal to CMake to use CPP compiler for all C files.
Eventually here is a portion of my CMakeLists.txt:
# set minimum cmake version
cmake_minimum_required(VERSION 3.19 FATAL_ERROR)
# project name and language
project(FootballManager LANGUAGES CXX)
# This is mandatory too, because otherwise they are not compiled an linked.
file(GLOB_RECURSE CFILES "${CMAKE_SOURCE_DIR}/*.c")
SET_SOURCE_FILES_PROPERTIES(${CFILES} PROPERTIES LANGUAGE CXX)
# list sources
list(APPEND _sources config.h)
# ... removed some of the other sources for brevity ...
list(APPEND _sources misc.c)
# Link SDL, yes this is SDL1.2
find_package(SDL REQUIRED)
include_directories(FootballManager ${SDL_INCLUDE_DIRS})
add_executable(fm main.cpp ${_sources})
target_link_libraries(fm ${SDL_LIBRARIES})