Hassan Qayyum
Hassan Qayyum

Reputation: 91

How to run c code in Linux if linux is installed on usb

I've installed Linux mint on usb as my hard drive got really slow. Now I want to compile and run C code. I successfully compiled it but as linux is in usb and I've to store program in one of my hard drive NTFS/FAT partitions so I'm getting bash permission denied error what should I do to run the code? I cannot store the program in usb(Linux partition)

Upvotes: 3

Views: 372

Answers (3)

user16654751
user16654751

Reputation: 1

I Had same program this is what worked, for a console HELLO WORLD test on Debian Linux.

Simply compiled with syntax: gcc hello.c -o hello.exe

Compiled in an instant and ran program at command line via:. ./hello.exe

Upvotes: 0

rodrigo
rodrigo

Reputation: 98506

Probably your problem is that the NFS/VFAT systems are mounted with the noexec flag or maybe the showexec flag. It instruct the kernel not to run any executable from these partitions (a security measure).

If it is showexec, then it is simply a matter of naming your executable with an .exe, .com or .bat extension (yes, even if it is a Linux executable, the vfat driver uses the extension to infer the executable permission bit).

If it is noexec, read on...

On older kernels you could bypass this with the /ld-*.so trick, but as man mount comments:

noexec: [...] (Until recently it was possible to run binaries anyway using a command like /lib/ld*.so /mnt/binary. This trick fails since Linux 2.4.25 / 2.6.0.)

If my guess is correct, you have several options:

A. Remove the flag from the partition, with this command as root:

mount -o remount,exec <mount-point>

B. Find out why your partitions have this flag, which program does it (gnome-disks or whatever) and change it.

C. Compile your program to another partition, if not in the USB partition, then for example in a tmpfs:

mkdir exe
sudo mount -t tmpfs exe exe

And then, when you compile your program:

gcc test.c -o exe/test

But beware! A tmpfs is volatile and will disappear when you umount it or shut down the machine. You can make a permanent partition-in-a-file:

truncate -s 512M exe.img
mkfs.ext4 exe.img
mkdir exe

Then, to mount the image each time you boot the machine:

sudo mount -o loop exe.img exe

Upvotes: 3

ams
ams

Reputation: 25599

Copy the file to /tmp, set the execute permission, and you should be good to go, until you reboot and then you'll have to repeat it.

cp /path/to/wherever/myprogram /tmp/myprogram
chmod +x /tmp/myprogram
/tmp/myprogram

Upvotes: 2

Related Questions