Thread: ld: No such file or directory

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > 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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    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.

  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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    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.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.

  11. #11
    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!!

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.

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

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Given that programs created with gcc work fine, then I would say that there is something wrong with your ld command line.

    Try
    Code:
    gcc -v -s -o main main.o /usr/lib/libGL.so /usr/lib/libGLU.so /usr/lib/libSDL.so
    This too will just call ld, but it will use the compiler driver to make sure that all the correct options are passed to ld. The purpose of the -v option is so that you can see what all those options are.
    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.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    Sorry in advance for the extra wide post!

    Code:
    kerry@BigOwl:~/Programming/x86_asm/extern_func_03$ gcc -v -s -o main main.o /usr/lib/libGL.so /usr/lib/libGLU.so /usr/lib/libSDL.so
    Using built-in specs.
    Target: i486-linux-gnu
    Configured with: ../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr \
     --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix \
     --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug \
     --enable-mpfr --enable-checking=release i486-linux-gnu
    Thread model: posix
    gcc version 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)
     /usr/lib/gcc/i486-linux-gnu/4.1.2/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 \
    -o main -s /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crt1.o /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crti.o \
     /usr/lib/gcc/i486-linux-gnu/4.1.2/crtbegin.o -L/usr/lib/gcc/i486-linux-gnu/4.1.2 \
    -L/usr/lib/gcc/i486-linux-gnu/4.1.2 -L/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib -L/lib/../lib \
    -L/usr/lib/../lib main.o /usr/lib/libGL.so /usr/lib/libGLU.so /usr/lib/libSDL.so -lgcc --as-needed -lgcc_s \
     --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i486-linux-gnu/4.1.2/crtend.o \
     /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/crtn.o
    kerry@BigOwl:~/Programming/x86_asm/extern_func_03$

    So I think there should be something else that I should link to, but I'm not sure what.

    Also, if there is something that I don't link to, why do i get a "no such file or directory" error?

    Thanks!!
    Last edited by Salem; 09-04-2007 at 02:38 AM. Reason: wrapped for clarity

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, 10: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, 02:58 AM