Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > How Do I Make a Simple C++ Program?

How Do I Make a Simple C++ Program?
Thread Tools
Mr.E
Junior Member
Join Date: Oct 2001
Location: UCI
Status: Offline
Reply With Quote
Nov 10, 2001, 02:49 PM
 
I cannot figure out how to make one through the project builder without having to use their interface. I just want to make a simple console program such as "hello world." Is there any way to do this with Apple's provided tools?
     
Hobbes
Mac Enthusiast
Join Date: Nov 2000
Status: Offline
Reply With Quote
Nov 10, 2001, 03:20 PM
 
Sorry to disappoint you. This isn't an answer. Just a confirmation that someone else is having the same problem. But with me, it may just be my own ignorance.
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Nov 10, 2001, 03:22 PM
 
Use the Standard Tool project template. By default that will have a .c file in it, delete it and add a file with the extension .cpp in order to make it compile as C++.
     
Juggle5
Junior Member
Join Date: Nov 2001
Location: Seattle
Status: Offline
Reply With Quote
Nov 10, 2001, 07:07 PM
 
Another question, along on the same line:

When I create the standard C++ tool "hello world" project using project maker, it won't compile. I get the linker error

<font face = "courier">/usr/bin/ld: /usr/lib/libSystem.dylib load command 6 unknown cmd field</font>

Any thoughts? I've never seen a linker error like this before. Same thing happened when I tried to compile the Vim source code, and the same thing if I just do "cc hellotest.cc" from the command line.
     
Apocalypse
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Nov 11, 2001, 12:08 AM
 
That link problem is because cc doesn't seem to like actual c++ code. You have to explicitly invoke the c++ compiler like this:

c++ helloWorld.cpp -o helloWorld

That worked for me when compiling a simple:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#include &lt;stdio.h&gt;
#include &lt;iostream.h&gt;

int main(){
cout &lt;&lt; <font color = red>"Hello World!\n"</font>;
}
</font>[/code]

Hope that helps,
Jeff.
Spectral Class
"Shedding Light on Innovation"
     
Mike the Knife
Forum Regular
Join Date: Nov 2001
Location: New Jersey, USA
Status: Offline
Reply With Quote
Nov 11, 2001, 02:39 AM
 
Mr. E, I actually had a similar problem -- I got some error involving some remote file that seems to have something to do with Project Builder itself. I've been editing in it as normal, and compiling from the command prompt. I like it better that way, actually.

PS - If you are who I think you are: Great site, buddy; I'm a daily reader =/
     
Moonray
Mac Elite
Join Date: May 2001
Status: Offline
Reply With Quote
Nov 11, 2001, 04:20 AM
 
Hey, the simplest way for "Hello world" is not to use the project builder at all:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
[localhost:~] % cat &gt; hello.c
#include &lt;stdio.h&gt;
int main(void)
{
printf(<font color = red>"Hello world!\n"</font>);
}
</font>[/code]
(press ctrl D here)
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
[localhost:~] % cc hello.c -o hello
[localhost:~] % ./hello
Hello world!
[localhost:~] %
</font>[/code]


-
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Nov 11, 2001, 06:23 AM
 
Originally posted by Juggle5:
<STRONG>Another question, along on the same line:

When I create the standard C++ tool "hello world" project using project maker, it won't compile. I get the linker error

&lt;font face = "courier"&gt;/usr/bin/ld: /usr/lib/libSystem.dylib load command 6 unknown cmd field&lt;/font&gt;

Any thoughts? I've never seen a linker error like this before. Same thing happened when I tried to compile the Vim source code, and the same thing if I just do "cc hellotest.cc" from the command line.</STRONG>
You are on OS X 10.1 and you didn't upgrade your developer tools when you upgraded.
     
Mr.E  (op)
Junior Member
Join Date: Oct 2001
Location: UCI
Status: Offline
Reply With Quote
Nov 11, 2001, 05:43 PM
 
I've tried using the CC tool to compile from the command line...but it's giving me the following error:

cc: installation problem, cannot exec `cc1plus': No such file or directory

I'm typing in the command as specified:

[me211128:~/desktop] jamesgui% cc source.cpp -o Program

I have the 10.1 developer tools installed, and it gave me no errors when I ran the installer. Anyone know what to do here?
     
Mr.E  (op)
Junior Member
Join Date: Oct 2001
Location: UCI
Status: Offline
Reply With Quote
Nov 11, 2001, 06:40 PM
 
The compiler does seem to work if I change the file name to source.c rather than source.cpp. However, it looks at the code as C instead of C++, and gives me a ton of errors. I definitely want to use C++, as that's the language I know inside and out.
     
Juggle5
Junior Member
Join Date: Nov 2001
Location: Seattle
Status: Offline
Reply With Quote
Nov 11, 2001, 09:19 PM
 
I'll bet it's the 10.0 dev tools on system 10.1. That might explain why I had to rename some files just to get project builder to run. When I installed 10.1, I looked, but I didn't remember seeing the new dev tools on the CD. I'll look again or download it. Thanks.

To Apocalypse: FYI, you shouldn't *need* to use c++ if you don't want to. From the compiler documentation (on my computer it's in Developer/Documentation/DeveloperTools/Compiler): "The C, C++, Objective-C, and Objective-C++ versions of the compiler are integrated; the GNU C compiler can compile programs written in C, C++, or Objective-C." You have an excellent point on many other systems, however.

To moonray: this was actually what I tried first. Since it didn't work, I figured there might be some weird compiler options and hoped the project builder would work.

Thanks again.
     
Angus_D
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status: Offline
Reply With Quote
Nov 12, 2001, 03:45 AM
 
The 10.1 Developer Tools are available from http://connect.apple.com and if you notice I suggested this earlier but everybody ignored me
     
Mr.E  (op)
Junior Member
Join Date: Oct 2001
Location: UCI
Status: Offline
Reply With Quote
Nov 12, 2001, 08:14 AM
 
No, I'm 100% certain it's the 10.1 tools...I downloaded them from the ADC last week. The project builder works fine...just doesn't do simple console programs (rather, I haven't figured out how).
     
chicken_tastes_good
Junior Member
Join Date: Dec 1999
Location: Clovis' underwater chicken ranch
Status: Offline
Reply With Quote
Nov 12, 2001, 08:25 PM
 
Originally posted by Mr.E:
<STRONG>
[me211128:~/desktop] jamesgui% cc source.cpp -o Program
</STRONG>
as was stated earlier you probably want

% c++ source.cpp -o Program
The empty can rattles the loudest
     
Scrod
Mac Elite
Join Date: Jan 2001
Location: Sad King Billy's Monument on Hyperion
Status: Offline
Reply With Quote
Nov 12, 2001, 10:09 PM
 
Originally posted by Mr.E:
<STRONG>No, I'm 100% certain it's the 10.1 tools...I downloaded them from the ADC last week. The project builder works fine...just doesn't do simple console programs (rather, I haven't figured out how).</STRONG>
Make a new project and choose "C++ Tool". That's a "simple console program". It's what you want.
I abused my signature until she cried.
     
11011001
Mac Elite
Join Date: May 2001
Location: Up north
Status: Offline
Reply With Quote
Nov 14, 2001, 09:01 PM
 
OS X is a unix OS, so you can use much of the tools that you can in UNIX to compile simple command line programs that can be run in the terminal. But learning how to do this is probably harder and more time consuming than finding an answer to your problem...

Off hand, (I am in OS 9 right now and don't feel like restarting) I believe VI can compile or is that emacs? Or perhaps it was another UNIX text editor that I downloaded. Anyways, there are many unix text editors that can compile programs for you in the terminal, that can be used in the terminal.

On thing for sure, Apple Developer Tools does not have any easy way or any way, that I know of, to make anything for the terminal...

Anyways good luck with this...
     
davecom
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Nov 14, 2001, 11:05 PM
 
Originally posted by 11011001:
<STRONG>OS X is a unix OS, so you can use much of the tools that you can in UNIX to compile simple command line programs that can be run in the terminal. But learning how to do this is probably harder and more time consuming than finding an answer to your problem...

Off hand, (I am in OS 9 right now and don't feel like restarting) I believe VI can compile or is that emacs? Or perhaps it was another UNIX text editor that I downloaded. Anyways, there are many unix text editors that can compile programs for you in the terminal, that can be used in the terminal.

On thing for sure, Apple Developer Tools does not have any easy way or any way, that I know of, to make anything for the terminal...

Anyways good luck with this... </STRONG>
This is not true. Any project type that is labeled as a tool, is mean't to be used in the terminal.
     
11011001
Mac Elite
Join Date: May 2001
Location: Up north
Status: Offline
Reply With Quote
Nov 16, 2001, 12:54 AM
 
Well I did not know developer could do that... so thx

But there is true stuff in there... One of those text editors can compile for the terminal. And one of the reasons why it is so easy to port unix programs and linux to OS X is cus OS X is unix, and though apple does not encourage it (they seem to be in love with aqua), you can port a text based program over from unix with only editing it a bit, to be used in the terminal... Examples are many and widespread...
     
Chaaaosss
Forum Regular
Join Date: Oct 2000
Status: Offline
Reply With Quote
Nov 17, 2001, 12:10 PM
 
Originally posted by 11011001:
<STRONG>Well I did not know developer could do that... so thx

But there is true stuff in there... One of those text editors can compile for the terminal. And one of the reasons why it is so easy to port unix programs and linux to OS X is cus OS X is unix, and though apple does not encourage it (they seem to be in love with aqua), you can port a text based program over from unix with only editing it a bit, to be used in the terminal... Examples are many and widespread...</STRONG>
Actually, at the WWDC this past May, Apple encouraged developers to use Interface Builder to develop interfaces on top of the existing source code of UNIX apps. That's one of the beauties of Apple's frameworks and interface builder architecture. You can take what used to be a command-line app and use Cocoa (or Carbon even, I think) and attach messages, mix and match Objective-C with C++, and create a stunning user interface.
     
Mithras
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Nov 18, 2001, 02:23 PM
 
Originally posted by Mr.E:
<STRONG>The compiler does seem to work if I change the file name to source.c rather than source.cpp. However, it looks at the code as C instead of C++, and gives me a ton of errors. I definitely want to use C++, as that's the language I know inside and out.</STRONG>
Unixland is used to a .cc extension for C++ files. I think the .cpp extension was a Microsoftism, to hue to DOS's three-letters-only extension policy.
     
kamprath
Junior Member
Join Date: Apr 2000
Location: San Francisco, CA
Status: Offline
Reply With Quote
Nov 18, 2001, 06:39 PM
 
Originally posted by 11011001:
<STRONG>OS X is a unix OS, so you can use much of the tools that you can in UNIX to compile simple command line programs that can be run in the terminal. But learning how to do this is probably harder and more time consuming than finding an answer to your problem...

Off hand, (I am in OS 9 right now and don't feel like restarting) I believe VI can compile or is that emacs? Or perhaps it was another UNIX text editor that I downloaded. Anyways, there are many unix text editors that can compile programs for you in the terminal, that can be used in the terminal.

On thing for sure, Apple Developer Tools does not have any easy way or any way, that I know of, to make anything for the terminal...

Anyways good luck with this... </STRONG>
Actually, the text editors (vi, emacs, etc) do not compile code, but you can use them to write code. You still need to issue the compile command on the command line (type out "cc ...", create a make file, or just use Project Builder which does it for you!).

What Project Builder calls a "tool" is really a standard C/C++ application in UNIX, which must be run from a shell environment. The easiest shell environment is the Terminal command line. So, Apple Developder Tools can write programs that run from the terminal's command line.

Michael Kamprath
--
Michael F. Kamprath
     
   
 
Forum Links
Forum 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 On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 01:44 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,