LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   the process of compiling c/c++ programs in linux (https://www.linuxquestions.org/questions/linux-newbie-8/the-process-of-compiling-c-c-programs-in-linux-715889/)

iochinome 03-31-2009 05:23 PM

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!

iochinome 03-31-2009 05:29 PM

oh yeah, i am running the latest version of ubuntu, whatever that is.

thanks!

deathalele 03-31-2009 05:31 PM

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

i92guboj 03-31-2009 05:41 PM

Quote:

Originally Posted by iochinome (Post 3494205)
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 :p

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.

iochinome 03-31-2009 08:56 PM

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!

i92guboj 03-31-2009 09:13 PM

Quote:

Originally Posted by iochinome (Post 3494373)

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


All times are GMT -5. The time now is 11:02 PM.