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.

14
  • 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
  • 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
  • 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? Dec 21 '15 at 12:35
  • 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…” Dec 21 '15 at 12:53
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.