PDA

View Full Version : :( complete programming noob! (C++)




gmcguinn
Jan 20, 2011, 06:31 AM
Hey guys,

Well from what you can see in the title I am completely new to C++ programming. I have just started it in college and am really enjoying it.
However I wanted to set up all the software on my MBP so that I could work on it.

In college we write the code on a program called Smultron, save it as a .cpp file and then go to terminal to run it.

Now here comes my problem.... I have downloaded Smultron for my laptop and I have written a quick "C -> F conversion program". When I try to set that up in Terminal by using the command that we were told to use ("make program1") I get this error message in return...

"make: *** No rule to make target `practical1'. Stop."

This error doesn't come up in college. What I get in college is something like "g++ ........." and then I put in something like "./practical1"

As I have said before I want to try and work on my programming at home so I would love for some of your help.

Don't forget that I am a complete noob so try and keep it simple! We all start somewhere! :)



robbieduncan
Jan 20, 2011, 06:43 AM
make requires a Makefile to work. I suggest you copy the one from college.

Bill McEnaney
Jan 20, 2011, 06:47 AM
Hey guys,

Well from what you can see in the title I am completely new to C++ programming. I have just started it in college and am really enjoying it.
However I wanted to set up all the software on my MBP so that I could work on it.

In college we write the code on a program called Smultron, save it as a .cpp file and then go to terminal to run it.

Now here comes my problem.... I have downloaded Smultron for my laptop and I have written a quick "C -> F conversion program". When I try to set that up in Terminal by using the command that we were told to use ("make program1") I get this error message in return...

"make: *** No rule to make target `practical1'. Stop."

This error doesn't come up in college. What I get in college is something like "g++ ........." and then I put in something like "./practical1"

As I have said before I want to try and work on my programming at home so I would love for some of your help.

Don't forget that I am a complete noob so try and keep it simple! We all start somewhere! :)
You don't need the "make" command if your program is only a few lines long. If it just converts degrees fahrenheit to degrees celsius, you probably can compile it with, say, this command: g++ practice1.cpp -o practice1. That command tells the computer to compile your program with the C++ compiler and to call the executable program "practice1." If the machine finds mistakes, typos, in your program, the machine won't create the executable program. To make it create one, you'll need to correct the mistakes and recompile the program with the command I wrote in the code box.

You usually need the "make" command when you want to compile a big program composed of many separately-compiled functions.

gmcguinn
Jan 20, 2011, 07:28 AM
make requires a Makefile to work. I suggest you copy the one from college.

Ok and where do I find that file and what will it look like?

robbieduncan
Jan 20, 2011, 07:29 AM
Ok and where do I find that file and what will it look like?

It'll be a file called "Makefile". Normally it'd be in the same directory as the code, but depending on their setup it could be anywhere. I'd ask your lecturer.

Or, as suggested above, do without it.

mydogisbox
Jan 20, 2011, 07:47 AM
Ok and where do I find that file and what will it look like?

http://www.eng.hawaii.edu/Tutor/Make/

You can just make your own makefile. If you are going to be doing more command-line programming, it would be a good idea to learn how to do so anyway.

gmcguinn
Jan 20, 2011, 07:50 AM
http://www.eng.hawaii.edu/Tutor/Make/

You can just make your own makefile. If you are going to be doing more command-line programming, it would be a good idea to learn how to do so anyway.

Ahh ok thanks... And it doesn't matter what text editor I am using? It will work fine with Smultron.

gmcguinn
Jan 20, 2011, 07:50 AM
It'll be a file called "Makefile". Normally it'd be in the same directory as the code, but depending on their setup it could be anywhere. I'd ask your lecturer.

Or, as suggested above, do without it.

Ok and say I go in and get it to a usb where is the best place to store it and how do I set it up?

balamw
Jan 20, 2011, 07:51 AM
First things first.

Do you have gcc/g++ installed on your Mac?

The easiest way to get it is to install the Developer Tools (including Xcode) from the Optional Installs disc shipped with your Mac.

B

robbieduncan
Jan 20, 2011, 07:56 AM
Ok and say I go in and get it to a usb where is the best place to store it and how do I set it up?

make will automatically look for a file called Makefile in the cwd. In all honesty instead of expecting to be spoon-fed every minor you should be doing your own research when pointed in the right direction. Before even asking the question at the top of this thread you should have read the manpage for make which would have told you the answer. Any programmer worth anything relies on being able to use the documentation for the tools, languages and APIs. Start building this critical core skill early.

balamw
Jan 20, 2011, 08:01 AM
Ah the joy of IDEs and editors that hide the inner workings from you. (There was a recent thread that had some resources to cure you of this, will link here). EDIT: http://forums.macrumors.com/showthread.php?t=1078777 read lee1210's posts #4 and #13 in particular.

Another issue here is that at school it sounds like Smultron is calling g++, while it is trying to call make at home. EDIT: This may be a configuration issue with Smultron or just the fact that g++ is missing. Make sure you can get the instructions in lee1210's post #4 linked above to work.

B

chown33
Jan 20, 2011, 11:54 AM
Make doesn't always require a makefile. It has built-in rules (implicit rules) for making simple targets.

If I go to a directory that contains a file "handy.cpp" and I type:
make handy

it will run the command:
g++ handy.cpp -o handy


You can watch it evaluate implicit rules with this command instead:
make -d handy

It'll output a pretty long list, of which the last few lines are most relevant.

gmcguinn
Jan 20, 2011, 12:16 PM
First things first.

Do you have gcc/g++ installed on your Mac?

The easiest way to get it is to install the Developer Tools (including Xcode) from the Optional Installs disc shipped with your Mac.

B

I have installed xCode but how do I install g++?

balamw
Jan 20, 2011, 12:24 PM
I have installed xCode but how do I install g++?

Comes with. If you installed Xcode with the default settings, you have g++.

B

subsonix
Jan 20, 2011, 12:25 PM
I have installed xCode but how do I install g++?

You don't need to, it's just a GCC, C++ front end.

gmcguinn
Jan 20, 2011, 12:28 PM
[QUOTE=chown33;11760143]
it will run the command:
g++ handy.cpp -o handy


I tried to run your suggestion in terminal and I got :
"i686-apple-darwin10-g++-4.2.1: practical1.cpp: No such file or directory
i686-apple-darwin10-g++-4.2.1: no input files"

gmcguinn
Jan 20, 2011, 12:30 PM
Comes with. If you installed Xcode with the default settings, you have g++.

B

I'm confused now!:confused:

I will try those tasks that you supplied from the other forum and i'll get back to you all.

subsonix
Jan 20, 2011, 12:31 PM
Are you in the folder where practical1.cpp is located? Did you spell it right etc?

Edit: The error message you get is from g++, the complaint is that g++ can't find the file practical.cpp in the current folder.

gmcguinn
Jan 20, 2011, 12:41 PM
Ah the joy of IDEs and editors that hide the inner workings from you. (There was a recent thread that had some resources to cure you of this, will link here). EDIT: http://forums.macrumors.com/showthread.php?t=1078777 read lee1210's posts #4 and #13 in particular.

Another issue here is that at school it sounds like Smultron is calling g++, while it is trying to call make at home. EDIT: This may be a configuration issue with Smultron or just the fact that g++ is missing. Make sure you can get the instructions in lee1210's post #4 linked above to work.

B

Ok I typed in ...

echo "#include <stdio.h>
int main(int argc, char *argv[]) {
printf(\"Hello, World.\\n\");
return 0;
}" > myHello.c
gcc myHello.c
./a.out

... to terminal and it worked so what should I do next?

gmcguinn
Jan 20, 2011, 12:43 PM
Are you in the folder where practical1.cpp is located? Did you spell it right etc?

Edit: The error message you get is from g++, the complaint is that g++ can't find the file practical.cpp in the current folder.

I have the practical1.cpp file on my desktop and I am trying to use terminal to run it... That is how we were shown to do it in the lecture. We were told to go into terminal and type "make practical1" and then "./practical1"

subsonix
Jan 20, 2011, 12:46 PM
I have the practical1.cpp file on my desktop and I am trying to use terminal to run it... That is how we were shown to do it in the lecture. We were told to go into terminal and type "make practical1" and then "./practical1"

Don't use make, if you have no make script. Make is meant for situations where you have multiple source files.

try this:


cat > hello.cpp << eof
> #include <iostream>
>
> int main()
> {
> std::cout << "Hello, world\n";
> return 0;
> }


Hit: ctrl -d

type: g++ hello.cpp -o hello

then: ./hello


There, your first c++ program.

ulbador
Jan 20, 2011, 12:47 PM
Make doesn't always require a makefile. It has built-in rules (implicit rules) for making simple targets.


Well that's neat... you learn something new every day...

chown33
Jan 20, 2011, 12:47 PM
I tried to run your suggestion in terminal and I got :
"i686-apple-darwin10-g++-4.2.1: practical1.cpp: No such file or directory
i686-apple-darwin10-g++-4.2.1: no input files"

Please provide more information. In the same directory where you typed the make command line, enter this command and post the output:
pwd; ls -l

We need to see exactly what context make is trying to use. Since that context is all the files in the current working directory, we need to see the actual list of files.


I wonder about the accuracy of your posts. For example, in your original post you wrote:

When I try to set that up in Terminal by using the command that we were told to use ("make program1") I get this error message in return...

"make: *** No rule to make target `practical1'. Stop."
I've added red and blue colors to hilite the inconsistency. program1 and practical1 are not the same thing.

You can copy from the Terminal window and paste into posts. You can also copy from other windows and paste into Terminal. I suggest that you edit command-lines in a text editor, then copy and paste them into Terminal. When something doesn't work, copy and paste the entire command-line and its output into a post.

balamw
Jan 20, 2011, 12:49 PM
Great. We know for sure you have a compiler now.

I have the practical1.cpp file on my desktop and I am trying to use terminal to run it... That is how we were shown to do it in the lecture. We were told to go into terminal and type "make practical1" and then "./practical1"

What happens now if you try that?

If you need to substitute

g++ -o practical1 practical1.cpp for make practical1

B

gmcguinn
Jan 20, 2011, 12:55 PM
Great. We know for sure you have a compiler now.



What happens now if you try that?

If you need to substitute

g++ -o practical1 practical1.cpp for make practical1

B

Here is my terminal output for make practcal1 and the replacement that you provided.....


Gavin-McGuinnesss-MacBook-Pro:~ Gav$ make practical1
make: *** No rule to make target `practical1'. Stop.
Gavin-McGuinnesss-MacBook-Pro:~ Gav$ g++ -o practical1 practical1.cpp
i686-apple-darwin10-g++-4.2.1: practical1.cpp: No such file or directory
i686-apple-darwin10-g++-4.2.1: no input files
Gavin-McGuinnesss-MacBook-Pro:~ Gav$

balamw
Jan 20, 2011, 12:59 PM
Here is my terminal output for make practcal1 and the replacement that you provided.....


Gavin-McGuinnesss-MacBook-Pro:~ Gav$ make practical1
make: *** No rule to make target `practical1'. Stop.
Gavin-McGuinnesss-MacBook-Pro:~ Gav$ g++ -o practical1 practical1.cpp
i686-apple-darwin10-g++-4.2.1: practical1.cpp: No such file or directory
i686-apple-darwin10-g++-4.2.1: no input files
Gavin-McGuinnesss-MacBook-Pro:~ Gav$

Your current working directory (cwd) is ~ (a.k.a. home, /Users/USERNAME) not your Desktop

cd ~/Desktop

first.

B

gmcguinn
Jan 20, 2011, 01:02 PM
Please provide more information. In the same directory where you typed the make command line, enter this command and post the output:
pwd; ls -l



This is what I got when I typed in that code.

Last login: Thu Jan 20 18:59:46 on ttys000
Gavin-McGuinnesss-MacBook-Pro:~ Gav$ pwd; ls -l
/Users/Gav
total 32
drwx------+ 7 Gav staff 238 19 Jan 20:22 Desktop
drwx------+ 10 Gav staff 340 15 Dec 14:25 Documents
drwx------+ 103 Gav staff 3502 19 Jan 15:37 Downloads
drwxr-xr-x@ 9 Gav staff 306 20 Jan 11:45 Dropbox
drwx------+ 36 Gav staff 1224 16 Jan 19:50 Library
drwx------+ 9 Gav staff 306 15 Dec 14:18 Movies
drwx------+ 6 Gav staff 204 9 Oct 18:59 Music
drwx------+ 15 Gav staff 510 2 Dec 18:22 Pictures
drwxr-xr-x+ 5 Gav staff 170 8 Oct 23:46 Public
drwxr-xr-x+ 5 Gav staff 170 8 Oct 23:46 Sites
-rwxr-xr-x 1 Gav staff 8704 20 Jan 18:38 a.out
-rw-r--r-- 1 Gav staff 93 20 Jan 18:38 myHello.c


That inconsistency with program1 and practical1 was just a mistype... the name of the file that I want to run is practical1.cpp

balamw
Jan 20, 2011, 01:04 PM
This is what I got when I typed in that code.

Last login: Thu Jan 20 18:59:46 on ttys000
Gavin-McGuinnesss-MacBook-Pro:~ Gav$ pwd; ls -l
/Users/Gav



cd Desktop
or
cd ~/Desktop

EDIT: One way to make sure the file you want is in the current working directory is to use tab completion from the shell.

i.e.


make p<TAB>


keep hitting TAB until the name of the file you want shows up.

B

gmcguinn
Jan 20, 2011, 01:06 PM
Your current working directory (cwd) is ~ (a.k.a. home, /Users/USERNAME) not your Desktop

cd ~/Desktop

first.

B

I LOVE YOU! No Homo!
I forgot one of the steps that we were told in college and that was to change where the program was going to save from desktop to USERNAME

PROBLEM SOLVED :):apple:

For all who care here is my fist program on my MBP

Gavin-McGuinnesss-MacBook-Pro:~ Gav$ make practical1
make: `practical1' is up to date.
Gavin-McGuinnesss-MacBook-Pro:~ Gav$ ./practical1
My first C++ program.
The sum of 2 and 3 = 5
7 + 8 = 15
Num = 6

gmcguinn
Jan 20, 2011, 01:07 PM
Once again thank you everyone for helping... I can't wait for my next lab session now. :)

notjustjay
Jan 20, 2011, 01:12 PM
Glad you got it going, Gavin! (Hey, that's alliteration!)

I suggest that you get yourself familiar with the Terminal (unix shell). You should have be comfortable enough that navigating to different directories, and doing basic file operations like copying, moving, renaming and deleting files should become second nature. You'll be doing a lot of it if you continue to learn how to do programming from a command-line environment.

It's also really good experience. Understanding Unix is a marketable skill that you can put on your resume, and it's one that has lasting value. It was useful back in the 70's and it's still useful today.

Become completely familiar with the following Unix commands:
cd (including the "~", "." and ".." directories)
ls (and its variants, ls -a, ls -l, etc.)
cat
cp
mv
pwd
echo

And your favourite text editor, be it TextEdit, or a Unix one like emacs, pico :)

And then of course your compiler tools:

make
g++
gdb (eventually)

gmcguinn
Jan 20, 2011, 01:16 PM
B

Ok I moved my project to a sub folder in my documents, How do I run that without having to move it back to the "Username" section?

gmcguinn
Jan 20, 2011, 01:20 PM
Glad you got it going, Gavin! (Hey, that's alliteration!)

I suggest that you get yourself familiar with the Terminal (unix shell). You should have be comfortable enough that navigating to different directories, and doing basic file operations like copying, moving, renaming and deleting files should become second nature. You'll be doing a lot of it if you continue to learn how to do programming from a command-line environment.

It's also really good experience. Understanding Unix is a marketable skill that you can put on your resume, and it's one that has lasting value. It was useful back in the 70's and it's still useful today.

Become completely familiar with the following Unix commands:
cd (including the "~", "." and ".." directories)
ls (and its variants, ls -a, ls -l, etc.)
cat
cp
mv
pwd
echo

And your favourite text editor, be it TextEdit, or a Unix one like emacs, pico :)

And then of course your compiler tools:

make
g++
gdb (eventually)

Thanks... Is it weird that I am really excited to get stuck into some work?
Hah yes well I am doing a general engineering degree at the moment and in my 3rd year I chose a topic to specialise in... looking forward to learning more about computer engineering and software engineering in the years to come.

balamw
Jan 20, 2011, 01:31 PM
Ok I moved my project to a sub folder in my documents, How do I run that without having to move it back to the "Username" section?

OS X actually makes that pretty easy.

In Terminal, type cd<SPACE>
Then drag the folder from finder to the Terminal window.

EDIT: or manually just cd ~/Documents/Subfolder

As notjustjay suggests, mess around with some basic unix commands and get comfortable with them.

B

gmcguinn
Jan 20, 2011, 01:51 PM
OS X actually makes that pretty easy.

In Terminal, type cd<SPACE>
Then drag the folder from finder to the Terminal window.*

EDIT: or manually just cd ~/Documents/Subfolder

As notjustjay suggests, mess around with some basic unix commands and get comfortable with them.

B

* I've done this and it works just fine!

Now one more question... Now that I have change my directory will it stay as that folder until I change it myself.

As I said I am really looking forward to learning more about terminal and coding.

balamw
Jan 20, 2011, 01:57 PM
Now that I have change my directory will it stay as that folder until I change it myself.
Generally, yes. Until you issue another cd command or close the Terminal window.

B

gmcguinn
Jan 20, 2011, 01:59 PM
Generally, yes. Until you issue another cd command or close the Terminal window.

B

Ok thank you again.
It has been a pleasure. If there was a rating feature on this you my friend would get a 5/5

Gav

notjustjay
Jan 20, 2011, 02:03 PM
Generally, yes. Until you issue another cd command or close the Terminal window.


Remember in Jurassic Park there's a scene in which the girl looks at a computer displaying some sort of 3-D overhead-flying render of the file system and she says "Hey, this is Unix! I know this!"

It's a silly scene, but maybe it can help with the Terminal frame of mind. With a desktop metaphor like OS X or Windows, you can have multiple folder windows open at once. When in a Terminal window, think of it like YOU are standing in one particular spot in your file system. You stay there until you move yourself. You can move yourself downward into another directory or back upward to the place you came from before. Like a little adventure game...

Every time you start a new Terminal, you're placed in the same folder, your "home" directory, like an adventure game placing you at the beginning of a level.

Hope that's helpful somehow :)

ulbador
Jan 20, 2011, 02:12 PM
Remember in Jurassic Park there's a scene in which the girl looks at a computer displaying some sort of 3-D overhead-flying render of the file system and she says "Hey, this is Unix! I know this!"


Ahh, that brings back some memories. That was actually a real file manager that ran under IRIX. They actually have a clone that will run under OSX

http://fsv.sourceforge.net/

lee1210
Jan 20, 2011, 02:29 PM
Here is my terminal output for make practcal1 and the replacement that you provided.....


Gavin-McGuinnesss-MacBook-Pro:~ Gav$ make practical1
make: *** No rule to make target `practical1'. Stop.
Gavin-McGuinnesss-MacBook-Pro:~ Gav$ g++ -o practical1 practical1.cpp
i686-apple-darwin10-g++-4.2.1: practical1.cpp: No such file or directory
i686-apple-darwin10-g++-4.2.1: no input files
Gavin-McGuinnesss-MacBook-Pro:~ Gav$

You said you saved practical1.cpp from Smultron on your Desktop. Terminal doesn't start in your Desktop, it starts one step higher in your home directory.

Try:
cd Desktop
make pratical1

And see what you get. Make will complain if it has no Makefile AND it can't find a source file that matches its "default" behavior mentioned earlier.

For future reference, run:
pwd

to see your working directory. If it's not where you think it should be, you'll need to move around with cd.

-Lee

EDIT: Oops. I read to the bottom of page 1 and was replying based on that. Oh well, you got it going, good times.

gmcguinn
Jan 20, 2011, 03:59 PM
Remember in Jurassic Park there's a scene in which the girl looks at a computer displaying some sort of 3-D overhead-flying render of the file system and she says "Hey, this is Unix! I know this!"

It's a silly scene, but maybe it can help with the Terminal frame of mind. With a desktop metaphor like OS X or Windows, you can have multiple folder windows open at once. When in a Terminal window, think of it like YOU are standing in one particular spot in your file system. You stay there until you move yourself. You can move yourself downward into another directory or back upward to the place you came from before. Like a little adventure game...

Every time you start a new Terminal, you're placed in the same folder, your "home" directory, like an adventure game placing you at the beginning of a level.

Hope that's helpful somehow :)

Ahh ok that is helpful... thank you man!

:)I now know where to come when I hit my next stumbling block!

gmcguinn
Jan 20, 2011, 04:00 PM
EDIT: Oops. I read to the bottom of page 1 and was replying based on that. Oh well, you got it going, good times.

Haha I sure did. Thanks for popping by though. You guys are great!