Problem with build SFML project with mingw - c++

I want to build simple project:
////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#define SFML_DYNAMIC
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
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;
}
I build it with:
g++ main.cpp -I./include -L./lib -o main -lsfml-system -lsfml-window -static-libgcc .
Everything compile without erros, but when I run it looks:
I try to build with:
-lopengl32 -lglu3s
Without -static-libgcc and #SFML_DYNAMICS
A lot of combinations, but I get the same result: black command window instead of normal window with graphics.
I use SFML 1.6 , and gcc 4.5.2 ( I have the same problem on 3... version :/ )
Anyone know what I do wrong ? Or how to compile it ? I know i can try visual studio, but I want to make it with gcc.

I've just tried this - I used MinGW under Windows XP, and I get a window pop up. This looks normal to me.
Try some more examples from the tutorials.

Related

Having trouble getting SFML to work with C++ in Geany on MacOS

I want to get into game development with C++, so I decided to try SFML. However, even after putting all the files in the right places, it won't find the files need.
I followed all the steps on the page https://www.sfml-dev.org/tutorials/2.5/start-osx.php, making sure to put all the frameworks and dylibs in the right folders, but it still won't allow me to use the SFML files
#include <iostream>
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
window.display();
}
return 0;
}
What the program should do is display a black window that can close, but it gives me the error message
g++ -Wall -o "helloWorld" "helloWorld.cpp" (in directory /Users/Splavacado100/Desktop/Coding/C++)
helloWorld.cpp:2:10 fatal error: 'SFML/Graphics.hpp' file not found
#include <SFML/Graphics.hpp>
SFML (and to my knowledge any additional library outside the Standard Template Library) requires the compiler to know where the lib and include folders are stored. In this error instance, it looks like the IDE you're using isn't finding the right path to the folders. It's not listed in the MacOS version, because the tutorial you liked to assumes you're using XCode, which is like Visual Studio for Mac.
From what I can gather, if you're writing your program in a plain text editor, and compiling using makefiles or command line prompts, look at the SFML and Linux article for more complete idea of how to use it. The relevant to this scenario:
In case you installed SFML to a non-standard path, you'll need to tell the compiler where to find the SFML headers (.hpp files):
g++ -c main.cpp -I<sfml-install-path>/include Here,
is the directory where you copied SFML, for
example /home/me/sfml.
You must then link the compiled file to the SFML libraries in order to
get the final executable. SFML is made of 5 modules (system, window,
graphics, network and audio), and there's one library for each of
them. To link an SFML library, you must add "-lsfml-xxx" to your
command line, for example "-lsfml-graphics" for the graphics module
(the "lib" prefix and the ".so" extension of the library file name
must be omitted).
g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system If
you installed SFML to a non-standard path, you'll need to tell the
linker where to find the SFML libraries (.so files):
g++ main.o -o sfml-app -L<sfml-install-path>/lib -lsfml-graphics
-\lsfml-window -lsfml-system We are now ready to execute the compiled program:

Cant run SFML library in netbeans on ubuntu

Hi i want to add SFML for a graphical project in c++ but i get this error when i try to run som basic code:
free(): invalid pointer: 0x00000000009ace48 ***
And in netbeans properties under c++ compiler i have added the SFML/include folder in the include directories
And under linker and additionally library directory i have the sfml/lib folder
And last under linker and in the library i have added sfml-graphics-s-d, sfml-audio-s-d, sfml-network-s-d, sfml-window-s-d and sfml-system-s-d in that order.
This is the code i want to run:
#include <cstdlib>
#include <SFML/Graphics.hpp>
/*
*
*/
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
The SFML window is opening and directly closes and then i get:
RUN FINISHED; Aborted; core dumped; real time: 240ms; user: 20ms; system: 30ms
and the error i told in the start
Do any one know if its me that have added the library wrong or what it can be. thanks for help
Edit:
for now i have solved it by using a makefile and writing it in a text editor but i wonder if i can use same method in netbeans?
This is how my makefile looks and it works but i would like to be able to do it all in netbeans:
all:
g++ -std=c++11 *.cpp -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system
clean:
rm -rf *.o sfml-app
I think you have added wrong libraries, you should use "-d" libraries in case of dynamic linking. "-s-d" libraries are being used for static linking. In case you want to use static linking you need to define SFML_STATIC macro for preprocessor in your project. And you should add additional libraries, such as opengl32.lib, and others.

Error running .exe compiled with i586-mingw32msvc-g++ and SFML on linux

EDIT: Thanks random downvoter!
EDIT 2: Thanks #πάνταῥεῖ for explaining what was wrong with the question. When running a debugger I get
Program received signal SIGSEGV, Segmentation fault.
0x0046f4c6 in ?? ()
EDIT 3: This only happens if I run the cross compiled program. If I compile the program in Windows and run it with WINE, nothing wrong happens.
So well, I'm fighting with WINE and MinGW32 right now.
So I have a file, sortem.cpp which is compiled with a Makefile and linked and all from that Makefile.
sortem.cpp
#include <windows.h>
#include <iostream>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
int main ()
{
sf::RenderWindow window;
window.create(sf::VideoMode(720,480),"Sort 'em!");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
window.clear();
switch (event.type)
{
case sf::Event::Closed:
window.close();
}
window.display();
}
}
return 0;
}
Makefile
LD = ../bin
LIBS= $(LD)/sfml-window-2.dll $(LD)/sfml-system-2.dll $(LD)/sfml-graphics-2.dll $(LD)/sfml-network-2.dll $(LD)/sfml-audio-2.dll
OBJECTS= sortem.o
CXX= i586-mingw32msvc-g++
all: sortem.exe
sortem.exe: $(OBJECTS)
$(CXX) -o ../bin/sortem.exe $(OBJECTS) $(LIBS)
%.o: %.cpp
$(CXX) -c $<
clean:
rm *.o
So the program compiles perfectly, but when running sortem.exe with WINE, it says the program must exit. I click "Show details" and this pops up. Unhandled exception: page fault on write access to 0x00000000 in 32-bit code (0x0046f4c6).and a lot of hexdumps. I really don't know what I'm doing wrong, maybe the SFML libraries aren't up to date? But that'd give me a compile error, not a runtime error... Thanks a lot for the help, guys.
The libraries were compiled with MinGW GCC 4.7.1 and I was trying to compile my program with MinGW GCC 4.8.1, downgraded thanks to a friend and back online it is. Thanks you guys all for having shown me how to debug.
Peace

How do you include files in C++ from the /Library/Framework folder in MAC

I am trying to use SDL. I have a folder in /Library/Frameworks called SDL2.framework. I want to include the file SDL.h in my project. How do I do this? My code looks like:
// 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 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;
}
The error I get is:
Aarons-MacBook-Air:SDL aaron$ g++ main.cpp
main.cpp:4:10: fatal error: 'SDL.h' file not found
#include <SDL.h>
^ 1 error generated.
How do I properly include the SDL file? It is inside SDL2.framework, headers, SDL.h...
you will want to make a build script for this obviously, but the important parts are:
-I/usr/local/include or wherever your headers get installed.
I used home brew:
brew install sdl2
which puts the libraries in /usr/local/Cellar/
so if you need to specify the lib path you will also add:
-L/usr/local/lib -lSDL2
I also changed your include line to #include <SDL2/SDL.h>
Your header files is under the Headers folder, so in order to include this properly:
clang++ -std=c++11 -stdlib=libc++ -I/Library/Frameworks/SDL2.framework/Headers/
But I recommend Installing with homebrew:
brew install sdl2
Homebrew will install SDL2 libSDL2.a file under /usr/local/lib and /usr/local/include, so you will just need to include this Library path using the -L for library and -I flag to add search in /usr/local/include dir:
clang++ -std=c++11 -stdlib=libc++ main.cpp -I/usr/local/include -L/usr/local/lib -lSDL2 -o programfile
And include:
#include <SDL2/SDL.h>

SDL library in linux

I'm trying to compile this code:
#include "SDL/SDL.h"
int main(void) {
SDL_Surface *Hello = NULL;
SDL_Surface *Screen = NULL;
SDL_Init( SDL_INIT_EVERYTHING );
return 0;
}
But it happens that the compiler says that:
undefined reference to SDL_Init
I dont know why this is happening. I'm using Debian Mint and Code::Blocks. Could you Help me?
It looks like you haven't got -lSDL on your link line.
sdl-config returns the compile and link flags for your installation of SDL.
Assuming the program is sdl.cpp
g++ -o sdl `sdl-config --cflags` sdl.cpp `sdl-config --libs`
Should give you the correct flags.
Go to project and then build options and select your project name.
Now go to the linker setting and type the following lines in the Other Linker options textbox:
-lSDLmain
-lSDL
SDL also requires command line arguments in the main function so you should change
int main(void)
to
int main(int argc, char **argv)
Now compile your project and it should work.