LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
LinkBack Search this Thread
Old 06-22-2013, 09:28 AM   #1
EldonCool
Member
 
Registered: Dec 2010
Location: Indiana
Distribution: Slackware
Posts: 70

Rep: Reputation: 0
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?
 
Old 06-22-2013, 09:36 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 1,172

Rep: Reputation: 261Reputation: 261Reputation: 261
What directory are you in?
Also try the command file a.out
 
Old 06-22-2013, 10:16 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 2,867

Rep: Reputation: 700Reputation: 700Reputation: 700Reputation: 700Reputation: 700Reputation: 700Reputation: 700
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.
 
1 members found this post helpful.
Old 06-22-2013, 10:21 AM   #4
Alien Bob
Slackware Contributor
 
Registered: Sep 2005
Location: Eindhoven, The Netherlands
Distribution: Slackware
Posts: 5,115

Rep: Reputation: Disabled
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
 
Old 06-22-2013, 12:23 PM   #5
Kallaste
Member
 
Registered: Nov 2011
Posts: 343

Rep: Reputation: Disabled
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.
 
Old 06-22-2013, 04:15 PM   #6
EldonCool
Member
 
Registered: Dec 2010
Location: Indiana
Distribution: Slackware
Posts: 70

Original Poster
Rep: Reputation: 0
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.

Last edited by EldonCool; 06-22-2013 at 04:17 PM.
 
Old 06-22-2013, 04:41 PM   #7
dr.s
Member
 
Registered: Feb 2010
Distribution: Slackware64
Posts: 126

Rep: Reputation: 19
Quote:
Originally Posted by tronayne View Post
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).
 
Old 06-22-2013, 05:47 PM   #8
Kallaste
Member
 
Registered: Nov 2011
Posts: 343

Rep: Reputation: Disabled
Quote:
Originally Posted by EldonCool View Post
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
 
Old 06-23-2013, 06:21 AM   #9
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 2,867

Rep: Reputation: 700Reputation: 700Reputation: 700Reputation: 700Reputation: 700Reputation: 700Reputation: 700
Quote:
Originally Posted by dr.s View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling and running programs in Lucid Puppy 2.5.2 Atharv Patil Puppy 6 03-30-2012 09:15 AM
compiling and running gtk programs in linux gibin Linux - Newbie 1 06-24-2010 02:46 PM
Need math refresher course Virtual Circuit General 2 04-10-2008 08:25 AM
refresher gentoo stuff patientzero Linux - General 4 04-18-2004 01:32 PM
I need help with compiling my C++ programs. bitessy Programming 3 02-23-2004 06:07 PM


All times are GMT -5. The time now is 03:04 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration