ld: No such file or directory

This is a discussion on ld: No such file or directory within the Linux Programming forums, part of the Platform Specific Boards category; Hello all! I am writing a program in x86 asm, using nasm syntax, and using opengl and sdl. I assemble ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    10

    ld: No such file or directory

    Hello all!

    I am writing a program in x86 asm, using nasm syntax, and using opengl and sdl. I assemble my program with:
    nasm -f elf main.asm
    and link with:
    ld -o main main.o /usr/lib/libGL.so /usr/lib/libGLU.so /usr/lib/libSDL.so

    so far so good. It produces an executable with no issues. However, when I try to run it with
    ./main
    It says "No such file or directory"

    What is going on here? When I do ls, main shows up.

    Thanks!!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > It produces an executable with no issues.
    It may have produced a file, but how do you know it's executable?

    If you do 'ls -l', does the file have the 'x' permission bits set?

    Can you post the actual terminal session. Perhaps there is a typo in what you typed which is not reflected in your post.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    Here is the session:

    kerry@BigOwl:~/Programming/x86_asm/extern_func_03$ ls
    main.asm main.asm~ makefile makefile~
    kerry@BigOwl:~/Programming/x86_asm/extern_func_03$ make
    nasm -f elf main.asm
    ld -o main main.o /usr/lib/libGL.so /usr/lib/libGLU.so /usr/lib/libSDL.so
    kerry@BigOwl:~/Programming/x86_asm/extern_func_03$ ./main
    bash: ./main: No such file or directory
    kerry@BigOwl:~/Programming/x86_asm/extern_func_03$ ls -l
    total 24
    -rwxr-xr-x 1 kerry kerry 3009 2007-09-02 02:42 main
    -rw-r--r-- 1 kerry kerry 1585 2007-09-02 02:42 main.asm
    -rw-r--r-- 1 kerry kerry 1581 2007-09-02 02:05 main.asm~
    -rw-r--r-- 1 kerry kerry 1760 2007-09-02 02:42 main.o
    -rw-r--r-- 1 kerry kerry 364 2007-09-02 02:41 makefile
    -rw-r--r-- 1 kerry kerry 364 2007-09-02 01:45 makefile~
    kerry@BigOwl:~/Programming/x86_asm/extern_func_03$


    I think it's some weird bug with ld, but maybe I am not linking right? It just doesn't make sense. ls can find the file, but bash can't.

    Thanks!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Can you do 'ls -lb' in case make is generating a file which only looks like 'main', but in fact contains invisible characters. The -b option will show these up. If this is the case, then you need to carefully inspect your makefile using an editor which will reveal such invisible characters.

    Also, try typing './m' then pressing the tab key to cause bash to attempt it's own filename completion. If it comes back with something looking like './main', then press enter and see if that runs the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    ls -lb gives the same result as ls -l

    and ./m tab causes it to auto complete with ./main (but still giving me the same error when i run it!)

    thanks!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you tried giving the file a different name? I don't think "main" is some sort of reserved name, but worth a try.

    It may be that it's something else going wrong (and the error message is a bit misleading). Try "strace ./main" - post the part towards the end if you're not familiar with "strace" output (the last several lines will have to do with printing the "file not found" error message - that's not the really interesting bit, but what happens before that for several lines would be).

    Also, if there's a space or some such at the end of the name, such as "main ", then it would stop at "main" when auto-expanding, because it's ambiguous between "main ", "main.c" and "main.o" for example.

    --
    Mats

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    The file definitely exists, I have tried changing the name and everything. Here is the output from strace:

    Code:
    kerry@BigOwl:~/Programming/x86_asm/extern_func_03$ strace ./main
    execve("./main", ["./main"], [/* 31 vars */]) = -1 ENOENT (No such file or directory)
    dup(2)                                  = 3
    fcntl64(3, F_GETFL)                     = 0x2 (flags O_RDWR)
    fstat64(3, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 2), ...}) = 0
    mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f13000
    _llseek(3, 0, 0xbf910a04, SEEK_CUR)     = -1 ESPIPE (Illegal seek)
    write(3, "strace: exec: No such file or di"..., 40strace: exec: No such file or directory
    ) = 40
    close(3)                                = 0
    munmap(0xb7f13000, 4096)                = 0
    exit_group(1)                           = ?
    Process 13595 detached
    I really think that it has something to do with how i am linking, but I am not sure why that would give me a "no such file or directory" error.

    Thanks!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But your strace clearly shows that the OS doesn't think your file exists!!
    The thought I had was that it was somehow a shared library or some such that didn't exist.

    Try this: "mv main blah" - if that shows the file doesn't exist, then it's certainly something up with the filename.

    --
    Mats

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    mv works. every file manipulation tool, such as ls, mv, cp, works fine on it. is this a bug with linux??

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kerryhall View Post
    mv works. every file manipulation tool, such as ls, mv, cp, works fine on it. is this a bug with linux??
    I doubt it's a bug in Linux - Linux may well have bugs, but something as trivial as this shouldn't happen in your kernel.

    It seems from strace that it's simply not finding the "main" file - which means to me that it's "not there" in the filesystem in general. Unless of course some security scheme is saying "don't let this user run anything" - quite often, the error codes from such security schemes are "obfuscated" to hide that it's the security system saying you can't do this - instead of saying "You are not allowed to do this", it says "the file doesn't exist", that way you don't KNOW that it's the security system that is causing the problem.

    So does the new file (blah or whatever) work to run? If you copy, say, grep to main, does it work then?, using "cp /usr/bin/grep main" (or similar). Can you build a simple hello program?

    Anoter option is that the file is refused to load (and the kernel doesn't distinguish on a fine-grain enough) because there's something else wrong with your load process. Could you dump with "objdump -d" the first few lines of assembler, and compare that to for example a C program (grep will work) to see if there's some noticable difference - if you are not sure, post some 10-15 lines of each here and I should be able to do that.

    --
    Mats



    --
    Mats

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    In addition, do programs created with say gcc run as expected?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Hey Kerryhall, what does the output of "ldd ./main" say?

    Programs on Linux can give a "No such file or directory" when they aren't dynamically linked quite right.
    Last edited by brewbuck; 09-03-2007 at 11:58 AM.

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    programs created with gcc work fine.

    Am I linking right?
    my assembling and linking commands:
    Code:
    nasm -f elf main.asm
    ld -s -o main main.o /usr/lib/libGL.so /usr/lib/libGLU.so /usr/lib/libSDL.so
    there doesn't seem to be anything significant with objdump

    and here is the result of ldd
    Code:
    kerry@BigOwl:~/Programming/x86_asm/extern_func_03$ ldd main
    /usr/bin/ldd: line 117: ./main: No such file or directory
    thanks all!!

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    The file's there, it's executable, objdump thinks it's okay, but ldd borks too. Well, ldd works by setting some magic environment variables and then running the binary, so that would be expected.

    This is just a wild guess, but can you run ANY program called ./main? Try copying /bin/ls to ./main and see if that will run.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    Yes, I can run any program called main.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21