I have written a C++ program and complied it to produce a.out file. However, whenever I try to run it, I get Permission Denied. I read that we can use sudo, but I can't quite get it to work. I use something like, sudo "./a.out" but that too doesn't work.
Edit:
Here is the message I get when I try "./a.out".
bash: ./a.out: Permission denied
Usually,
g++
gives the created file execute permissions. If you do not pass the-o
option, the file will be nameda.out
.Two possible reasons why your file does not have the execute bit set, with their solutions:
The umask value is set to a value like 0133, thereby preventing the execute bit from being set. Solution: set the permissions explicitly:
fmask=0022
orumask=0022
(omittingfmask
). See the Mount options for fat section on the manual page of mount for more details.For bash scripts which do not have the execute bit set, you could run
bash file.sh
. Such a feature exists for all files with executable content (compiled files and files with a shebang line#!/path/to/interpreter
set). To execute files without the execute bit set, use the special file/lib/ld-linux.so.2
(or/lib/ld-linux-x86-64.so.2
for 64-bit applications) to run such a program:.out is an unusual extension. Usually this would normally signify a "trace output" file.
Check your syntax that you are using to compile
e.g.
or maybe
You can should examine to see if the executable bit is set on the file
or you can just force the executable bit
then you can run your file
or simply
You should also perhaps check that the output file has been written correctly as a binary
i.e.
This will report what format the file is - either a script or a binary
You rarely need to execute as root unless you have restricted who should be able to run the executable.
If you have compiled as root (e.g. sudo make), or have a Makefile that installed the executable as root then can I suggest you regain the permission as the user logged in
i.e.
i.e. replace "fred" with your user id.
Just copy the folder to your home folder and it will work. You are probably trying to run it on an external drive or something.
The workaround for FAT-filesystems in the first answer
"This could be the case if you're putting files on a FAT32-formatted flash drive. Solution: (...) mount the drive with fmask=0022 or umask=0022 (omitting fmask)."
normally does not work - the default for umask is mostly 0022 anyway, so this does not change anything.
Another mount parameter's default, however, effectively disallows execution of binaries, especially if the FAT-filesystem is mounted as non-root-user:
noexec
So just mount FAT-formatted drives with the option
exec
like so:(this must normally be done as root, hence the "sudo") and you should be able to execute binaries directly from there.
I'd wager that your program does not have a 'main()' function, as if it did, your compiler would have made a.out executable. Right now it is just an object file full of code, but there's no entry point. main() is a special function name in C and C++ that tells the compiler to create a program rather than just object files which can be linked to a program or library.
I'd be interested to know what command line you used to produce this file, as GNU GCC's c++ compiler, g++, will not let me create a simple program w/o a main function:
However if I make change 'void no_main' to 'int main' it works: