问题
I'm trying to follow a tutorial using the SDL2 library. I've followed the instructions here verbatim.
The program compiles fine, but when I try to run it within eclipse I get the following error when calling SDL_Init(SDL_INIT_VIDEO)
:
No available video device
After some research I decided this was because SDL2 had not been correctly linked to any display drivers so wasted several hours installing it from source, and trying various fixes suggested on various forums.
After following another tutorial I found that there was nothing wrong with my SDL2 installation. And in fact when I run the executable created by the eclipse project from the terminal, it runs without any problems.
There are similar questions (for example here), but I can't find one where the error occurs from within eclipse, but not the terminal.
Any pointers would be greatly appreciated. I suspect it may be to do with an environment variable not being set, but not sure which or why. Based on answers on SO I added the DISPLAY
environment variable and set it to :0
in the project set-up but with no luck.
Update: I found a way to quickly add all of my bash environment variables to my eclipse project and I still get the No available video device
error.
Update 2: Turns out I wasn't adding environment variables in the right place (I was adding them to the C++ build environment, not the Run environment; which is obviously where the problem was giving application was building but not running). Posted answer below.
回答1:
It turns out it was an environment variable problem. To fix you need to add the environment variable DISPLAY
to your project and set it to :0.0
.
To do this right-click on your project, then select Run As >> Run Configurations...
Then select your application in the left-hand panel. Select the Environment tab on the right. Click New... and then enter DISPLAY
in the Name: field, and :0.0
in the Value: field.
Click Apply and then Run, and it should work. Thanks to @Anon Mail for pointing me in the direction of environment variables.
来源:https://stackoverflow.com/questions/43220318/c-executable-runs-from-command-line-but-not-from-eclipse