LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   compiling c,c++ programs (https://www.linuxquestions.org/questions/linux-newbie-8/compiling-c-c-programs-103107/)

ksd 10-12-2003 01:27 PM

compiling c,c++ programs
 
hey does any one know how to compile and run c,c++ programs in linux.i am confident that the gcc compiler is loaded into my system while i was installing linux.i wanna work out on c..some one help me out plz...

Mara 10-12-2003 02:24 PM

The command to compile your program is
gcc -o executablename filename.c
You can also use the shorter version:
gcc filename.c
(it produces a.out file)

ksd 10-13-2003 02:37 AM

hey i did not get it exactly.initially
i will open a new program using vi new1.c
then,i write the code and compile it like this ..

gcc new1.c
then what is a.out?please be more specific.

hw-tph 10-13-2003 02:45 AM

a.out is what the output executable file will be called unless you specify a name for it with the -o switch, as in Mara's example.

hw

ksd 10-13-2003 03:03 AM

hey every one is being obscure in giving their replies. can you be more clear?i opened the console and
did

vi dhhh.cpp
then with in the vi editor,i typed the code and pressed ctrl+alt+z ,came out and then compiled
gcc -o dh.out dhhh.cpp
when i run the above command, i get a n error saying


Code:

root@dheeraj:~# gcc -o dh.out dhhh.cpp
gcc: dhhh.cpp: No such file or directory
gcc: no input files
root@dheeraj:~#

now what do i do ??

megaspaz 10-13-2003 03:19 AM

Quote:

Originally posted by ksd
hey every one is being obscure in giving their replies. can you be more clear?i opened the console and
did

vi dhhh.cpp
then with in the vi editor,i typed the code and pressed ctrl+alt+z ,came out and then compiled
gcc -o dh.out dhhh.cpp
when i run the above command, i get a n error saying


Code:

root@dheeraj:~# gcc -o dh.out dhhh.cpp
gcc: dhhh.cpp: No such file or directory
gcc: no input files
root@dheeraj:~#

now what do i do ??

what does ctrl-alt-z do in vi? i honestly don't have any idea. the way i learned how to save a file in vi is when you're done editing, press the esc key to get into command mode. then type ":" to enter the command, "wq". there's no quotes around the vi commands. then run the gcc command givent to you by mara.

when you want to run the compiled program use the command:
./dh.out

if you're using the command in the quote.
also, btw, get out of root and use a user account.

rid00z 10-13-2003 04:53 AM

*.cpp is c++ code... so use g++

do this:

g++ XXX.cpp //this compiles the code

./a.out //this is the program produced by the compile

to change the output file

g++ XXX.cpp > newbie

./newbie

praveenk 10-13-2003 06:22 AM

I don't have an idea of Ctrl+Alt+Z in Vi. I think that it would suspend the process. Ctrl+Z! So, save your file in vi by using the command :wq and then try compiling it.

My advice to newbies is that go for simple editors like pico, nano, jed intially or spend some time in reading the help for vi or emacs.

If your program is hello.c, then

gcc hello.c
./a.out

(or)

gcc hello.c -o hello
./hello

If your program is a C++ program, hello.cc

g++ hello.cc
./a.out

(or)

g++ hello.cc -o hello
./hello

ksd 10-13-2003 07:59 AM

hey thanx praveenk for your reply.i have already implemented my first c++ program on my slack..
thanx all....btw i am beginning to feel vi is not a very good editor for newbies..so i will better try out ....
some other editors....emacs.....

praveenk 10-13-2003 08:23 AM

Hey Emacs is more painful than Vi and also more powerful too. Emacs learning curve is more than that of Vi. My suggestion is that joe, jed, nano, pico are suitable for newbies.


All times are GMT -5. The time now is 05:35 PM.