0

I'm a begginer in programming and new to everything.

I am trying to use the Mac terminal to run my C++ program. My file name is Learning and my executable file is main.cpp.

I've tried:

$ g++ -o Learning main.cpp
$ ./main

But when I command the first line it returns:

clang: error: no such file or directory: 'main.cpp'
clang: error: no input files

This gives me the error message so I tried another way:

$ g++ main.cpp
$ ./a.out main.cpp

But it still gives me the same error message. I'm lost. Does anyone have any anwsers?

P.S. Thanks for taking the time to read this and potentially help me.

t.niese
  • 39,256
  • 9
  • 74
  • 101
  • 2
    Does this answer your question? [How to compile C++ with C++11 support in Mac Terminal](https://stackoverflow.com/questions/14228856/how-to-compile-c-with-c11-support-in-mac-terminal) – emegona Aug 31 '20 at 16:21
  • 2
    Are you sure that you execute `g++ -o Learning main.cpp` in the directory in which `main.cpp` is located? Because the error message says that this isn't the case. – t.niese Aug 31 '20 at 16:21
  • 2
    Type `ls` and see if `main.cpp` appears. If not `cd ` to move around. – Richard Aug 31 '20 at 16:24
  • You say the file name is Learning and executable is main.cpp but the way you’re compiling it, Learning is your executable and main.cpp is your program. Try switching the `-o` to main.cpp if that’s really what you meant. – crdrisko Aug 31 '20 at 16:25
  • I don't know how to check the directory in which it's being executed. – Nion _1714 Aug 31 '20 at 16:25
  • 1
    It is the directory in which you currently are in the Terminal. You either see that at the beginning of your command prompt (`~` in the command prompt e.g. refers to your home directory.), or by running the command `pwd`. – t.niese Aug 31 '20 at 16:27
  • 1
    You probably should spend a little while learning the shell. – drescherjm Aug 31 '20 at 16:29
  • Yes, I'm in the directory CLionProjects and that's where the Learning file is located. – Nion _1714 Aug 31 '20 at 16:30
  • Clang told you that there is no main.cpp file in the directory that you tried to compile it from. – drescherjm Aug 31 '20 at 16:31
  • `main.cpp` is supposed to be the source code of your c++ program. `Learning` is supposed to be the executable file generated by the compile and linker programs. – drescherjm Aug 31 '20 at 16:32
  • @Nion_1714 `I'm in the directory CLionProjects and that's where the Learning file is located. ` but where is **main.cpp** located which you try to compile? – t.niese Aug 31 '20 at 16:33
  • You probably want to rename `Learning` to `main.cpp` if `Learning` is really your source code. – drescherjm Aug 31 '20 at 16:33
  • @t.niese main.cpp is located in the Learning file. – Nion _1714 Aug 31 '20 at 16:36
  • @Nion_1714 Can you run `cd Learning` then `ls` and tell us what you see? – crdrisko Aug 31 '20 at 16:37
  • @Nion_1714 A file cannot be located in another file. Is `Learning` a directory? – t.niese Aug 31 '20 at 16:37
  • @crdrisko Thanks I did it and then it showed main & main.cpp and then I tried to compile and run it and it worked! – Nion _1714 Aug 31 '20 at 16:44
  • You really should learn the basics of the shell first, and the correct naming of stuff like, directory and files (or how to correctly differentiate between them in the shell), otherwise you introduce confusion, which makes it hard or impossible to help. – t.niese Aug 31 '20 at 16:46
  • 2
    So for future reference, we refer to Learning as a directory and main.cpp as a file. Your executable is a.out or whatever file name you specified after the -o option to g++. Hence the back and forth in the comments – crdrisko Aug 31 '20 at 16:46
  • Thanks, everyone. Sorry for not naming the directories and files correctly and giving you a headache. – Nion _1714 Aug 31 '20 at 16:49

0 Answers0