PDA

View Full Version : [SOLVED] C++ in Linux



Hoom@n
February 5th, 2008, 04:18 PM
Hello!
I write some command-line applications with c++(like acm questions) In windows I had tree choices:

Open a console application in Visual Studio
Write it in Borland c++ for Dos
Write it using a text-editor and run it with g++

How can I write a console c++ application in linux? (my program should have standard IO, not buttons)
Thanks.

emarkd
February 5th, 2008, 04:29 PM
#3 is the standard way of doing it in Linux and it works fine. g++ is already installed in Ubuntu.

If you're a GUI/IDE type of programmer, try Anjuta. It's in the repositories.

Cypher
February 5th, 2008, 04:44 PM
If you want to compile and run your program entirely in the command line, then you can use a text editor like GEDIT for basic stuff or if you want to go hardcore, Emacs as your code editor and then use the standard G++ command to compile your code..

Hoom@n
February 5th, 2008, 04:44 PM
Thanks you. But how can I compile my program? e.g I have program.cpp and I wanna know how to compile it with g++ in termnal. Please tell it. Thanks.

LaRoza
February 5th, 2008, 04:45 PM
http://ubuntuprogramming.wikidot.com/

Moved to Programming Talk

Cypher
February 5th, 2008, 04:52 PM
Well the way to compile your program depends on what you are coding..if you are doing something as simple as..


#include <iostream>

int main(void)
{
cout << "Hello World" << endl;

return 0;
}

Then you can compile this code easily with


g++ -o hello hello.c


If you have multiple files, then you'll want to create .o of them all and then compile them together using any specific libraries you need.

For anything more than a single file, you really should look into Makefile. If you aren't interested in making Makefile, most IDE's like KDevelop, Ajunta will create one for you and you can uese these IDEs to create your console applications..

Hoom@n
February 5th, 2008, 05:20 PM
Thanks, but what is -o(in the first command)?

Majorix
February 5th, 2008, 05:44 PM
Thanks, but what is -o(in the first command)?

It is used to link object files.

Cypher
February 5th, 2008, 05:55 PM
Thanks, but what is -o(in the first command)?
"-o" is Output file name. If you don't use that, the default output name of "a.out" is used.

Doing "man g++" will explain all the flags..

Hoom@n
February 5th, 2008, 08:11 PM
Thank you, so to be sure: "g++ -o <output> <input>" and it'll generate output.exe as output, Am I right?
+++
How can I see all the warnings?
____
Thanks.

aks44
February 5th, 2008, 08:17 PM
How can I see all the warnings?

There are many switches to enable warnings, from the top of my head I can only remember -Wall (which contrary to what it seems, doesn't enable *all* warnings)

You should check the g++ manual:

man g++

Hoom@n
February 5th, 2008, 09:33 PM
Thank you. First of all I hadn't g++ installed, so I installed it by apt-get. And second:
This is a normal program:

#include<iostream>
using namespace std;
int main()
{
cout<<"hello";
int i;
cin>>i;
return 0;
}
But:

hooman@hooman-laptop:~$ cd Desktop
hooman@hooman-laptop:~/Desktop$ g++ -o hello hello.cpp
hooman@hooman-laptop:~/Desktop$ sh hello
hello: 1: Syntax error: "(" unexpected

What's the problem?
Thanks.

aks44
February 5th, 2008, 09:37 PM
Run it using ./hello rather than sh hello (sh is intended to execute scripts, not compiled programs).



EDIT: how did you install g++? On Ubuntu you'd better install it through the build-essential package or else be prepared for troubles...

Hoom@n
February 5th, 2008, 11:08 PM
Thank you.
+++
sudo apt-get install g++ and then it installed g++ from my ubuntu cd. Is there any problem? Really I don't like to have a new problem!

LaRoza
February 5th, 2008, 11:09 PM
Thank you.
+++
sudo apt-get install g++ and then it installed g++ from my ubuntu cd. Is there any problem? Really I don't like to have a new problem!

sudo aptitude install build-essential

Hoom@n
February 5th, 2008, 11:27 PM
Thank you. I did it! Is there any problem now?

BoazAdreal
February 11th, 2008, 09:29 AM
Hey I am a bit new to programming but I am running into a IOStream not found error when I attempt to compile the code you have provided. I have GCC 4.1 and 4.2 installed, but I had the same thing occuring when I had it not installed. Any suggestions would be much appreciated.

Caduceus
February 11th, 2008, 12:34 PM
Are you doing

#include <iostream>

Or

#include <IOStream>

The first one is correct, C++ is dodgy regarding upper and lower case letters.

HueyLuey
February 11th, 2008, 02:35 PM
I'd recommend using an IDE such as Eclipse with CDT. It provides a half decent editor and provides a wrapper over g++ with an easier to use interface for build settings etc.

BoazAdreal
February 11th, 2008, 07:40 PM
Are you doing

#include <iostream>

Or

#include <IOStream>

The first one is correct, C++ is dodgy regarding upper and lower case letters.

The first one. It's the same thing as Java from what I had remembered for case tense.

aks44
February 11th, 2008, 07:49 PM
The first one is correct, C++ is dodgy regarding upper and lower case letters.

The C++ language doesn't care, it directly passes the filename to the underlying filesystem.

Which means: on case-sensitive filesystems (*nix) the compiler will whine; on case-insensitive filesystems (Windows) it will compile fine.

Again, it has nothing to do with the C++ language or the compiler... ;)

leileicats
February 12th, 2008, 07:29 AM
Thank you, so to be sure: "g++ -o <output> <input>" and it'll generate output.exe as output, Am I right?
+++
How can I see all the warnings?
____
Thanks.

Using "g++ -Wall -o <output> <input>" will show the detailed warnings and error.

Hoom@n
February 12th, 2008, 04:03 PM
Thank you.