Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thrill4rishabh

macrumors newbie
Original poster
Jul 17, 2012
26
0
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

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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:
  1. compiler_name options program_name.ext
  2. ./program_name
...where the compiler_name and options depend on your compiler.

For Xcode, either GCC or Clang will work.
 

thrill4rishabh

macrumors newbie
Original poster
Jul 17, 2012
26
0
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:
  1. compiler_name options program_name.ext
  2. ./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?
 

thrill4rishabh

macrumors newbie
Original poster
Jul 17, 2012
26
0
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

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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:
Code:
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

macrumors newbie
Original poster
Jul 17, 2012
26
0
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:
Code:
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?
 

thrill4rishabh

macrumors newbie
Original poster
Jul 17, 2012
26
0
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.
 

Pivs

macrumors member
Jul 2, 2012
38
0
Virginia
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.

Code:
g++ hello.cpp
./a.out
 
Last edited:

thrill4rishabh

macrumors newbie
Original poster
Jul 17, 2012
26
0
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

macrumors 604
Jul 4, 2008
6,772
17
Silicon Valley
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

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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.
 

thrill4rishabh

macrumors newbie
Original poster
Jul 17, 2012
26
0
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.
 
Last edited:

wrldwzrd89

macrumors G5
Jun 6, 2003
12,110
77
Solon, OH
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors newbie
Original poster
Jul 17, 2012
26
0
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.
 

thrill4rishabh

macrumors newbie
Original poster
Jul 17, 2012
26
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.