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

Thread Starter

asilvester635

Joined Jan 26, 2017
73
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:
#include <cstdio>
//using namespace std;

int main (int argc, char** argv)
{

printf("Hello World!!!\n");

}

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.
 

wayneh

Joined Sep 9, 2010
16,384
clang: error: no such file or directory: 'hello.cc'
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.
 

Thread Starter

asilvester635

Joined Jan 26, 2017
73
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.
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
 

joeyd999

Joined Jun 6, 2011
4,376
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
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.
 

Thread Starter

asilvester635

Joined Jan 26, 2017
73
Try typing your compile command first, then drop the file in, then hit return.
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$
 

Thread Starter

asilvester635

Joined Jan 26, 2017
73
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
 
Top