PDA

View Full Version : Compiling and running C++ programs




thrill4rishabh
Jul 24, 2012, 11:56 AM
Hey,

I learnt C++ on Borland C++. Now I have shifted to mac and am using Eclipse, which is good, but not quite the same.

I read somewhere that we can write a program in TextEdit and compile and link it via terminal?

Can anyone tell me how to do that? Please go step by step and very detailed.

Thanks



wrldwzrd89
Jul 24, 2012, 12:14 PM
Here's the basic outline:

Make SURE you convert to plain text in TextEdit when writing code. Compilers and rich text format don't get along.
You'll need a C++ compiler, obviously - and it needs to be in your PATH environment variable to be usable in the Terminal. If you use Xcode's command line tools, this part is done for you.
Compiling and running is really easy:

compiler_name options program_name.ext
./program_name

...where the compiler_name and options depend on your compiler.

For Xcode, either GCC or Clang will work.

thrill4rishabh
Jul 24, 2012, 12:57 PM
Here's the basic outline:

Make SURE you convert to plain text in TextEdit when writing code. Compilers and rich text format don't get along.
You'll need a C++ compiler, obviously - and it needs to be in your PATH environment variable to be usable in the Terminal. If you use Xcode's command line tools, this part is done for you.
Compiling and running is really easy:

compiler_name options program_name.ext
./program_name

...where the compiler_name and options depend on your compiler.

For Xcode, either GCC or Clang will work.


My compiler is gcc, but what do you mean by options?

robvas
Jul 24, 2012, 01:01 PM
My compiler is gcc, but what do you mean by options?

enter 'man gcc' in a Terminal window - there's lots of options ;)

thrill4rishabh
Jul 24, 2012, 01:07 PM
enter 'man gcc' in a Terminal window - there's lots of options ;)

GCC(1) GNU GCC(1)

NAME
gcc - GNU project C and C++ compiler

SYNOPSIS
gcc [-c|-S|-E] [-std=standard]
[-g] [-pg] [-Olevel]
[-Wwarn...] [-pedantic]
[-Idir...] [-Ldir...]
[-Dmacro[=defn]...] [-Umacro]
[-foption...] [-mmachine-option...]
[-o outfile] [@file] infile...

Only the most useful options are listed here; see below for the
remainder. g++ accepts mostly the same options as gcc.

In Apple's version of GCC, both cc and gcc are actually symbolic links
to the llvm-gcc compiler. Similarly, c++ and g++ are links to
llvm-g++.

Note that Apple's GCC includes a number of extensions to standard GCC
(flagged below with "APPLE ONLY"), and that not all generic GCC options
:


Again, no idea what to do.

wrldwzrd89
Jul 24, 2012, 01:12 PM
GCC(1) GNU GCC(1)

NAME
gcc - GNU project C and C++ compiler

SYNOPSIS
gcc [-c|-S|-E] [-std=standard]
[-g] [-pg] [-Olevel]
[-Wwarn...] [-pedantic]
[-Idir...] [-Ldir...]
[-Dmacro[=defn]...] [-Umacro]
[-foption...] [-mmachine-option...]
[-o outfile] [@file] infile...

Only the most useful options are listed here; see below for the
remainder. g++ accepts mostly the same options as gcc.

In Apple's version of GCC, both cc and gcc are actually symbolic links
to the llvm-gcc compiler. Similarly, c++ and g++ are links to
llvm-g++.

Note that Apple's GCC includes a number of extensions to standard GCC
(flagged below with "APPLE ONLY"), and that not all generic GCC options
:


Again, no idea what to do.
The only option that you might want to use if your program consists of multiple source files is -o (output file). Example:
gcc -o myprog myprog.cpp myprog2.cpp myprog_funcs.cpp
Other than that, you don't need much in the way of options - the defaults are good for someone just learning, like you.

thrill4rishabh
Jul 24, 2012, 01:22 PM
enter 'man gcc' in a Terminal window - there's lots of options ;)

The only option that you might want to use if your program consists of multiple source files is -o (output file). Example:
gcc -o myprog myprog.cpp myprog2.cpp myprog_funcs.cpp
Other than that, you don't need much in the way of options - the defaults are good for someone just learning, like you.


I entered man gcc, but how to enter further commands? any commant to close the man command?

wrldwzrd89
Jul 24, 2012, 01:26 PM
I entered man gcc, but how to enter further commands? any commant to close the man command?
That's easy - type q to get out of the manual page viewer and back to a command prompt.

chown33
Jul 24, 2012, 01:29 PM
That's easy - type q to get out of the manual page viewer and back to a command prompt.

Or install Bwana and use your web browser to read man pages:
http://www.bruji.com/bwana/

wrldwzrd89
Jul 24, 2012, 01:29 PM
Or install Bwana and use your web browser to read man pages:
http://www.bruji.com/bwana/
Thanks for that link - that looks useful to programmers. Bookmarked.

thrill4rishabh
Jul 24, 2012, 01:35 PM
Thanks for that link - that looks useful to programmers. Bookmarked.


I'm sorry but I cant't seem to get it to work.

I wrote a simple hello world program in TextEdit and saved as hello.txt on desktop.

What exactly do I type in terminal.

chown33
Jul 24, 2012, 01:36 PM
Thanks for that link - that looks useful to programmers. Bookmarked.

I haven't used the 'man' command in years. Once Bwana is installed, it even recognizes command-line URLs, so this opens a man page in your browser:
open man:gcc

Pivs
Jul 24, 2012, 01:45 PM
I'm sorry but I cant't seem to get it to work.

I wrote a simple hello world program in TextEdit and saved as hello.txt on desktop.

What exactly do I type in terminal.

Save it as hello.cpp (plain text), compile with g++ and run.

g++ hello.cpp
./a.out

thrill4rishabh
Jul 24, 2012, 02:00 PM
Save it as hello.cpp (plain text), compile with g++ and run.

g++ hello.cpp
./a.out


hello.cpp: No such file or directory

Pivs
Jul 24, 2012, 02:03 PM
hello.cpp: No such file or directory

Make sure you rename hello.txt to hello.cpp and are in the same directory as hello.cpp when you run the g++ command.

thrill4rishabh
Jul 24, 2012, 02:08 PM
Make sure you rename hello.txt to hello.cpp and are in the same directory as hello.cpp when you run the g++ command.

Thanks man!! done finally!! But can anyone recommend an absolutely basic IDE, like Borland, in which i just type all the code and run the program?

SnowLeopard2008
Jul 24, 2012, 02:12 PM
Real programmers use VI. The problem you are having is that you need to change the .txt to .cpp. And next time, learn VI.
edit: too slow...
----------

Thanks man!! done finally!! But can anyone recommend an absolutely basic IDE, like Borland, in which i just type all the code and run the program?

Net beans? Btw, sorry for the double post. I was typing a bit slow.

wrldwzrd89
Jul 24, 2012, 02:17 PM
Thanks man!! done finally!! But can anyone recommend an absolutely basic IDE, like Borland, in which i just type all the code and run the program?
The most notable Mac C/C++ IDE options out there are Xcode, Eclipse (requires Java) and NetBeans (requires Java). I'm sure there are more I don't know about.

SnowLeopard2008
Jul 24, 2012, 02:20 PM
The most notable Mac C/C++ IDE options out there are Xcode, Eclipse (requires Java) and NetBeans (requires Java). I'm sure there are more I don't know about.

OP already tried Eclipse and it's not absolutely basic. Net beans is tho.

thrill4rishabh
Jul 24, 2012, 02:35 PM
Anyone here has some experience with Eclipse?

I have one problem. I can create and execute a program if:

1. File>>New>>C++Project>>Executable Files>>Hello World C++ Project.

But when I choose Empty Project I get lost.

I'm sorry. I'm totally confused and irritated. I am intermediate level in C++ ( I have done until dynamic memory allocation ), but the moment I started with mac, it was like I was starting all over again.

At this point I would be ready to try all the IDE's there are to get one which is simple and effective.

wrldwzrd89
Jul 24, 2012, 02:57 PM
Anyone here has some experience with Eclipse?

I have one problem. I can create and execute a program if:

1. File>>New>>C++Project>>Executable Files>>Hello World C++ Project.

But when I choose Empty Project I get lost.

I'm sorry. I'm totally confused and irritated. I am intermediate to advanced level in C++ ( I have done until dynamic memory allocation ), but the moment I started with mac, it was like I was starting all over again.

At this point I would be ready to try all the IDE's there are to get one which is simple and effective.
I use Eclipse, but not for C development. An empty project is just that - a project with no files in it. To add source files to the project you just created, go to New -> C++ Source File, give the file a name, and start writing code.

lee1210
Jul 24, 2012, 02:59 PM
Are your projects 10s,100s,1000s,10000s,etc. lines of code? I've worked on professional projects with millions of lines of code, and we used vi/vim to edit and make/rake to build. My point is that you're complaining that the tools youve found aren't just perfect for you to ply your trade. I encourage you to stop that sort of thinking. It will serve you well to be comfortable with a wide range of tools, so you can be effective in lots of environments.
You've stated Eclipse is too complex and a text editor and gcc is too simple. You're basically saying: without Borland I'm not comfortable programming. I would insist you work through that discomfort and come out the other side a more versatile programmer. If you don't want anything different, you can get a Windows VM and install Borland. You'd be robbing yourself of an opportunity to learn and grow if you do, though.

Code more, search for a Borland-alike less. Good luck.

-Lee

P.S. unless you invent a language and code in it for years, don't say you're advanced. It's a death sentence, and makes the offer to others to attack your understanding.

thrill4rishabh
Jul 24, 2012, 03:12 PM
I use Eclipse, but not for C development. An empty project is just that - a project with no files in it. To add source files to the project you just created, go to New -> C++ Source File, give the file a name, and start writing code.

Yeah that part I can do. But I'm not able to run the programs.

wrldwzrd89
Jul 24, 2012, 03:13 PM
Yeah that part I can do. But I'm not able to run the programs.
Oh, that's most likely because Eclipse doesn't know where your C++ compiler is located. This is set in the preferences -> C++.

thrill4rishabh
Jul 24, 2012, 03:13 PM
Are your projects 10s,100s,1000s,10000s,etc. lines of code? I've worked on professional projects with millions of lines of code, and we used vi/vim to edit and make/rake to build. My point is that you're complaining that the tools youve found aren't just perfect for you to ply your trade. I encourage you to stop that sort of thinking. It will serve you well to be comfortable with a wide range of tools, so you can be effective in lots of environments.
You've stated Eclipse is too complex and a text editor and gcc is too simple. You're basically saying: without Borland I'm not comfortable programming. I would insist you work through that discomfort and come out the other side a more versatile programmer. If you don't want anything different, you can get a Windows VM and install Borland. You'd be robbing yourself of an opportunity to learn and grow if you do, though.

Code more, search for a Borland-alike less. Good luck.

-Lee

P.S. unless you invent a language and code in it for years, don't say you're advanced. It's a death sentence, and makes the offer to others to attack your understanding.


You are right to some extent. I am willing to work with Eclipse, which I find decent, but I'm kinda stuck and don't know what to do next, and what irritates me is that its not the coding part, its the building and running part.

ChristianJapan
Jul 24, 2012, 03:24 PM
Wouldn't it much easier to just use XCode and write a console program ? Cost nothing, download from AppStore and get the basic done with with the help of the GUI. Once it's run you could (if you really) want use the command line mode of Xcode; but why these days.
Sorry, Borland I did not see in the last 20 years; my memory how it looks are vanished. My memories on XCode are fresh ;)

chown33
Jul 24, 2012, 03:26 PM
You are right to some extent. I am willing to work with Eclipse, which I find decent, but I'm kinda stuck and don't know what to do next, and what irritates me is that its not the coding part, its the building and running part.

Which suggests spending some time learning how to use Eclipse, perhaps with a tutorial on Eclipse.

Possible google search terms:
eclipse tutorial
eclipse tutorial c++
eclipse tutorial c++ mac

thrill4rishabh
Jul 24, 2012, 03:43 PM
Oh, that's most likely because Eclipse doesn't know where your C++ compiler is located. This is set in the preferences -> C++.

What to do here?

kthomp
Jul 28, 2012, 08:19 PM
Real programmers use VI.

VI? Real programmers use ed (http://www.gnu.org/fun/jokes/ed-msg.html).

lloyddean
Jul 28, 2012, 08:47 PM
Real programmers use the tools available - without comment on what"s not.

Cromulent
Jul 29, 2012, 05:19 PM
Just as an aside you should always use the following command line options when compiling with GCC or G++:


-Wall -Wextra -pedantic


this will ensure that the compiler warns you if you are doing anything stupid.