LinuxQuestions.org
Help answer threads with 0 replies.
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 03-31-2009, 06:23 PM   #1
iochinome
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 15
the process of compiling c/c++ programs in linux


hey guys, I am a newbie to both Linux and the forum. this = first of hopefully many!

so i am a bit of a programmer, and i am used to the ease of ide's in windows, like bloodshed, in which you just hit compile to compile a program. i totally do not understand the process in linux, though. everyone says i should use g++, but i dont even think it is installed. (and the way to find out is to use the terminal, which is just a bunch of jargon to me at this point.) when i type in 'g++' in the terminal, it says:

The program 'g++' can be found in the following packages:
*g++
*pentium-builder
Try: sudo apt-get install <select-package>
bash: g++: command not found

does that mean it is installed? i have no idea...

so if i want to make a hello world program, i just write out something like this in text edit:

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

then name it hello.c, then type into terminal
g++ hello.c
?
what the hell is a make file?

if you know of a good tutorial explaining this, that would be GREAT!
thanks!
 
Old 03-31-2009, 06:29 PM   #2
iochinome
Member
 
Registered: Mar 2009
Posts: 32

Original Poster
Rep: Reputation: 15
oh yeah, i am running the latest version of ubuntu, whatever that is.

thanks!
 
Old 03-31-2009, 06:31 PM   #3
deathalele
Member
 
Registered: May 2008
Location: deep dark north wales
Distribution: Zenwalk
Posts: 162

Rep: Reputation: 30
I'm guessing your using ubuntu?
to install g++ do
Quote:
sudo apt-get install g++
also installing build essentials would be a good idea
Quote:
sudo apt-get install build-essentials
that will install most of the necessary libraries you will need

report back so i can see how it went
 
Old 03-31-2009, 06:41 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by iochinome View Post
so i am a bit of a programmer, and i am used to the ease of ide's in windows, like bloodshed, in which you just hit compile to compile a program. i totally do not understand the process in linux, though. everyone says i should use g++, but i dont even think it is installed. (and the way to find out is to use the terminal, which is just a bunch of jargon to me at this point.) when i type in 'g++' in the terminal, it says:

The program 'g++' can be found in the following packages:
*g++
*pentium-builder
Try: sudo apt-get install <select-package>
bash: g++: command not found

does that mean it is installed? i have no idea...
"command not found" seems to suggest so

You need to install g++, yep.

Quote:
so if i want to make a hello world program, i just write out something like this in text edit:

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

then name it hello.c, then type into terminal
g++ hello.c
?
You should really adopt another extension for c++ files. Files ending in ".c" are supposed to be C source files, and not C++ source files. And while we are at it, C is not C++ either, nor the oposite.

Quote:
what the hell is a make file?
A Makefile contains directives which tells your compiler how different source files are interrelated, and how to compile them, what flags to use for each one, etc. etc. etc. There're lots of things that a Makefile can be used for, but it all comes to simply how to maintain and compile your project. When you have a project with 3000 source files it becomes a little complicated to compile them one by one and remember how to link all of them together. If you have a Makefile, you can just type "make", press enter and forget about it. It will also take care of recompiling only the files which needs to be recompiled, taking into consideration the date of the last modification of the source file(s) vs. the creation date of the correspondent object file, and taking into consideration interdependencies as well.

They are like projects, but in my humble opinion, more versatile.

If you are done to IDEs, there're many, but I suggest you to learn at least the basics of gcc and g++ before using anything else.

There are thousands of tutorials on how to write a makefile, I don't think you will have problems finding that.
 
Old 03-31-2009, 09:56 PM   #5
iochinome
Member
 
Registered: Mar 2009
Posts: 32

Original Poster
Rep: Reputation: 15
okay, so it seems that g++ is up and working.
so then, i tried compiling this program:

#include <iostream>
using namespace std;

int main() {cout << "hello world!"; return0;}

under the name hello.c
i put in to the command line
g++ -o hello hello.c

and lo and behold, upon the desktop a file called 'hello' appeared, with no extension or anything. i tried clicking on it, nothing. i tried putting in to the command line
hello
nothing. whats up?
thanks for all the help!

oh, i just got it. you have to type in
./hello
is that the same for all programs? and is this called an 'executable?'
thanks!

Last edited by iochinome; 03-31-2009 at 09:59 PM. Reason: found something out
 
Old 03-31-2009, 10:13 PM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,063

Rep: Reputation: 381Reputation: 381Reputation: 381Reputation: 381
Quote:
Originally Posted by iochinome View Post

oh, i just got it. you have to type in
./hello
is that the same for all programs?
Yes. As long as they directory where they reside is not in your $PATH. By default, and unlike in DOS and Windows, the current directory "." is not in your $PATH, which means that you will have to tell your shell where the binary lives, you can do so using a relative path, or an absolute one.

For example, if your program lives in /home/jochinome/ you could do this to run it:

Code:
/home/jochinome/hello
As if you are already in /home/jochinome, you can do as you did:

Code:
./hello
Which means "run the file hello which lives on the current directory".

Alternatively you could also put this file in /usr/local/bin or another location that is on your path. To see your current path you could use this:

Code:
echo $PATH
To add a directory to your path you can add this line into ~/.bashrc and ~/.bash_profile (create them if they do not exist, it's perfectly normal):

Code:
export PATH="$PATH:$HOME/bin"
Then restart your shell, open a new one, or just run "exec bash" to reload this config files. Then create the bin/ directory inside your home, and from that moment, every executable file in that directory can be run by just invoking its name, just like any other system tool.

Feel free to ask for clarification if you need so.

Quote:
and is this called an 'executable?'
Yes. That's a binary program file which I guess is what you wanted to ask. About it being executable, well, "executable" is just an attribute of files, you can turn it on/off for any file (another things is if that will actually be useful, that depends on the file). We use the chmod command for that. But that's for another thread.

There are lots of different executable files in linux, some are binary object code like those produced by C and compilers in general, just like the .exe files. Some others are scripts of any kind (perl, shell scripts which can also be of many types, lisp, ...).

Postscript: I really don't want to sound like a purist smartass of anykind, but you really should correct the habit of using .c for c++ sources, in the future you are going to have problems if you stick to that in both IDEs and with gcc/g++. The compiler will assume that you are using c syntax if you use .c as the file extension, and that will confuse it. You can read all the extensions that the compiler accepts in the man page, look this string into the man page, and it will show you:

Quote:
input file

Last edited by i92guboj; 03-31-2009 at 10:18 PM.
 
  


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
Compiling programs for Windows on Linux bugscripts Linux - Software 24 03-10-2008 10:27 PM
Changing the process name for java programs chandru.in Programming 9 06-02-2007 05:23 AM
why is compiling source programs under linux such a headache? guy24x Linux - Software 21 07-28-2005 07:04 AM
How does one launch programs 'out of process' in a terminal window? DKnight Linux - General 5 06-07-2003 11:11 PM


All times are GMT -5. The time now is 04: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