0

I am trying to compile my C++ programs that I have written on my flash drive. I am in the terminal right now, and currently inside of the correct file (in my flash drive) that I want to compile. However, when I run these commands:

$ g++ -std=c++11 -main.cpp -o main

$ ./main

I am getting this error: bash: ./main: Permission denied

Usually when I run these commands in my OS Documents folder, I have no issues. Why am I getting this error?

Thank you.

3
  • How did you mount the drive?
    – user323419
    May 19, 2016 at 16:55
  • Probably because you are trying to store the compiled program on flash. Try changing the output file to somewhere on HD to see if this is the problem. Or look here for how to mount USB with executable permissions: askubuntu.com/questions/499275/… May 19, 2016 at 16:56
  • @TheBrownOne I don't think I mounted this stick. I just formatted it and inserted into my USB port.
    – user529161
    May 19, 2016 at 16:57

1 Answer 1

0

chmod u+x program_name. Then execute it.

If that does not work, copy the program from the USB device to a native volume on the system. Then chmod u+x program_name on the local copy and execute that.

Unix and Unix-like systems generally will not execute a program unless it is marked with permission to execute. The way you copied the file from one system to another (or mounted an external volume) may have turned off execute permission (as a safety feature). The command chmod u+x name adds permission for the user that owns the file to execute it.

That command only changes the permissions associated with the file; it does not change the security controls associated with the entire volume. If it is security controls on the volume that are interfering with execution, then you can remount the volume with options to allow execution. However, copying the file to a local volume may be a quicker and easier solution.

2
  • So if I were trying to compile a program with the initial command of $ g++ -std=c++11 -main.cpp -o main What would be the new command using chmod?
    – user529161
    May 19, 2016 at 17:04
  • your file main.cpp don't have execution permission, so try running chmod u+x file_name, in your case file name will be main.cpp here.
    – Sinscary
    May 19, 2016 at 17:10

You must log in to answer this question.