LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-31-2008, 09:38 PM   #1
swish
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Rep: Reputation: 0
How do I run a C++ console app in linux?


I am learning C++. I used Windows Vista and Visual C++ Express edition.
But I read in some article that linux is better to program in. So I got Ubuntu installed and an eclipse IDE for C++. But it doesn't run anything.

When I tried to run this I got an error saying cannot find "iostream" :

#include <iostream>
using namespace std;

int main()
{
cout << "hello world";
return 0;
}


What would I have to do to compile and run it?
My vista is on another partition and when I access it, while in Ubuntu, and try to run the .exe applicationss I created with VC++ nothing works?

Any help is appreciated.

Last edited by swish; 12-31-2008 at 09:42 PM.
 
Old 12-31-2008, 10:47 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by swish View Post
...
My vista is on another partition and when I access it, while in Ubuntu, and try to run the .exe applicationss I created with VC++ nothing works?

Any help is appreciated.
It's probably too early for you to learn programming if you are blindly trying to run an application compiled for one OS to run under another OS.

I suggest to first learn what in general is compilation, linking, loading, executing, how binary file formats differ, what are file permissions under Windows and under UNIX, what in the end is executed, what are basic OS functions WRT programs, etc.
 
Old 12-31-2008, 11:57 PM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quoth Sergei Steshenko:
Quote:
It's probably too early for you to learn programming if you are blindly trying to run an application compiled for one OS to run under another OS.
But he's not trying to run an application previously compiled for any OS at all. He's trying to compile it in Linux, as he said:
Quote:
What would I have to do to compile and run it?
Edit: Actually, Sergei, you're right. He was trying to blindly run those exe's. swish, don't try to run those exe's. But you're on the right track when you realize that you have to "compile and run it". That will involve making a new compiled executable for Linux; your Microsoft exe's are no good here.

swish, I've never used an IDE under Linux; I'm a command line kinda guy. But run this as a shell script, and it should compile and run your C++ program. If it doesn't compile, then it's a good bet that your compiler isn't installed properly. But it probably will compile and run:
Code:
#!/bin/bash

cat > 4.cpp <<EOD
#include <iostream>
using namespace std;

int main()
{
  cout << "hello world\n" ;

  return 0;
}
EOD

g++ 4.cpp -o 4
./4
Note that I've made one change to the source. It's in red.

Hope this helps.

Last edited by wje_lq; 01-01-2009 at 12:03 AM. Reason: Oops. Didn't read things carefully enough.
 
Old 01-01-2009, 12:19 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by wje_lq View Post
Quoth Sergei Steshenko:

But he's not trying to run an application previously compiled for any OS at all. He's trying to compile it in Linux, as he said:


Edit: Actually, Sergei, you're right. He was trying to blindly run those exe's. swish, don't try to run those exe's. But you're on the right track when you realize that you have to "compile and run it". That will involve making a new compiled executable for Linux; your Microsoft exe's are no good here.

swish, I've never used an IDE under Linux; I'm a command line kinda guy. But run this as a shell script, and it should compile and run your C++ program. If it doesn't compile, then it's a good bet that your compiler isn't installed properly. But it probably will compile and run:
Code:
#!/bin/bash

cat > 4.cpp <<EOD
#include <iostream>
using namespace std;

int main()
{
  cout << "hello world\n" ;

  return 0;
}
EOD

g++ 4.cpp -o 4
./4
Note that I've made one change to the source. It's in red.

Hope this helps.
The OP complained:

Quote:
When I tried to run this I got an error saying cannot find "iostream"
.

He probably doesn't have development packages installed.
 
Old 01-01-2009, 12:30 AM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
He probably doesn't have development packages installed.
Most likely. But it could be simply that the IDE is not correctly installed. The bash script would help determine which of those situations is true.
 
Old 01-01-2009, 03:04 AM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
He might not have the right path set for the includes also. If cpp runs, one would expect the basic C++ includes to be present. Of course, we don't know that he actually tried to compile with cpp; he might have used gcc.

The command "locate iostream" should shed some light on the subject.
 
Old 01-01-2009, 04:10 AM   #7
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
Have you tried reading the manual of Eclipse or gcc at all?
 
Old 01-01-2009, 05:12 AM   #8
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
IMHO, if you are just starting to learn to program in C or C++, you shouldn't work in an IDE. You need to learn the g++ command and options, and creating make files on your own. The IDE would do this for you and do it so generally, with so many options, that it will be impossible to fathom for a beginner. You need to learn to swim before deep sea scuba diving.

It is ironic that making things easier, adds a lot of complexity.
 
Old 01-01-2009, 10:27 AM   #9
swish
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
I think I figured out what the problem is.
Eclipse said it needed java to run.
I went to the java site and all of those were for other distros(redhat), I have Ubuntu. So I used Synaptic to install another java runtime. When I started eclipse it gave me an error something like this, "Eclipse *version blah* has never been tested on this java platform.Continue?" I didn't take that seriously I guess and thats probably why it won't compile right.


Quote: Sergei Steshenko:
Quote:
It's probably too early for you to learn programming if you are blindly trying to run an application compiled for one OS to run under another OS.
The only .exe I tried to run were like dead simple console apps. Ex. "Hello World".

But I realized that I have to recompile apps to run it on linux.
So after some research...this is my notion:

.exe files compiled in Win32 architecture won't run in linux? But if you get the source code and recompile it on linux it will run? Am I right?


Also, I really want to use command line(helps me think easier without all the unnecessary gui). But if I used command line what about error checking? I don't want it to run and just crash.

Can someone give me a website that explains using command line to compile and run, can't be too advanced(i am newbee)? Also, a website on what Sergie was talking about:

Quote:
Originally Posted by Sergie
I suggest to first learn what in general is compilation, linking, loading, executing, how binary file formats differ, what are file permissions under Windows and under UNIX, what in the end is executed, what are basic OS functions WRT programs, etc.
 
Old 01-01-2009, 10:50 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by swish View Post
...
Also, I really want to use command line(helps me think easier without all the unnecessary gui). But if I used command line what about error checking? I don't want it to run and just crash.
...

What is your definition of error checking ?

Why do you think IDE is different from command line WRT error checking ?

How are you going to differentiate between IDE bugs and your program bugs ?

How are you going to differentiate between whatever library your program uses bugs and your bugs ?
 
Old 01-01-2009, 11:03 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by swish View Post
...
Can someone give me a website that explains using command line to compile and run, can't be too advanced(i am newbee)? Also, a website on what Sergie was talking about:
http://www.apsu.edu/lij/06sm1010/handouts/g++ Tutorial.htm - just from a quick search using Yahoo.

Also use -Wall command line switch on g++ command line.
 
Old 01-01-2009, 12:02 PM   #12
swish
LQ Newbie
 
Registered: Dec 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Sergie I mean error checking as in...

cut << "Hello world";

Visual Studio would shoot out a message saying "cut" undeclared identifier..that one is a simple error.
But I am talking about large scale programs.

And uhm...
Quote:
http://www.apsu.edu/lij/06sm1010/handouts/g++ Tutorial.htm - just from a quick search using Yahoo.


I get the following error:

404 file not found.

Austin Peay State University

We're sorry but the page you requested could not be located. (404 error)
Our alphabetical site index is located: http://www.apsu.edu/atoz/


If you wish to contact the Webmaster for assistance, please include the address of the page that returned this error
 
Old 01-01-2009, 12:21 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by swish View Post
Sergie I mean error checking as in...

cut << "Hello world";

Visual Studio would shoot out a message saying "cut" undeclared identifier..that one is a simple error.
But I am talking about large scale programs.

And uhm...
[/B]

I get the following error:

404 file not found.

Austin Peay State University

We're sorry but the page you requested could not be located. (404 error)
Our alphabetical site index is located: http://www.apsu.edu/atoz/


If you wish to contact the Webmaster for assistance, please include the address of the page that returned this error
Copy-paste the whole URL into the browser window - there is a space in it, so the forum engine truncated the URL.

At all, use web search:

http://search.yahoo.com/search?p=g%2...p=mss&ei=UTF-8

- you should see the URL near the page bottom.
 
Old 01-01-2009, 12:27 PM   #14
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quoth swish:
Quote:
.exe files compiled in Win32 architecture won't run in linux? But if you get the source code and recompile it on linux it will run? Am I right?
Yes, and the bash script I posted earlier demonstrates how this can work.

Your source code doesn't do all the work itself. It depends on two other entities to help it:
  1. One entity is libraries. These contain code that you could just as well have written yourself and included as part of your code, but this saves you the trouble. They include math routines (sine, cosine, and so on), data formatting routines (for converting native floating point numbers into strings of characters which are printable) and many other things.
  2. Another entity is the operating system. This includes code that you couldn't simply incorporate into your own code, because the operating system won't normally allow you to do these things yourself. Things like doing actual I/O with ready-to-go arrays of bytes, getting your process ID, starting a child process, and exiting.
The way your compiled code requests the operating system to do things covered by that second item varies wildly from operating system to operating system. There are other reasons why your compiled Windows code won't run under Linux, but that reason is one of the most conspicuous.
Quote:
Visual Studio would shoot out a message saying "cut" undeclared identifier
The Linux compiler will do the same thing, and will not continue to produce a linked object file. These are known around here as "compilation errors".
Quote:
But I am talking about large scale programs.
If you have 500,000 compilation errors in your code, you'll see them all fly past you on the screen. When I'm confronted with that, I do the compilation command thus:
Code:
clear; g++ your options and stuff here 2>&1 | less
I do the "clear" because if you've been compiling and compiling and you get the number of error message lines down to, say, 10 or so, you won't have to squint at the screen to separate the most recent compilation errors from the ones in the previous compile.

In the "options and stuff", definitely include the -Wall option that Sergei recommended. Upper case W.

When you talk about "large scale programs", maybe you're not talking about handling zillions of error messages. Maybe you're talking about compiling subzillions of source files. The classic (for decades and decades) program for taking care of that is "make". Do this at the command line:
Code:
man make
Using make is not a black art and not rocket science, but it can present gotchas for the unwary, and there are many wrinkles to it. Don't rely on just the man page. scroogle the following search terms:
Code:
make tutorial
and knock yourself out.

I avoid make. My projects don't take that long to compile from scratch, so I just construct a bash script to do it all each time.

Oh. And.

Speaking of errors: I assume you're doing error checking on all your function calls, right? Full details for such error checking are found in the man pages. You know about man pages, yes? If you don't, then do this at the command line:
Code:
man man     # definitely do this one
man fork    # here's another example
This kind of error handling should be no different from what you do in Windows, though I have no experience in Windows.

Now as to the link that Sergei posted, the space in the original link showed itself as a larger problem, because the linuxquestions software left out the space and everything after it as part of the link. So if you use his advice, you'll have to copy and paste not just the link as shown in his post, but also everything up to and including the "htm". As an alternative, click on this link, lovingly repaired by hand.
 
  


Reply



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
where to post for help on Linux XP to run .exe app KerryWales Linux - Newbie 4 07-09-2008 08:12 PM
Getting a script to run X app through console Harlin Linux - Software 1 07-06-2007 10:38 AM
run linux app. on windows SlipAway172 Linux - Software 2 06-21-2005 08:51 PM
How to Run Win32 Console App in linux Console Lokie Linux - Software 2 08-12-2004 10:00 PM
run a linux app on windows Dr_G Linux - General 4 07-16-2001 08:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:13 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
Open Source Consulting | Domain Registration