Search code examples
clinuxgccmakefile

Linux doesn't want run a compiled file


I create a parser engine for Gold Parser, improving an existing one adding whole support for UTF-8 or UNICODE and error handling. And I want make it available also on Linux and therefore I want test it on Linux and at the same time improve my skills with this SO(Ubuntu 22) and learn Makefile.

In hurry for ask this question on Stack Overflow I uploaded all on github. The library is mparser, main contains the test functions and dhash is my tool for the debug(just checks the memory leaks) I included dhash so I have a more complicated scenario to learn Makefile.

What is the problem: It does not run on Linux. I copied the Makefile from this site Make tutorial, after I understood it closely 80% I did some changes to run with my project, it is available on GitHub but I post it also here:

IDIR =include
CC=gcc
CFLAGS=-I$(IDIR)

ODIR=obj
LDIR =../lib

LIBS=-lm

_DEPS = dhash.h mparser.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
FDEPS =$(_DEPS)

_OBJ =  mparser.o dhash.o main.o 
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


$(ODIR)/%.o: %.c $(FDEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

mparser: $(OBJ)
    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

clean:
    rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ 

In Linux I added the current directory . to the path as suggested here.

I run file mparser and it says:

mparser: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=7b91119ab5e7a335c16c61c883ad5092da23eab5, for GNU/Linux 3.2.0, not stripped

So there is the magic word executable as suggested here. But if I launch mparser it says denied access. I tried also with whole path, /media/jurhas/INTENSO/mparser, and doesn't work either.

How can I run my program on Linux after I compiled it?

Edit:

The shell


Solution

  • ": Permission denied" when trying to execute something much always means about linux file permissions.

    You are trying to "execute" a file on a Linux filesystem that does not have the executable flag.

    Here is the simplest way to view if a file is executable:

    user@host:~$ ls -lah file.txt 
    -rw-rw-r-- 1 myuser myusergroup 0 Jan 23 13:37 file.txt
    

    You want to look at this: -rw-rw-r--

    It states:

    • first characters (-): the dash means its a file (and, for example, not a directory)
    • char 2-4 (rw-): the owner of the file (myuser) is allowed to read (r) write (w) and NOT execute (-) the file
    • char 5-6 (rw-): the group of the file (myusergroup) is allowed to read (r) write (w) and NOT execute (-) the file
    • chat 7-8 (r--): everyone who is not the owner of the file (myuser) or in the group that owns the file (myusergroup) is ONLY allowed to read (r) and NOT allowed to write or execute (--) the file

    rw-rw-r-- is the default for most Linux distributions.

    What you want to do is make your file executable. There are a number of ways for that.

    chmod +x

    # before
    user@host:~$ ls -lha file.txt 
    -rw-rw-r-- 1 user user 0 Jan 23 13:37 file.txt
    
    # give everyone executable permissions
    user@host:~$ chmod +x file.txt 
    
    # after
    user@host:~$ ls -lha file.txt 
    -rwxrwxr-x 1 user user 0 Jan 23 13:37 file.txt
    

    Now EVERYONE can execute your file. This is most likely not what you want.

    chmod u+x

    user@host:~$ ls -lha file.txt 
    -rw-rw-r-- 1 user user 0 Jan 23 13:41 file.txt
    user@host:~$ chmod u+x file.txt 
    user@host:~$ ls -lha file.txt 
    -rwxrw-r-- 1 user user 0 Jan 23 13:41 file.txt
    

    You can use u+x as argument to chmod to give ONLY the user that owns the file execute permissions.

    If this does not help try mount | grep noexec. If your file is located in a filesystem that is mounted with noexec, you are unable to execute files in there. You can update your /etc/fstab (the file that lists whats to be mounted on boot), remove the noexec and remount the directory using mount -o remount /path/to/mounted/dir