:( complete programming noob! (C++)

Discussion in 'Mac Programming' started by gmcguinn, Jan 20, 2011.

  1. macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #1
    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! :)
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    make requires a Makefile to work. I suggest you copy the one from college.
     
  3. Bill McEnaney, Jan 20, 2011
    Last edited: Jan 20, 2011

    macrumors 6502

    Joined:
    Apr 29, 2010
    #3
    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:
    Code:
    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.
     
  4. thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #4
    Ok and where do I find that file and what will it look like?
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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.
     
  6. macrumors member

    Joined:
    Jan 16, 2011
    #6
    How to make a makefile

    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.
     
  7. thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #7
    Ahh ok thanks... And it doesn't matter what text editor I am using? It will work fine with Smultron.
     
  8. thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #8
    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?
     
  9. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #9
    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
     
  10. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    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.
     
  11. balamw, Jan 20, 2011
    Last edited: Jan 20, 2011

    Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    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
     
  12. macrumors 603

    Joined:
    Aug 9, 2009
    #12
    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:
    Code:
    make handy
    
    it will run the command:
    Code:
    g++     handy.cpp   -o handy
    
    You can watch it evaluate implicit rules with this command instead:
    Code:
    make -d handy
    
    It'll output a pretty long list, of which the last few lines are most relevant.
     
  13. thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #13
    I have installed xCode but how do I install g++?
     
  14. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #14
    Comes with. If you installed Xcode with the default settings, you have g++.

    B
     
  15. macrumors 68040

    Joined:
    Feb 2, 2008
    #15
    You don't need to, it's just a GCC, C++ front end.
     
  16. thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #16
     
  17. thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #17
    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.
     
  18. macrumors 68040

    Joined:
    Feb 2, 2008
    #18
    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.
     
  19. thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #19
    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?
     
  20. thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #20
    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"
     
  21. macrumors 68040

    Joined:
    Feb 2, 2008
    #21
    Don't use make, if you have no make script. Make is meant for situations where you have multiple source files.

    try this:

    Code:
    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.
     
  22. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #22
    Well that's neat... you learn something new every day...
     
  23. macrumors 603

    Joined:
    Aug 9, 2009
    #23
    Please provide more information. In the same directory where you typed the make command line, enter this command and post the output:
    Code:
    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:
    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.
     
  24. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #24
    Great. We know for sure you have a compiler now.

    What happens now if you try that?

    If you need to substitute

    Code:
    g++ -o practical1 practical1.cpp
    for
    Code:
    make practical1
    B
     
  25. thread starter macrumors newbie

    gmcguinn

    Joined:
    Jan 20, 2011
    Location:
    Dublin, Ireland
    #25
    Here is my terminal output for make practcal1 and the replacement that you provided.....


    Code:
    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$ 
     

Share This Page