LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   refresher course in compiling and running C programs (http://www.linuxquestions.org/questions/slackware-14/refresher-course-in-compiling-and-running-c-programs-4175466991/)

EldonCool 06-22-2013 10:28 AM

refresher course in compiling and running C programs
 
It has been a while since I compiled and ran C programs
in unix or linux and I am presently having trouble.

As memory serves me I used to do something like
this to compile and run a program. The one I am
doing now is kmp.c and here is an actual cut
and paste of my attempt:
Quote:

bash-4.2$ cc kmp.c
bash-4.2$ ls -l a.out
-rw-rw-rw- 1 root root 7132 Jun 22 07:13 a.out
bash-4.2$ ./a.out
bash: ./a.out: Permission denied
bash-4.2$ chmod a+x a.out
chmod: changing permissions of 'a.out': Operation not permitted
bash-4.2$
As you can see I used the command "cc kmp.c",
and it produced the file output of a.out with no errors,
the output was not executable, so I tried to make it
executable with chmod a+x,
Operation was not allowed.

I also tried switching to root and do the chmod a+x.
Here is the result of that:
Quote:

bash-4.2$ su
Password:
bash-4.2# whoami
root
bash-4.2# chmod a+x a.out
bash-4.2# ls -l a.out
-rw-rw-rw- 1 root root 7132 Jun 22 07:13 a.out
bash-4.2# ./a.out
bash: ./a.out: Permission denied
bash-4.2#
As you can see, I got no error message on
trying to make the file a.out executable,
but it did not work, was still not executable
according to the above, and still permission
was denied to run the program.

So, what am I missing here?

smallpond 06-22-2013 10:36 AM

What directory are you in?
Also try the command file a.out

tronayne 06-22-2013 11:16 AM

You might be able to save yourself a lot of time and trouble if you
Code:

make kmp
Your source is kmp.c your output will be kmp and it will be executable (if, of course, you can write in the directory!).

Let's say you've got hello.c:
Code:

cat hello.c
#include <stdio.h>

int        main (void)
{
        printf ("Hello, world\n");
}
make hello
cc    hello.c  -o hello
hello
Hello, world

The make utility "knows" how to compile a C source file into an executable.

Notice that what make did was cc hello.c -o hello? That's how you doit toit.

Hope this helps some.

Alien Bob 06-22-2013 11:21 AM

Another possibility: your working directory is in /tmp or in a RAM-based tmpfs and you have one or more of the flags "nodev,nosuid,noexec" in the computer's /etc/fstab entry for /tmp . In particular "noexec", thereby preventing any executable from executing.

Eric

Kallaste 06-22-2013 01:23 PM

Are you perhaps trying to do this on a file system that doesn't support Linux permissions, such as a FAT-32 formatted flash drive or a Windows partition mounted under Linux? Such cases would cause errors like that I believe.

EldonCool 06-22-2013 05:15 PM

You figured it out. The key was the comment on what directory
I was in. I was in /d/cee directory. This is a directory
of C programs I keep on a flash drive, so that I have them
available to run on Windows or Linux. All I have to do is
some editing of my include files and then I can compile
the programs on linux or windows.

The problem apparently was that /d is a FAT file system, and
it apparently cannot let linux figure out what is an executable
program.

To fix it I used the suggestion of compile it this way:

cc kmp.c -o kmp


and I changed it slightly so that the output was on a linux
drive, like this:

cc kmp.c -o /home/joe/kmp

Now linux can run it. I could also have just copied over
the file from the FAT type directory, and changed it
to executable like this:

cc kmp.c
cp a.out /home/joe
chmod a+x /home/joe/a.out

The whole problem centered around how linux lets programs
be executable.

Thanks for the help, you got my problem fixed.

dr.s 06-22-2013 05:41 PM

Quote:

Originally Posted by tronayne (Post 4976615)
You might be able to save yourself a lot of time and trouble if you
Code:

make kmp
Your source is kmp.c your output will be kmp and it will be executable (if, of course, you can write in the directory!).

Let's say you've got hello.c:
Code:

cat hello.c
#include <stdio.h>

int        main (void)
{
        printf ("Hello, world\n");
}
make hello
cc    hello.c  -o hello
hello
Hello, world

The make utility "knows" how to compile a C source file into an executable.

Notice that what make did was cc hello.c -o hello? That's how you doit toit.

Hope this helps some.

Thanks for the tip, I'd thought a makefile was needed before using make (I'm not a C expert).

Kallaste 06-22-2013 06:47 PM

Quote:

Originally Posted by EldonCool (Post 4976776)
The problem apparently was that /d is a FAT file system, and
it apparently cannot let linux figure out what is an executable
program.

Yes, FAT filesystems do not support Linux permissions. Permissions are stored in the file's inode on Linux or Unix type filesystems, but FAT does not even use inodes. In order to access FAT partitions, the Linux kernel assigns a random but unique inode number to a file on these partitions when it is accessed. This is what I was trying to say in my post #5 above, but perhaps I wasn't clear enough. Wasn't sure that was your problem though, since I wasn't sure you were using a flash drive.

If you want to be able to work on your flash drive, you can add an entry for the device in your /etc/fstab using "umask=000" as a mount option. This specifies the default permissions for all files on the given filesystem; a umask value of 000 would mean you have octal permissions of 777 for everything on the drive. Then you could run the program from there.

More about /etc/fstab: http://www.tuxfiles.org/linuxhelp/fstab.html

tronayne 06-23-2013 07:21 AM

Quote:

Originally Posted by dr.s (Post 4976786)
Thanks for the tip, I'd thought a makefile was needed before using make (I'm not a C expert).

For many tasks, make does not require a Makefile; e.g., if you write a shell program (a "script") that will be around for more than five minutes, you may take the software development route, writing the program as "prog.sh" then adding it to a source code repository (with CVS or whatever your favorite source management software may be).

Then you've got prog.sh.

You can force people to actually type prog.sh to use it (how... Windowsy is that) or you can simply
Code:

make prog
Here's what happens:
Code:

fubar-trona-/tmp: cat > prog.sh
echo "Hello, world."
fubar-trona-/tmp: cat prog.sh
echo "Hello, world."
fubar-trona-/tmp: make prog
cat prog.sh >prog
chmod a+x prog

fubar-trona-/tmp: prog
Hello, world.

The user sees an executable program named prog (not something that requires typing a file name extension), hasn't got any more idea of what it is, just that it's a program to be used to accomplish something.

The above is trivial, of course, but there are hundreds of shell program spread around the systems -- 450 in /bin on my system, all of which are under source code control and are "compiled" with make or some offshoot of make (just like everything else).

Hope this helps some.


All times are GMT -5. The time now is 12:15 AM.