0

I created a dummy program to see if C++ was working, test.cpp:

int main(){
  int a = 10;
  return 0;
}

In my terminal, I typed:

gcc -Wall -c test.cpp -o test
./test

I got the following as the output:

bash: ./test: Permission denied

What can I do to be able to run my program?

  • 2
    Do you know what the `-c` flag does when you build your program? If you don't, then why do you use it? Have you tried *not* using it? Perhaps you should take some time to read [the documentation for GCC](https://gcc.gnu.org/onlinedocs/), or considering you're on a macOS system [the documentation for Clang](https://clang.llvm.org/docs/UsersManual.html). – Some programmer dude May 21 '18 at 02:17
  • 3
    I can -c clearly now the switch is gone. – user4581301 May 21 '18 at 02:19
  • @JonDeaton I typed that into terminal and then ./test, but got an error saying "bash: ./test: cannot execute binary file". – Arjun Gupta May 21 '18 at 02:19
  • @Someprogrammerdude I got rid of the -c and I got an error: "clang: error: linker command failed with exit code 1 (use -v to see invocation)" – Arjun Gupta May 21 '18 at 02:21
  • @ArjunGupta Can you try doing `chmod u+x test`? And then try and run it? – Rivasa May 21 '18 at 02:22
  • 1
    You are closer to success, though. I recommend adding the complete build output to your question. There will be a more descriptive error message in there somewhere. – user4581301 May 21 '18 at 02:22
  • @user4581301 What do you mean? – Arjun Gupta May 21 '18 at 02:23
  • "clang: error: linker command failed with exit code 1 (use -v to see invocation)` should be the tail end of the output. Earlier there should be a diagnostic explaining why the linker could not link the program. – user4581301 May 21 '18 at 02:25
  • @Annabelle I get an error saying "bash: ./test: cannot execute binary file". – Arjun Gupta May 21 '18 at 02:25
  • @user4581301 The first line is "Undefined symbols for architecture x86_64:". Does that help? – Arjun Gupta May 21 '18 at 02:26
  • 1
    Since it doesn't look like anyone has bothered looking it up, -c means stop building **before** linking an executable program. The job';s not done and you're trying to run [one of the intermediate steps of the building process](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work). – user4581301 May 21 '18 at 02:28
  • https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html#zz-1.6 This might be of interest. – Rivasa May 21 '18 at 02:28
  • "Undefined symbols for architecture x86_64:" means something is missing, but you've left out what it is that is missing. Edit the question and put all of the output in. Unless you know what line is important, it's a whole lot easier if you provide the whole thing. – user4581301 May 21 '18 at 02:32
  • @Annabelle Thank you! I did "g++ -o hello hello.cpp THEN chmod a+x hello THEN ./hello" and it worked! Thank you so much! – Arjun Gupta May 21 '18 at 02:34
  • 3
    If you're building a C++ program, you should use the `g++` program (or `clang++`). The difference between `gcc` and `g++` is that `g++` will link with the C++ standard library. – Some programmer dude May 21 '18 at 02:34
  • @user4581301 Thanks for all your help. As I wrote above, I believe I was simply using the wrong command. I used the commands in https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html#zz-1.6 and it worked just fine! Thanks once again! – Arjun Gupta May 21 '18 at 02:35
  • @Someprogrammerdude Yep! Thanks for your help! – Arjun Gupta May 21 '18 at 02:36

1 Answers1

0

C++ is a compiled language, therefore you cannot run it with "./test.cpp"

In my case it went as follows:

zsh:

  1. % g++ test.cpp --> It generates an (a.out file)
  2. % ./a.out --> It run flawlessly

;)

Blaze
  • 1
  • 2
    Welcome to Stack Overflow! Nowhere in the question did the user mention attempting to run the app using `./test.cpp`. They specifically named the file `test` using the `-o` option and attempted to run it. By the way, the solution to this one is in the comments. ;-) – NotTheDr01ds Aug 07 '23 at 10:51