Windows Closes my SFML project after running - c++

Everything is added correctly and I don't get any errors in the debug window. However when I go to RUN the application windows gives me the normal "SFMLProject.exe Not Responding" and then closes my application. I run a Tight computer and there is no virus protector doing anything to my application. I'm not sure what is wrong.
Here is the code I have but I don't think it will help. Just a simple "Music Stream"
#include <SFML/Audio.hpp>
int main()
{
sf::Music music;
if ( !music.openFromFile("music.ogg") )
return -1; // error
music.play();
return 0;
}

Your program shows Not responding because it returned a non-zero value. Somehow, SFML is not able to play the file. Check what the exact error is by reading the SFML documentation.
Where is your OGG file located? Is it in the Debug builds folder, or in the Release builds folder? Try providing the absolute path like C:/Path/To/Your/File/music.ogg

Related

OpenCV program compiles but doesn't run

I am working on Windows 8 with OpenCV 2.4.13 and MinGW 4.9.
I wrote a simple and small opencv program to check if everything was installed properly. Following is the code:
#include <opencv2/highgui/highgui.hpp>
int main () {
printf("in main\n");
for (int i = 0; i<10; i++) {
printf("here\n");
IplImage * image = cvLoadImage("C:/{...}/test.jpg");
cvReleaseImage(&image);
}
return 0;
}
I compiled it with the following command at the command prompt:
g++ -o test test.cpp -LC:\{...}\opencv\build\x64\vc11\lib -lopencv_core2413 -lopencv_highgui2413 -IC:\{...}\opencv\build\include
{...} is the path to the specified folder/file.
This command runs properly and compilation is successful without any errors. However, when I run it with:
test
in main and one here gets printed after which I get the error message as 'test.exe has stopped working. Windows is looking for a solution.'
What all I have tried:
For installation of OpenCV, ran the downloaded opencv executable file (which extracts all files) and added the system variable OPENCV_DIR and edited the system PATH for location of DLLs (which reside in %OPENCV_DIR%\bin) as per:
http://docs.opencv.org/2.4/doc/tutorials/introduction/windows_install/windows_install.html#installation-by-using-the-pre-built-libraries
Tried adding the required DLLs in the same directory as the .exe.
Tried doing the whole thing from vc12 directory.
After the error message appears, it gives an option of debugging. On pressing that, the Just In Time Debugger opens up and says 'An unhandled win32 exception occurred in test.exe'. I googled this and tried inspecting the registry key as directed here
https://support.microsoft.com/en-us/kb/811191
but it was already properly set. So, there was nothing for me to change in that.
Nothing is working for me at all. Please let me know if any more information is required. I'm desperately looking for a solution to this.
For those who might be encountering the same problem, I compiled the program with OpenCV dynamic (.dll) libraries instead of the .lib files and it ran just fine at runtime for some reason.

Application runs fine when built from Xcode, won't run directly from the .app

Working on a c++ mac application in Xcode. When I hit run everything is fine (debug and release mode). But when I go into finder into the Products/Release folder and double click on the .app directly, the app immediately closes with an error (application quit unexpectedly. click report to see more detailed information). When I click report, the line of code it errors on seems to indicate that it isn't finding my resource files, but it finds them fine when I build/run the same exact app from xcode. heres an example of how i use the resource files in the code:
std::ifstream file;
file.open("AppName.app/Contents/Resources/Saves/1.svd");
//do stuff
file.close();
anyone have any ideas why double clicking the .app that xcode just created would have different results than running it from xcode?
also not sure if this is part of the issue, but it seems strange that I have to reference the resource folder from outside the .app file (AppName.app/Contents/Resources/Saves/1.svd). I would expect that path to be relative to the executable in .app/Contents/MacOS like this (../Resources/Saves/1.svd) but that didnt work either.
I had similar problem and spending some time researching different options I ended up using Apple's objects. I needed to get resources folder:
#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#endif
Then actual snippet is:
#ifdef __APPLE__
char path[FILENAME_MAX];
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX)){
//
//log("CFURLGetFileSystemRepresentation returned false. Path:%s", path);
exit(0);
}
CFRelease(resourcesURL);
chdir(path);
//log("Current path:%s", path);
#endif

C++ on windows closes a program immediately after launching

I installed minGW and the eclipse CDT, and the console keeps doing something weird. The code of the program is
using namespace std;
#include <iostream>
int main() {
cout << "Hello, windows (8, c++)" << endl;
//system("PAUSE");
return 0;
}
You all know it, its the Hello World program. Now when I run this the Eclipse console displays some stuff about building, and then goes blank. And when I navigate to the HelloWorldProgram.exe in the explorer and run it, a windows flashes up and displays "hello world", but then immediately closes. When I do this on Mac OSX there's no problem, and the windows stays up until I decide to close it. Now I know there's a command
system("PAUSE") //I dont know what I need to import to use this. Could you tell me that too?
Which will give me more or less the same effect, but I'd like to know why Windows does it differently from OSX, and what I can do to fix it (bc this annoys the crap out of me).
Looking forward to your replies!
This happens on Windows because this is just the behavior of the Windows console. You'll have to open up the console manually and then running your program through the console you've opened if you don't want the window to close automatically once the program has executed.
You may want to take a look at these:
What is the Best Practice for Combating the Console Closing Issue?
https://superuser.com/questions/186562/how-can-i-keep-the-terminal-open
Don't use system("pause"), it's wrong for a multitude of reasons (read more about it here).
Put cin.get() before return and the window will stay open until you press enter.
If you want to just run your console program, you should open a console, and run it.
Apparently, the OSX version of Eclipse is configured to open a console, and run the program, and not close it. Maybe you can configure the Win version so, too.
You shouldn't meddle with your program to behave differently on another platform, instead wrap it into something that 'adapts' the behaviour.
Probably, you can tell eclipse to use "cmd /c 'yourprogram.exe && pause'", to open a command window and have it execute your program and then pause.
Just add getch(); before return, and add #include <conio.h>.

Qt Creator + OpenCV: Program runs from .exe but not from editor

Well, I need to start working with OpenCV and as I'm used to working with QtCreator, I'm trying to make it all work together. I downloaded the latest OpenCV version, and compiled it with MinGW. Then, I created this little console project to try it out. Below is the .pro file:
QT += core
QT -= gui
TARGET = OpenCV_test4
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:\\Librerias\\opencv2.3.1\\release\\include
LIBS += -LC:\\Librerias\\opencv2.3.1\\release\\lib \
-lopencv_core231.dll \
-lopencv_highgui231.dll \
-lopencv_imgproc231.dll \
-lopencv_features2d231.dll \
-lopencv_calib3d231.dll
Here is the main.cpp file:
#include <QtCore/QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// read an image
cv::Mat image= cv::imread("img.jpg");
// create image window named "My Image"
cv::namedWindow("OpenCV Window");
// show the image on window
cv::imshow("OpenCV Window", image);
// wait key for 5000 ms
cv::waitKey(5000);
return a.exec();
}
(I have tried this code with and without the QCoreApplication lines)
The deal is: It links and builds, and when runs from QtCreator only a terminal window called C:\QtSDK\QtCreator\bin\qtcreator_process_stub.exe appears with the line "Press RETURN to close this window..."
But, if I run the .exe from the project folder, it runs perfectly!! Why is QtCreator unable to launch the application? I found this really strange, and I would appreciate any hint about this. It's really not THAT important, but it is kind of a pain to have to run the .exe manually every time I change something to check how it works.
Thanks for your time :)
Additional Info:
I have tried both debug and release versions, the problem is the same in both of them.
Debugging does not work, it never stops at any breakpoint.
I'm running on Windows 7 Proffesional x64
SOLVED, I don't really know what I did, it suddenly worked and keeps working, I wish I could tell you how I fixed it but I have no idea, such a weird thing :(
Check Projects -> Run Settings -> Run in terminal. It have to be enabled, but seems to be disabled.
I have met same problem with QtCreator and OpenCL under Linux. Simple test program works after start from terminal and it does not work after start from the QtCreator.
I found the cause was hardcoded LD_LIBRARY_PATH in the project's run environment settings. I had dropped it to empty string and this had fixed issue.
I had the same issue with the following environment: Raspbian, Qt, openCV and a gui application.
old-ufo recommendation worked for me:
- First, enable "Run in terminal", which failed
- Then, disable "Run in terminal", which allowed me to correctly debug my app.
I understand that this is not scientific.

Somehow display C++ application using double-click on Ubuntu?

I'm totally new to Ubuntu and C++. Anyway, I have PHP experience.
I just created very simple application...
#include <iostream>
int main() {
std::cout << "Hello, world!";
return false;
}
Then compiled it....
g++ hello-world.cpp -o hello-world
But I can't open it with double-click on it, like I did on Windows 7. Only way to get that text printed is to do command...
./hello-world
Is it possible to open compiled file using simple double-click and then get that text somehow printed?
The program you wrote is a console application. In most Linux GUIs, by default if you open a console program from the GUI, the console output will not be displayed. You can either configure the GUI to open a terminal, or you can manually open a terminal and run it yourself.
When doing development, I highly recommend manually running the program - with using the GUI's automatic terminal window opening mode, the terminal will close as soon as the program terminates; so if the program crashes, the message will be lost. Manually opening a terminal ensures it sticks around after termination, so you can read the program's last messages before terminating.