LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Linux Answers > Programming
User Name
Password

Notices


By hylke at 2004-05-02 03:24
First thing we need to do is make a .cpp file.

Type in a shell:
vi HelloWorld.cpp
That means create a file HelloWorld.cpp and then edit it.
Type i to insert text.
Type Esc w q to save the file.

You can also use any other editor you like.

HELLO WORLD EXAMPLE
Here comes the source code:
Code:
#include <iostream>

using namespace std;

int main()
{
cout << "Hello World \n";
return 0;
}
#include <iostream> Means, include the iostream header so we can use cout to print out our text.
using namespace std; means, use the standard namespace.
You can also delete it and add std:: before cout.
int main() means this our main function, of the type integer.C++ automatically jumps to the main function and executes every line what's between { and }.
cout << "Hello World \n"; Means: print Hello World on the screen and end the line(\n).What i mean with end the line, if use another cout to print text it would appear on the next line, if you didn't do that it would appear next to it.The ; means, and of our cout line.
return 0; Means: return the value 0 to the function.It isn't really important to understand what it does, but you have to know that its necessary.

COMPILING THE SOURCE CODE
Now we're going to compile our source code using the GNU g++ compiler.
Take care you're in the directory with HelloWorld.cpp in it.
Then type:
g++ HelloWorld.cpp -o HelloWorld
It means compile and link HelloWorld.cpp to an executable called HelloWorld.
Now HelloWorld is ready to be executed and Hello World will appear in your shell.

VARIABLES AND INPUT
If we want input from the user we need a variable to store our input in.There are lots of different variables, but to keep it simple we only use the string type.
Here's our source code:
[code]#include <iostream>
#include <string>

int main()
{
string name;
cout << "Type in your name: ";
cin >> name;
cout << "Hello " << name << "! \n";
return 0;
}

Our first new line is #include <string>, that means include an header called string to let us use a string variable.
The first line in our main function is string name;.
That means: make a string variable called name to store our input in.

cin << name; means: Put the input of the user in the variable called name(we use created it).
The next line prints first Hello, then the name you typed and then !.
Now it probably makes sense why we use " and " to print our text in and not without the "s, because that may confuse the compiler.Is it a variable or just text?

So that's my short howto. Have fun with it. If you have any questions email to hylke@linux.net

by the_gripmaster on Fri, 2004-07-09 17:41
This article does not say how you execute the binary "HelloWorld". You do it by typing the following in the shell (dont type %, it is the shell prompt) and hitting enter or return:

% ./HelloWorld

by johnnyICON on Thu, 2004-09-30 10:42
I have heard of Oracle 9, not too sure what it is but from what I've read it's a C++ developer application right?

What other C++ applications are available, and which would you suggest?

by Tinkster on Thu, 2004-09-30 18:23
Oracle is a database engine/server that happens to have
APIs ... it's not a C++ development tool.


Cheers,
Tink

by Donkey3000 on Fri, 2004-10-01 05:39
Yeah, Oracle is a database like mysql and ms access. However there is some higher level (4gl) development tool for and by Oracle. So maybe that got you confused.
---

I think this article or how to was a bit short. I clicked on it to find information on how to start programming for Linux.

I would have like to read not a simple sample of hello world as that's basically the same on any platform. But I wanted to know whats better KDE(QT) or GNOME (GTK) and what frameworks and libs are good and from a developers point of view but also from a end users point of view.

Basically what I need to start working on a modern application for Linux and the pro's and cons of certain frameworks.

by cryptowicked on Fri, 2004-12-03 07:31
how do you exit out of the edit file? I write the source and then i save it, but how do i exit back into the user shell?
--noob tryin to rtfm--
cryptowicked-

by beliavsky on Sun, 2004-12-12 08:58
The description of your tutorial says:

"I've seen the howto of crabboy so i thought, maybe its fun to make one in c++. This is just some verry basics of c++. Suchs ass printing text, some variables and stuff like that."

There are many English mistakes in this. "its" should be "it's". "This is" should be "These are". "verry" should be "very". "suchs" should be "such". "ass" should definitely be "as"! "c++" should be "C++". Here is a more concise and correct version:

"I've seen the howto of crabboy and thought it would be fun to make one in C++. These are just some basics of C++, such as printing text, some variables and stuff like that."

I am trying to be helpful. People are more likely to spend time on your content when your grammar, spelling, and general usage of English are adequate.

by akhot on Tue, 2005-01-18 03:04
One quick way to build is ( given the source file is tst.cpp )

$ make tst

g++ tst.cpp -o tst

make is intelligent about what to do now ( it will invoke the g++ compiler ).
This works on almost all unix platforms.

If you want to pass options use the CXXFLAGS make variable

$ make tst CXXFLAGS=-g
g++ -g tst.cpp -o tst

Now if you are using vim for editing the files, this becomes even simpler:

$ vim tst.cpp

:make tst

this invokes the compiler for you.

To run it just come out of vim with a Ctrl-Z

./tst

$ fg

takes you back to your editing session.

by partha6794 on Tue, 2005-11-01 11:39
There is a easy method to bulid "C" & "C ++ " programming:

There are three methods to bulid "C" & "C++" programming in RHL 9.0 & FC .by using GNOME desktop & another in KDE .
There are mainly two editor in GNOME , viz. vi- editor & another is emacs editor.I will discuss both here.



using VI-editor:

1. open terminal .
2. type for C programming
vi <filename>.c

for C++
vi <filename>.cpp

e.g:

vi hello.c { for C}
vi hello. cpp { for C++}

3. then type " i" ( i= insert)
4. then write source code . for details www.howstuffworks.com how to works "C" programming?
5. then press
esc : wq
it is save now.

6. now for comile the programme type,
cc <filename>
7. then for execute type
(dot)/a.out ( dot = . )

8. then it will see your screen.


USING EMACS EDITOR:

1.type emacs<file name> on terminal.
3. then type " i" ( i= insert)
4. then write source code . for details www.howstuffworks.com how to works "C" programming?
5. then press
esc : wq
it is save now.

6. now for comile the programme type,
cc <filename>
7. then for execute type
(dot)/a.out ( dot = . )

8. then it will see your screen.


USING KWRITE IN ( GNOME OR KDE DESKTOP)

1. Open terminal & type kwrite
2. then write source code
3. then click file .
4. then clck save as
5. save the file wtth extension .c/.cpp
now for comile the programme type,
cc <filename>
7. then for execute type
(dot)/a.out ( dot = . )


I hope it will helpful .


If you face any diffuculty u can email me
partha_26786794123@yahoo.com , ( for alert reply here)

partha

by Ygrex on Fri, 2005-11-04 05:53
IMHO this article is intended for complete newbies in the C++ development. So I
think it is good to explain how to create a Makefile and why almost any source
one can download will contain this file (sometimes after configure executing).

by fakie_flip on Tue, 2006-01-17 19:12
Can someone tell me a guide or howto for making c++ programs in linux that use the qt library for graphics. I want to make some c++ programs in linux with gui. What are the commands to compile a c++ program that uses qt?


  



All times are GMT -5. The time now is 09:41 AM.

Main Menu
Advertisement
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