C++ Help on Mac
From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.
| Announcements | Posted on | |
|---|---|---|
| Enter our travel-writing competition for the chance to win a Nikon 1 J3 camera | 20-05-2013 | |
-
C++ Help on Mac
Hi all,
I am currently studying Maths at uni and for part of my project next year I need to do some C++ work.
I have a mac and was wondering if anyone could give me any help/pointers.
I have so far fumbled through as far as I can get and am now really unsure of what is goings on. So here's what I have done.
I have installed TextWrangler and xCode. First xCode, but after a while online I found this was relatively hard for beginners (thoughts on this??) so I then got TextWrangler to write my code into and then compile through the terminal.
To use the terminal to compile I had to get gcc installed from xCode (it became useful in the end I suppose) and so did that. I have now changed my directory on terminal to the one where my code file is and written some code. I have gcc installed and want to compile my code but it keeps spitting out errors.
The code is copied and pasted from a file my lecturer gave me and so I have no idea really what is says.
I don't really understand any of this yet as it is the first time I have ever opened terminal So here goes.
In textwrangler I have:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
int n,i,j,k;
double x[101][4],length,energy,distance;
/* set the number of points */
n=12;
/* check that there are no more than 100 points */
if(n>100){
cout << n << " is too many points for me :-( \n";
exit(0);
}
/* reset the random number generator */
srand((unsigned)time(0));
/* assign random points */
for (i=1;i<=n;i++){
x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
x[i][3]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));
for (k=1;k<=3;k++){x[i][k]=x[i][k]/length;}
}
/* calculate the energy */
energy=0.0;
for(i=1;i<=n;i++){
for(j=i+1;j<=n;j++){
distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
+pow(x[i][3]-x[j][3],2));
energy=energy+1.0/distance;
}}
/* output the energy to the screen and the points to a file */
cout << "The energy is E=" << energy << "\n";
ofstream mypoints ("mypoints");
for(i=1;i<=n;i++){
mypoints << x[i][1] << " " << x[i][2] << " " << x[i][3] << "\n";
}
mypoints.close();
}
and in terminal I have:
adie_raz's-MacBook-Pro:Points on a Sphere adie_raz$ gcc -o program file.c
file.c
20: error: iostream: No such file or directory
file.c
19: error: fstream: No such file or directory
file.c
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘namespace’
file.c: In function ‘main’:
file.c
error: ‘cout’ undeclared (first use in this function)
file.c
error: (Each undeclared identifier is reported only once
file.c
error: for each function it appears in.)
file.c
error: ‘ofstream’ undeclared (first use in this function)
file.c
error: expected ‘;’ before ‘mypoints’
file.c
error: ‘mypoints’ undeclared (first use in this function)
adie_raz's-MacBook-Pro:Points on a Sphere adie_raz$
I have very little experience of this and am confused as to why I have errors, if I need to install something or not, or how to C++ code in the first place (basically just please I managed to get gcc installed and recognised and used cd and ls on terminal so far).
Anyone know why it has errors?? If so would really appreciate any help and am going to keep trying to learn C++ whilst I wait for responses.
Thanks, A. -
Re: C++ Help on Macgcc is the C compiler (which is not the same as g++, the C++ compiler). Actually, it doesn't really matter which you use so long as the extension is changed to be correct. Rename the .c file to a .cc file and try again:(Original post by adie_raz)
adie_raz's-MacBook-Pro:Points on a Sphere adie_raz$ gcc -o program file.c
file.c
20: error: iostream: No such file or directory
file.c
19: error: fstream: No such file or directory
file.c
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘namespace’
file.c: In function ‘main’:
file.c
error: ‘cout’ undeclared (first use in this function)
file.c
error: (Each undeclared identifier is reported only once
file.c
error: for each function it appears in.)
file.c
error: ‘ofstream’ undeclared (first use in this function)
file.c
error: expected ‘;’ before ‘mypoints’
file.c
error: ‘mypoints’ undeclared (first use in this function)
adie_raz's-MacBook-Pro:Points on a Sphere adie_raz$
I have very little experience of this and am confused as to why I have errors, if I need to install something or not, or how to C++ code in the first place (basically just please I managed to get gcc installed and recognised and used cd and ls on terminal so far).
Anyone know why it has errors?? If so would really appreciate any help and am going to keep trying to learn C++ whilst I wait for responses.
Thanks, A.
Code:#change c file to cc file mv file.c file.cc #compile g++ -o program file.cc # run the program ./program
Last edited by roblee; 20-07-2012 at 16:12. Reason: yu -> you -
Re: C++ Help on Mac(Original post by roblee)
gcc is the C compiler (which is not the same as g++, the C++ compiler). Actually, it doesn't really matter which you use so long as the extension is changed to be correct. Rename the .c file to a .cc file and try again:
Code:#change c file to cc file mv file.c file.cc #compile g++ -o program file.cc # run the program ./program
Sweet this worked. Thank you! -
Re: C++ Help on Macgcc/g++ will by default compile for the platform it's running on so on a mac you'll get something that runs on a mac, if you did it on windows you'll get something that runs (if it's got the right extension). It is possible to compile for a different platform (cross compiling) but that's a whole different thing.(Original post by fran.ha)
Can you even do this? Mac cannot run .exe...
.exe is just a tag windows uses to indicate that the program is a windows compatible executable file, most other operating systems use various other techniques to determine what a file is (exectuable flag, file contents etc)