Im trying to check if sdl is properly installed on my Ubuntu 20.04 and its not.
Running all on 64bit type.
This is my code:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
bool initSDL()
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
std::cout << "Failed to init sdl: " << SDL_GetError() << std::endl;
return false;
}
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG)
{
std::cout << "Failed to init sdl_image: " << IMG_GetError() << std::endl;
return false;
}
return true;
}
int main(int argc, char *argv[])
{
std::cout << "Code Starting..." << std::endl;
initSDL();
int winX = 900, winY = 600;
std::cout << "Code Exited Properly." << std::endl;
return 0;
}
And this is my output:
Code Starting...
Failed to init sdl: No available video device
Code Exited Properly.
Im running my code with this commands:
gcc -c src/*.cpp -I include -m64 -lstdc++ -std=c++11
gcc *.o -o out/main -lSDL2main -lSDL2 -lSDL2_image -m64 -lstdc++ -std=c++11
./out/main
My code strucure is this way:
Main directory 'sdl_hello_world'
->src -> contains 'main.cpp'
->out -> contains compiled 'main'
Some methods i tried was:
export DISPLAY=:0
export SDL_VIDEODRIVER=x11
So how do i fix this ?
EDIT: I also followed this tutorial (https://www.youtube.com/watch?v=P3_xhDIP7bc&list=PLvv0ScY6vfd-p1gSnbQhY7vMe2rng0IL0&ab_channel=MikeShah), and still it gave the same error. So i guess the problem is in some computer setting.
EDIT: SOLVED:
i followed almost all tutorials and fix methods on the internet to solve my issue which uses gcc in linux. Some solutions on stack overflow seemed to prefer to compile the source code on their own computer WHICH WORKED.
So im pretty sure the way ubuntu installs it on my computer/ idk im new to using sdl. Updating this as might come handy to anyone else.
Still curious, is there a chance i did something wrong, or could this be considered a bug ?
EDIT: followed this btw https://wiki.libsdl.org/Installation
Related
I am trying to run a simple image show program in Eclipse CDT in MinGW built using cmake.
OpenCV Include Path : "E:\cv\opencv\eclipse\install\include"
OpenCV Library Path : "E:\cv\opencv\eclipse\lib" (has all libraries eg.libopencv_highgui310)
My Code is,
#include <iostream>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
int main(int argc, const char** argv) {
Mat img(500, 500, CV_8UC3, Scalar(100, 0, 0));
cout << "LOL!!!" << endl;
if (img.empty()) {
cout << "Error: Image cannot be loaded." << endl;
system("pause");
return -1;
}
namedWindow("Image Window", CV_WINDOW_AUTOSIZE);
imshow("Image Window", img);
if (waitKey(10) == 27) {
return -1;
}
destroyWindow("Image Window");
return 1;
}
When I build the code my console shows,
07:19:50 **** Incremental Build of configuration Release for project opencv_cpp ****
Info: Internal Builder is used for build
g++ "-IE:\\cv\\opencv\\eclipseBuild\\install\\include" -O3 -Wall -c -fmessage-length=0 -o "src\\faceDetect.o" "..\\src\\faceDetect.cpp"
g++ "-LE:\\cv\\opencv\\eclipseBuild\\lib" -o opencv_cpp.exe "src\\faceDetect.o" -llibopencv_highgui310 -llibopencv_core310 -llibopencv_imgproc310 -llibopencv_imgcodecs310 -llibopencv_objdetect310
07:19:56 Build Finished (took 5s.647ms)
When I run the program it just terminates and nothing happens. Even the print statement is not executed.
Here is the youtube link for the video of the problem,
https://youtu.be/kCrz_WPi_AI
Can someone help me with this?
Even I too faced this problem for longtime.
I researched through all the forum's , websites and all over google but i'm not able to find the answer.
But suddenly I thought of switching off the Windows Firewall and Windows Defender, And it magically works for me.. You too try it and let me know the result !
Happy coding :)
-compiled and installed successfully mongo-cxx-driver (mongo db c++ driver - 26Compat - all test ok passed). directory /usr, so /usrmongo/client/dbclient.h exists.
-running cmd:
g++ tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_system -lboost_regex -lboost_filesystem -lboost_program_options -o tutorial
-file tutorial.cpp
#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h" // for the driver
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
mongo::client::initialize();
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
results - error:
tutorial.cpp: In function ‘int main()’:
tutorial.cpp:11:12: error: ‘mongo::client’ has not been declared
any hint?
Not sure this helps but I got a similar error after installing the mongo-dev package using apt-get. This should not be done after mongo 2.6; it only works up to mongo 2.4 or something. It ended up corrupting my 2.6, so I had to clean up everything, reinstall mongo and then build mongo-cxx-driver from the github repo https://github.com/mongodb/mongo-cxx-driver according to their instructions.
Afterward eclipse still gave an error for the tutorial, but strangely it did build the thing. I had to clean up both Debug and Release there and ended up with only a warning, because the includes were messed up. So finally I just scrapped the eclipse project, copied the tutorial file to a new project and now it builds clean.
I am running Ubuntu 14.04, and using Eclipse CDT.
In my program, I am trying to initialize SDL and if it doesn't initialize then output the error, but SDL_GetError() returns "Failed to connect to the Mir Server". I am sure SDL is installed correctly since I can successfully initialize SDL on another project.
These are the libraries I am using: http://i.imgur.com/SS1mjzQ.png
#include <SDL2/SDL.h>
#include <iostream>
int main(int argc, char* args[]) {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cout << SDL_GetError() << std::endl;
}
return 0;
}
bash$ export DISPLAY=:0
Setting the DISPLAY run time environment variable fixes it for me — typical X Windows lossage, fails to default to your local display (designed for remote displays) which you'd only know if you'd gone to X11 summer camp.
A complete working example in bash:
(cd /tmp && g++ -xc++ - -lSDL2 && (DISPLAY=:0 ./a.out; echo \$? = $?)) <<.
#include <SDL2/SDL.h>
#include <iostream>
int main() {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cout << SDL_GetError() << std::endl;
return 1;
}
return 0;
}
.
$? = 0
Does the environment you are running on have a windowing system?
This error has come up for me when I'm running tests initializing SDL2 on Travis CI using Ubuntu 14.04. Based on what I've been able to gather from further testing and hints I've gotten from google searches SDL_Init when initializing SDL if it is passed SDL_INIT_VIDEO (which you are doing implicitly with SDL_INIT_EVERYTHING), it will try and connect with the windowing system of your machine.
So perhaps try:
SDL_Init(0)
and then initialize the other subsystems later with:
SDL_InitSubSystem(SDL_INIT_EVERYTHING)
If you do keep in mind that you must quit every subsystem you initialize in this case it's just technically. You would do that like this:
SDL_QuitSubSystem(SDL_INIT_EVERYTHING)
I had this error when I was using gcc to compile.
When I used g++ to compile it fixes the issue. The Lazy Foo tutorial also recommends you to use g++ to compile.
If you are having this problem you can try using g++ to compile and see if this resolves the issue.
After I learned the very basics of cpp i decided to push myself ahead and try SDL2 and try to make a game. I found the lazy foo's SDL tutorials. I tried to follow it but i seemed to have problems with installation. After putting in a "test" code i tried compiling it, and these messages showed up on the log:
C:\File\Location\For\My\Project\Makefile.win file not recognized: File format not recognized
C:\File\Location\For\My\Project\collect2.exe [Error] ld returned 1 exit status
I think it might be a linking error and heres my linkers:-lmingw32-lSDL2main-lSDL2
I tried deleting this Makefile.win but the same message just showed up and there isn't even a collect2.exe
I'm using the Orwell Dev-C++ using the mingw gcc 4.8.1 32bit release compiler, and heres the code:
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, char **argv){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Quit();
return 0;
}
Im using the latest SDL 2.0 version on Xubuntu 64-bits. I installed through the provided install script on the source code.
Compiling works well, however when trying to open a font or image (regardless of its extension), it will always fail to open.
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
int main (int argc, char *argvp[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
cout << SDL_GetError() << endl;
}
if (TTF_Init() == -1)
{
std::cout << TTF_GetError() << std::endl;
return 2;
}
TTF_Font *font1 = NULL;
font1 = TTF_OpenFont("SourceSansPro-Regular.ttf", 20);
if (font1 == NULL)
{
std::cout << "ERROR OPENING FONT = " << TTF_GetError() << std::endl;
}
TTF_CloseFont(font1);
SDL_Quit();
return 0;
}
I compiled with
g++ -Wall fontTEST.cpp -o TEST -lSDL2 -lSDL_ttf (NOTE that SDL_ttf installs as such, not as SDL2_ttf)
And get the following error: Failed to load font: 0 Couldn't load font file
This happens with images as well. I've already tried with different fonts and images, apparently it works if I compile with SDL 1.2, just not with 2.0.
Also why does the provided install script installs the lib and include folders in /user/local/?
I moved them to /usr/ but the problem persists.
Remember the following:
On Unix, file paths are case-sensitive
As said in Xonar's comment, tilde '~' expansion is a shell feature, it does not work in C/C++ programs, you should use the real path instead.
The strace log says clearly that something is wrong with the path.
You should try the following:
Rename your font file to "font.ttf"
put it in /home/user/font.ttf
use "/home/user/font.ttf" as the path in your code.