LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-16-2011, 08:38 PM   #1
LaserGuided
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
Cannot run C++ executable file from Ubuntu 10.10 GUI


I can compile and run this C++ file from the command line, but when I double click on it from the GUI, nothing happens.

Code:
#include <iostream>

int main()
{
    std::cout << "\n Hello GNU/Linux!\n";

    int foo = 0;

    std::cin >> foo;

    return 0;
}
The permissions for the file are as follows:
Code:
-rwxrwxr-x 1 jimmy jimmy 7674 2011-04-16 20:06 main
 
Old 04-16-2011, 09:08 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

Keep running it from a shell; chances are it starts & finishes
before you notice anything.



Cheers,
Tink
 
Old 04-17-2011, 01:49 AM   #3
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7
Posts: 872

Rep: Reputation: 112Reputation: 112
You issue is what Tinkster said. You can't just double click it and see it. It is better to run the program in a shell. Once you made the file exacutable then you cd to the directory and ./filename. But if you still want to double click it put this code in right befor the return statement

Code:
char response;
std:cin >> response;
So it would look like something like this

Code:
#include <iostream>

int main()
{
    std::cout << "\n Hello GNU/Linux!\n";

    int foo = 0;

    std::cin >> foo;
    
    char response;
    std:cin >> response;

    return 0;
}
 
Old 04-17-2011, 09:22 AM   #4
LaserGuided
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for trying to help me solve this mystery.

I forgot to mention the file property "Allow executing file as program" is checked.

I'm not a C++ newbie. I'm a Linux newbie. I've been developing software on Windows. I now have a PC with Ubuntu and I'm making my software portable. I would like to understand why my C++ executable files do not execute from the Graphical User Interface on my Ubuntu machine. If I don't understand why this sample file doesn't execute from the desktop, I cannot get my real programs to execute from the desktop.

I included a variable (foo) and a request for input (cin) to prevent the program from closing without my input.
Including another variable (response) and a second request for input did not change anything.
jmc1987 - I compiled the program with your changes and I get the same results.

Last edited by LaserGuided; 04-17-2011 at 09:32 AM.
 
Old 04-17-2011, 09:38 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by jmc1987 View Post
But if you still want to double click it put this code in right befor the return statement
Did you really mean to say that adding a second read from std::cin would cause a wait for user input when the first read from std::cin did not?

I don't know the answer to the original question. But I think it might help to add a related question:

When you run a program by double clicking from the Ubuntu GUI, what is connected to std::cout and std::cin for that program?

I don't even fully understand how that works in Microsoft Windows. Sometimes in Windows, the OS decides that the program you are starting needs a terminal window and lacks one, so the OS creates a terminal Window for that program. If the program just writes to cout and exits, that terminal window may vanish before you see it. But if you read from cin, the terminal window waits.

In Linux, I don't think the OS itself gets involved in decisions like that. The initial cin and cout of a child process are the responsibility of the parent process. I don't know what conditions if any would cause a GUI parent process to create a terminal window for the cin and cout of a child process.

Last edited by johnsfine; 04-17-2011 at 09:39 AM.
 
Old 04-17-2011, 12:29 PM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by johnsfine View Post
When you run a program by double clicking from the Ubuntu GUI, what is connected to std::cout and std::cin for that program?
Exactly. I also wanted to ask that.

Quote:
Originally Posted by LaserGuided View Post
I forgot to mention the file property "Allow executing file as program" is checked.

I'm not a C++ newbie. I'm a Linux newbie. I've been developing software on Windows. I now have a PC with Ubuntu and I'm making my software portable. I would like to understand why my C++ executable files do not execute from the Graphical User Interface on my Ubuntu machine.
This is the kind of thing you should really use the command line for.

And you shouldn't be afraid of it - it's very different from the DOS prompt. It's much more powerful and easier to use, and it's not considered a relic of the past that should be avoided. In fact, most intermediate-advanced Linux users (including me) use the command line every day, not because Linux has poor GUIs (which it doesn't), but because many things are easier in the command line.
 
Old 04-18-2011, 08:54 AM   #7
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2
Posts: 104

Rep: Reputation: 21
What do you mean by GUI? Is it simply a desktop entry pointing to the executable? If so, there is nowhere for the output to go, since your application does not create a window.. The alternative is to run it in a terminal. I do not know Ubuntu, but this is how you would do it using KDE under OpenSuse.

Right click on the desktop, select "create new" then "Link to application". A box appears with four columns: select "Application" and enter under "Command" the complete path to your executable; this may be what you have done, in effect. Now open "advanced options" and select "run in terminal" and "do not close when command exits"; this last will allow you to look at the output.
 
Old 04-18-2011, 09:22 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by Peverel View Post
What do you mean by GUI? Is it simply a desktop entry pointing to the executable?
I'm pretty sure it's an executable, not a desktop file. Do you think that the OP would make a desktop file for a command line "Hello World" program, or that the compiler would make one?
 
Old 04-18-2011, 09:28 AM   #9
Noway2
Senior Member
 
Registered: Jul 2007
Distribution: Ubuntu 10.10, Slackware 64-current
Posts: 2,124

Rep: Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778
As an alternate perspective, you may wish to learn about programming for Gnome / X. If you are familiar with the GUI Builders, like visual basic or visual C++ you will find the Linux method to be very similar. Of course, this isn't to say that you must use a builder tool, you are free to develop the GUI applications from source only, which in my opinion is far easier on Linux than it is on Windows. Many of the concepts are the same in both environments: creating objects that are derived from a base class and registering a call back function that responds to signal such as mouse clicks. If this notion appeals to you, do a search for GTK++ development tutorials.
 
Old 04-18-2011, 09:37 AM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by Noway2 View Post
As an alternate perspective, you may wish to learn about programming for Gnome / X. If you are familiar with the GUI Builders, like visual basic or visual C++ you will find the Linux method to be very similar. Of course, this isn't to say that you must use a builder tool, you are free to develop the GUI applications from source only, which in my opinion is far easier on Linux than it is on Windows. Many of the concepts are the same in both environments: creating objects that are derived from a base class and registering a call back function that responds to signal such as mouse clicks. If this notion appeals to you, do a search for GTK++ development tutorials.
I would recommend against GNOME development, becasue many more advanced Linux users do not like it when applications depend on having parts of a desktop environment that they don't use installed. Just GTK+ is fine, though. It does not depend on GNOME.

You can also check out Qt. In my opinion it's easier to work with and has a much better GUI builder. (Note that there is a common misconception that Qt depends on KDE, this is completely false. Just like you can choose to write a GTK+ application without GNOME, you can choose to write a Qt application without KDE).

Last edited by MTK358; 04-18-2011 at 09:39 AM.
 
Old 04-18-2011, 12:55 PM   #11
Noway2
Senior Member
 
Registered: Jul 2007
Distribution: Ubuntu 10.10, Slackware 64-current
Posts: 2,124

Rep: Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778
Thank you for the clarification. I had a misconception about the link between GTK and Gnome, just as I thought that in order to use QT one had to run KDE. According to wikipedia, Gnome is built upon GTK, but as you pointed out it is better to write GTK or QT applications rather than Gnome or KDE applications if at all possible. Another thing to consider is that other window managers, e.g. Fluxbox, XFCE, and Windowmaker, are rapidly becoming more popular and hence it is best to write your applications for maximum compatibility.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Make a file executable and then run mostafaashish Linux - Newbie 7 02-20-2014 06:34 AM
Run an executable file ( created from Qt) post installation (linux/redhat). udaypratapyati Linux - Newbie 2 02-26-2011 02:19 AM
[SOLVED] Using variable to run an executable file mahmoodn Linux - General 3 05-27-2010 07:17 AM
can't run the executable file after compiling with g++ compiler moondog1129 Linux - Laptop and Netbook 2 02-06-2006 10:24 PM
Cannot not run executable jar file after set content type for download antony_csf Programming 1 12-08-2004 01:22 AM


All times are GMT -5. The time now is 06:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration