1

Every time I try to execute a program, no matter what program, I get this message:

bash: ./filename.cpp: Permission denied

When I compile I have no problems, it works fine. But when I want to execute I get this message.

Does it have anything to do with permissions? Because I'm having a lot of problems with this, too. I can't access my shared folders, unless I use

sudo nautilus

Please see my other question.

Francesca
  • 19
  • 1
  • 2
  • Try `chmod u+x program_name` where program_name is the programs name. And then execute the program. – Steam gamer Dec 21 '15 at 12:21
  • 1
    You can't just run a .cpp file you MUST compile it, it is not a bash script it is a program.. As for your shared folders, what are there paths ? – Mark Kirby Dec 21 '15 at 12:21
  • @Steamgamer I have already tried that, but it doesn't work. – Francesca Dec 21 '15 at 12:22
  • @markkirby I compile everytime, but then I can't execute. The shared folder is in computer/media/ but everytime I click on it I get this message "This location could not be displayed. You do not have the permissions necessary to view the contents of "sf_sharedfolder". – Francesca Dec 21 '15 at 12:24
  • To be clear, are you talking specifically about programs that you have compiled? What *exactly* are you typing (a) to compile them and (b) to run them. Please be specific i.e. use the **real** names of files. – steeldriver Dec 21 '15 at 12:26
  • Are you trying to compile/run programs on a virtualbox shared folder? it is likely that the sf_ filesystem doesn't support the necessary Unix permissions – steeldriver Dec 21 '15 at 12:28
  • 2
    You are executing the wrong file, the compiled file is a .out file, by default it is called a.out and goes to your home folder, run the a.out not he .cpp. You can't access your sheared folders as user because root owns /media, what are they sheared from/with ? – Mark Kirby Dec 21 '15 at 12:28
  • @steeldriver I'm talking about specific programs that I've compiled, but I think I have a problem with execution in general, because I tried to create new programs, compile and execute them, and they don't work either. My program is main.cpp. When I compile it I type g++ -c main.cpp, then I trype ./main.cpp and I get that "permission denied" message. – Francesca Dec 21 '15 at 12:31
  • @markkirby When I type ./a.out I get this "bash: ./a.out: No such file or directory" Am I typing it wrong? – Francesca Dec 21 '15 at 12:35
  • 1
    That's simply **not** how to compile and run a C++ program: you do `g++ -o yourprog main.c` then `./yourprog` - do *not* add the `-c` flag to the `g++` command unless you want it to produce an object code file instead of a full executable. See [What is a command to compile and run C++ programs?](http://askubuntu.com/questions/61408/what-is-a-command-to-compile-and-run-c-programs) – steeldriver Dec 21 '15 at 12:35
  • I am doing an answer with simple steps to compile and run cpp code. – Mark Kirby Dec 21 '15 at 12:35
  • @steeldriver When I use g++ main.cpp -o main I get a list of "relocation 0 has invalid symbol index 11, relocation 1 has invalid symbol index 12" and so on – Francesca Dec 21 '15 at 12:38
  • 1
    As these issues are unrelated, please make a new question asking about you sheared folder permissions. – Mark Kirby Dec 21 '15 at 12:48
  • 2
    @Francesca the *"relocation"* errors suggest your `main.cpp` file does not actually contain a `main` function: see [Linker returns “relocation has an invalid symbol at symbol index…”](http://stackoverflow.com/questions/10766256/linker-returns-relocation-has-an-invalid-symbol-at-symbol-index) – steeldriver Dec 21 '15 at 12:53

1 Answers1

5

To compile and run a .cpp file in Ubuntu, follow this example guide

First we need a .cpp file, we will save it as main.cpp and in home/user/documents

#include <iostream>
using namespace std;

int main ()
{
cout << "Hello World!";
return 0;
}

If you need it install g++

sudo apt-get install g++

Now we have an program, we can go to Home/user/documents and compile it

cd ~/Documents
g++ main.cpp 

This will produce a file named a.out in your /home/user/Documents directory

Now run the a.out

./a.out

Output will be

Hello World!
Mark Kirby
  • 18,289
  • 19
  • 78
  • 113