Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question
    
What is the output of ls -l a.out? What is the output of file a.out? Is the executable on a USB memory stick / Harddisk? –  JRT May 23 '11 at 13:19
    
Its on Harddisk. Its the same file created soon after the the compilation process. I didn't set any other parameters. –  Shamim Hafiz May 23 '11 at 16:39
    
-rw------- 1 shamimhafiz shamimhafiz 7721 2011-05-22 23:30 a.out Is the output of ls -l a.out –  Shamim Hafiz May 23 '11 at 17:49
    
So based on the output of ls -l a.out it's a permissions problem. If you do chmod +x a.out then try ./a.out does it execute? What is the output of umask? –  JRT May 23 '11 at 22:04
    
@JRT: It doesn't execute. Doing "chmod +x a.out" doesn't give any message, but looks like it has no effect. Trying "ls -l a.out" again still shows the same thing. –  Shamim Hafiz May 24 '11 at 5:32

5 Answers 5

up vote 13 down vote accepted

Usually, g++ gives the created file execute permissions. If you do not pass the -o option, the file will be named a.out.

Two possible reasons why your file does not have the execute bit set, with their solutions:

  1. The umask value is set to a value like 0133, thereby preventing the execute bit from being set. Solution: set the permissions explicitly:

    chmod 755 a.out

  2. The filesystem you're working on does not support Linux permissions. This could be the case if you're putting files on a FAT32-formatted flash drive. Solution: either backup the files and format it to ext2 or mount the drive with fmask=0022 or umask=0022 (omitting fmask). See the Mount options for fat section on the manual page of mount for more details.

For bash scripts which does 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 bits applications) to run such a program:

/lib/ld-linux-x86-64.so.2 a.out
share|improve this answer
    
This answer is more interesting, just to add, the way I Installed Ubuntu is over Windows and a folder named was created on C drive(windows installation drive). This drive is formatted as FAT32. Could this have something to do with that? –  Shamim Hafiz May 24 '11 at 5:55
1  
You've done a Wubi installation (C:\Ubuntu had been created). This should not be a problem unless you're putting files on "C:" and not your Ubuntu installation. If you're not using Windows, or have plenty of disk space, I suggest installing Ubuntu on a dedicated partition. And again, NTFS / FAT32 does NOT support Linux permissions, so you can run sudo chown user file, chmod 755 file, it won't work. You really need an EXT filesystem for that. –  Lekensteyn May 24 '11 at 8:08
3  
The same story, none of Windows filesystems supports Linux file permissions. You should put the files on a filesystem formatted EXT. –  Lekensteyn May 24 '11 at 8:17
1  
So simply working on a folder that belongs to Ubuntu should do the trick, right? I mean, I should not use folders that are not part of Ubuntus File System Structure? –  Shamim Hafiz May 24 '11 at 8:38
3  
Exactly, just stay in ~, I create ~/projects and put all my projects in it, you could do the same thing. –  Lekensteyn May 24 '11 at 8:46

.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.

gcc myfile.c /usr/lib/libsomelibrary.a -o outputfilename

or maybe

g++ myfile.cpp -lm -o outputfilename

You can should examine to see if the executable bit is set on the file

ls -l a.out

or you can just force the executable bit

chmod +x a.out

then you can run your file

./a.out

or simply

a.out

You should also perhaps check that the output file has been written correctly as a binary

i.e.

file a.out

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.

sudo chown fred:fred a.out

i.e. replace "fred" with your user id.

share|improve this answer
    
Thanks for the information. I am actually logged in as the only user, and I presume I am the main user. I am not quite sure, why I need to use administrative authentication anyway. How would I be able to remove this option so I can always run the files. –  Shamim Hafiz May 23 '11 at 12:33
    
updated - hopefully to clarify and answer your question. –  fossfreedom May 23 '11 at 12:40
    
a.out is a legacy feature of the compiler so it produces a predictable filename if you didn't ask for one. This is not a permissions problem, but a misunderstanding of how compilers and C++ work. –  SpamapS May 23 '11 at 12:43
    
Linux/Unix does not rely on file extensions to determine the file-type. Normally executable files have no extension at all. Also, the format to executable a file does not differ between shells generally. All shells should work find with ./a.out unless it is some exotic shell. –  JRT May 23 '11 at 12:54
1  
Gunner - please copy and paste the complete output in the terminal starting with your compile command, followed by the ls -l, chmod +x and finally the execution. Please confirm your name by typing "whoami" –  fossfreedom May 23 '11 at 18:32

just copy the folder to your home folder and it will work.

share|improve this answer

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:

sudo mount -o exec /dev/sd.. /mountpoint

(this must normally be done as root, hence the "sudo") and you should be able to execute binaries directly from there.

share|improve this answer

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:

#include <iostream>

using namespace std;

void no_main()
{
  cout << "Hello World" << endl;
}

$ g++ hello.cc
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 12
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 13
/usr/bin/ld.bfd.real: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 21
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

However if I make change 'void no_main' to 'int main' it works:

$ g++ hello.cc
$ ./a.out
Hello World
share|improve this answer
5  
If he had no main function, it would not link and therefore it would not produce an a.out file. –  JRT May 23 '11 at 13:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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