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.
Related
i've been having some trouble trying to compile a very basic SFML project on an M1 mac. the exact error i've been getting is as follows:
Undefined symbols for architecture arm64: "__ZN2sf6StringC1EPKcRKSt6locale", referenced from: _main in cczblZnn.o ld: symbol(s) not found for architecture arm64 collect2: error: ld returned 1 exit status
when running
g++-12 -std=c++20 src/main.cpp -I/Users/rooster/SFML/include -o Ghost -L/Users/rooster/SFML/build/lib -lsfml-audio -lsfml-network -lsfml-graphics -lsfml-window -lsfml-system
so far, nothing i've tried has worked; i've already compiled the libs myself from the source using cmake, i've rearranged the order of the libraries in the build command, among other seemingly inconsequential tweaks in VSCode.
here's what i'm using:
VSCode
g++-12
library files compiled by me with cmake (using unix makefiles)
c++20
SFML 2.5.1_2
and here's my main.cpp:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(sf::Vector2u(200, 200)), "SFML");
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;
}
i'm relatively confident that i'm making some simple mistake. any help is greatly appreciated, and thank you for your time!!
fixed by scrubbing all references to SFML from my computer, installing it again with brew, and then using clang++ instead of compiling with g++ :)
I was trying to set up Sfml for my c ++ project by watching tutorials and documentation on how to do this.
I am using Mingw to compile and produce an executable, see how it goes.
#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;
}
Mingw command line input
g++ mycppfile -o myexefile -IDependencies/include -LDependencies/lib -lsfml-graphics -lsfml-window -lsfml-system -lopengl32 -lwinmm -lgdi32
But this error always appears.
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lsfml-graphics
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lsfml-window
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lsfml-system
I've researched many times trying to solve this problem, reviewed the names of the libraries, put all the Sfml dependencies in my project folder and the compiler folder, and also put the Sfml Dlls in the system32 folder and my project folder, but the problem has not yet been solved.
What can I do now?
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:
I saw similiar problem, but it didn't solved me my problem.
I can't compile this example code http://www.sfml-dev.org/tutorials/2.3/start-linux.php . I am following this instructions but I still have error:
In one before last step I wrote : g++ sfml.o -o sfml-app -L /home/Documents/SFML/SFML-2.1/lib -lsfml-graphics -lsfml-window -lsfml-system
and I got this:
sfml.o: In function main':
sfml.cpp:(.text+0x12d): undefined reference to sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)' collect2: ld returned 1 exit status
Can somebody help me with that?
It seems like you have't linked the sfml libraries to your program. Further down that page (just after the code snippet), the page further describes:
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
Although you have included the linking in your command line, my best guess is that the linker cannot find the libraries still, which is why sf::RenderWindow is undefined (it's declared, so the compiler knows what it is and thus compiles successfully, but cannot link properly because the linker can't find a reference for it)
I checked the path and folder with libraries and libs are there:
http://i.imgur.com/bTk7QCZ.png
But I still can't linkt it correctly.. I have installed SFML in my standard path (usr/lib)
http://i.imgur.com/Mzp9tMY.png
and after compile it shows the same error.
Error:
sfml.o: In function main':
sfml.cpp:(.text+0x12d): undefined reference tosf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
collect2: ld returned 1 exit status
My code it's the same as example code:
#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;
}
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