Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

c++ executable runs from command line, but not from eclipse

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...