File permissions with gcc

Is there a way to change the default file permission of a file created by gcc? or is there a command line argument I can pass to gcc to change the file permissions upon compile? Like can I gcc main.c -f 500 with arguments to make a.out 500 upon execution or do i have to change the file with chmod every time i compile?

Have you tried setting umask to 077 ?

what is umask no i have never herd of it.

It’s a property in your process. It sets defaults for newly created file permissions.

Typically, you set it in your “.bashrc” or other shell startup file.

Try

man umask

but what if i want to only set it for gcc programs to 544?

You can use:

( umask 077; gcc somefile.c )

so that “gcc” runs in a subshell and you set the wanted umask for that subshell.

Now i dont understand what 077 did i wanted 544 for chmod but it seems different, can you explain?

wait i think i get it its like opposite if i want it to be read i have to do 033 because i don’t want 2 and 1.

Yes, I think you have got it.

I hope you read the man page as advised.

Then you will understand that umask does not set permission bits. It masks off permission bits that are intended to be set by a program. I other words, you can switch bits off from the suggested set, but you can not switch bits on from what is being suggested.

Many people have e.g. umask 027 in their .profile so that, whatever the suggestion of a program to set permissions is, nothing is allowed for the world (others), and group members can not write.

Yea I figured it out. 033 is what I wanted to mask. Wierdly if i did 733 it wouldn’t even let me make the file with gcc, i guess it checks the permissions before creating the file.

I doubt. “It” (the compiler) does not even know what the umask is (well it could check, but I doubt very much it does.). It is much more simple. The process asks the kernel to create the file with a suggestion of permission bits. The kernel then removes the bits mentioned in the umask and creates the file. After that the process can not write to the file (or read from it), because the process owner (the user) has no write permission. Go figure.