How to compile a c++ program using the terminal on a mac

Discussion in 'Programmer's Corner' started by asilvester635, Jan 27, 2017.

  1. asilvester635

    Thread Starter Member

    Jan 26, 2017
    73
    3
    I'm currently learning c++, and I have trouble compiling it via my Macbook pro's terminal.

    Below is a simple code that I wrote.

    Code (Text):
    1.  
    2. #include <cstdio>
    3. //using namespace std;
    4.  
    5. int main (int argc, char** argv)
    6. {
    7.  
    8. printf("Hello World!!!\n");
    9.  
    10. }
    11.  

    This is what I typed in the terminal: g++ -c -Werror hello.cc

    My source code (named hello.cc) is stored in a folder called Hello program, which is stored in a folder called c++ programs, which is located on my desktop. When I type in g++ -c -Werror hello.cc into the terminal, this is what I get.

    Conrados-MBP:~ conrados$ g++ -c -Werror hello.cc

    clang: error: no such file or directory: 'hello.cc'

    clang: error: no input files

    I'm kind of desperate right now. It's hard to figure this out.
     
  2. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    3,967
    5,360
    What's the name of your source file, and where is it?
     
    asilvester635 likes this.
  3. asilvester635

    Thread Starter Member

    Jan 26, 2017
    73
    3
    My source code or file (named hello.cc) is stored in a folder called Hello program, which is stored in a folder called c++ programs, which is located on my desktop
     
  4. thumb2

    Member

    Oct 4, 2015
    122
    9
    Code (Bash):
    1. g++ hello.cc -o hello
    From the folder containing the file.
    Take a look to the gcc man page for the options.
     
    asilvester635 likes this.
  5. asilvester635

    Thread Starter Member

    Jan 26, 2017
    73
    3
    I do not know how to "look to the gcc man page for the options". What steps would I need to do that?
     
  6. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    3,967
    5,360
    cd ~/Desktop/Hello\ program/

    Then run g++.
     
    asilvester635 likes this.
  7. wayneh

    Expert

    Sep 9, 2010
    14,799
    5,283
    I use Xcode and I'm not really familiar with compiling from Terminal, but this appears to be simply a problem of not being in the right folder. Use the cd command to burrow into your desktop folder. Use the ls command to verify you're location and see what files are there.

    I believe you could also drop your file into Terminal and it will give you the path to your file. Use that in your command, not just the file name, if you're not working in the desktop folder.
     
    asilvester635 likes this.
  8. thumb2

    Member

    Oct 4, 2015
    122
    9
    Code (Bash):
    1. man gcc
    The man page of gcc is the same for g++. ;)
     
    asilvester635 likes this.
  9. asilvester635

    Thread Starter Member

    Jan 26, 2017
    73
    3
    Hey I dropped the .cc file into the terminal and it gave me this

    Conrados-MBP:~ conrados$ /Users/conrados/Desktop/C++\ Programs/Hello\ Program/hello.cc

    -bash: /Users/conrados/Desktop/C++ Programs/Hello Program/hello.cc: Permission denied
     
  10. wayneh

    Expert

    Sep 9, 2010
    14,799
    5,283
    Try typing your compile command first, then drop the file in, then hit return.
     
    asilvester635 likes this.
  11. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    3,967
    5,360
    That's because you attempted to "run" (execute) hello.cc, which is not allowed (execute bit not set nor should it be).

    Just cd to that directory and run your g++ command as in your original post.
     
    asilvester635 and wayneh like this.
  12. asilvester635

    Thread Starter Member

    Jan 26, 2017
    73
    3
    Is this the right keywords: cd /User/conrados
     
  13. asilvester635

    Thread Starter Member

    Jan 26, 2017
    73
    3
    I did just that and it gave me this

    Conrados-MBP:~ conrados$ g++ -c -Werror hello.cc

    clang: error: no such file or directory: 'hello.cc'

    clang: error: no input files

    Conrados-MBP:~ conrados$ /Users/conrados/Desktop/C++\ Programs/Hello\ Program/hello.cc

    -bash: /Users/conrados/Desktop/C++ Programs/Hello Program/hello.cc: Permission denied

    Conrados-MBP:~ conrados$
     
  14. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    3,967
    5,360
    Code (Text):
    1. cd /Users/conrados/Desktop/C++\ Programs/Hello\ Program/
    2. g++ -c -Werror hello.cc
     
    asilvester635 likes this.
  15. asilvester635

    Thread Starter Member

    Jan 26, 2017
    73
    3
    I was able to execute the source code with this
    /Users/conrados/Desktop/C++\ Programs/Hello\ Program/hello

    Though I did not even have to type in the compiling code g++ -c -Werror hello.cc
     
  16. asilvester635

    Thread Starter Member

    Jan 26, 2017
    73
    3
    IT WORKED!!! THANKS!!!!!
     
    wayneh and joeyd999 like this.
  17. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    3,967
    5,360
    Click the like button. I redeem those later for cash.
     
    asilvester635 likes this.
  18. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    3,967
    5,360
    Within the directory, just type:

    ./hello

    To run the compiled program.
     
    asilvester635 likes this.
  19. wayneh

    Expert

    Sep 9, 2010
    14,799
    5,283
    So you're a Mac user?
     
    asilvester635 likes this.
  20. asilvester635

    Thread Starter Member

    Jan 26, 2017
    73
    3
    Yup.
     
Loading...