'Why won't this C++ program compile and run? [duplicate]

I tried to run a basic C++ file in the terminal:

#include <iostream>

using namespace std;

int main() {
    cont << "This is my first C++ program!";
    return 0
}

And then tried to run it in the terminal:

make learningCPP.cpp

    make: *** No rule to make target `learningCPP.cpp'.  Stop.

And tried:

make learningCPP

    make: *** No rule to make target `learningCPP'.  Stop.

And tried:

gcc learningCPP.cpp -o learningCPP.out

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

Here is the entire Bash/Clang file:

Last login: Mon May 25 07:49:21 on console

make learningCPP.cpp

    make: *** No rule to make target `learningCPP.cpp'.  Stop.

make learningCPP

    make: *** No rule to make target `learningCPP'.  Stop.

gcc learningCPP.cpp -o learningCPP.out

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

$ g++ -o lab21 learningCPP.cpp

    -bash: $: command not found

$ ./lab21

    -bash: $: command not found

./learningCPP.cpp

    -bash: ./learningCPP.cpp: No such file or directory

./main

    -bash: ./main: No such file or directory

$ g++ -o main learningCPP.cpp

    -bash: $: command not found

cpp

make learningCPP.cpp

run

How can I fix it?



Solution 1:[1]

Judging by the errors, your file either isn't called learningCPP.cpp, or isn't in the directory you're trying to compile it from.

Rename it so it has that name, or change directory to its location, then the build command is

g++ learningCPP.cpp -o learningCPP

not gcc, and with no spurious $ before it. Alternatively, as long as the source file is present in the working directory, you could use make learningCPP.

Once that succeeds, run the program with

./learningCPP

although you'll have to read the new error messages, and use that to figure out how to fix the syntax errors, before it will compile.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1