Having trouble executing a compiled C program

5,809

Since you're using the -o option to rename the compiled program, the a.out you have there now belongs to something else (earlier version or something completely unrelated).

Your program probably is running but just isn't returning any output. Check the file size and run file program_name to convince yourself that it's an executable then see what the code should do -- take an argument perhaps.

Share:
5,809

Related videos on Youtube

d0rmLife
Author by

d0rmLife

Updated on September 18, 2022

Comments

  • d0rmLife
    d0rmLife over 1 year

    I am running an Ubuntu v10.04 through VirtualBox. To test the persistence of files I create in the virtual machine, I wrote a very basic C program. When I shutdown the virtual machine and restart it, I see that the files have been retained.

    What happens:

    • I can check the directory with ls /root/ and see both program_name.c and the compiled program_name

    • I use this command to compile: gcc program_name.c -o program_name

    • I can run ./a.out and the program will execute properly.

    • Opening the C code with nano program_name.c shows the program, as I expect it to be.

    The problem is...

    • When I try to the program via ./program_name, nothing happens!

    • If I check the exit status (echo $?), it is zero.

      I am having difficulties understanding why I can't run the compiled C program, even though I can find it. It is most bizarre to me considering I can locate all the files AND ./a.out executes it properly.

    EDIT:

    Edited for clearer presentation.

    Edit regarding answer...

    In the off chance that someone else comes to this looking for help, it should be noted that while the file program_name command did expose that the file was blank, the cause hasn't been determined. I successfully ran the executable before I restarted the virtual machine.

    Perhaps I didn't shut down the virtual machine properly? Anyways, checking the file was/is certainly helpful!

    • Renan
      Renan about 11 years
      What command line are you using to build the program? If you're using cc program_name.c you will get an a.out file; try cc -o program_name program_name.c, then.
    • d0rmLife
      d0rmLife about 11 years
      @Renan I am using gcc as that is what I am accustomed to. gcc program_name.c -o program_name
    • Admin
      Admin about 11 years
      @d0rmLife with the -o flag passed to compiler, you specify the name for the output file as prog_name. I'd suggest reading the man page for gcc with command man gcc, which will help you use it better and learn quickly.
    • d0rmLife
      d0rmLife about 11 years
      @G.Kayaalp Is that not indicated by how I input it? I have used that convention for other linux systems as well as OSX with no problems.
    • daisy
      daisy about 11 years
      Maybe you can upload the source code somewhere for inspection?
    • goldilocks
      goldilocks about 11 years
      @d0rmLife I removed the '.o' in the title since there is no .o file in the question. FYI: The -o switch is completely unrelated to '.o' files, but they are both cc related concepts, so we need to avoid confusion ;) '.o's are actually created with -c -- you'll get to that later...
    • d0rmLife
      d0rmLife about 11 years
      @goldilocks Thank you for explaining the edit, as that seems to be a rare courtesy. More importantly, I appreciate the explanation of gcc ... -o verse file.o !
    • d0rmLife
      d0rmLife about 11 years
      @warl0ck The source code was good (I'm not bragging, it was supremely trivial!); for some reason the file seems to have been corrupted. Thanks for looking, though!
  • igelkott
    igelkott about 11 years
    Btw, the title is misleading since this doesn't involve ".o" (object) files.
  • d0rmLife
    d0rmLife about 11 years
    You are correct, the file was corrupted somehow. Considering I executed the file before I restarted the virtual machine, I'm not exactly sure why that happened. I will vote you up when I acquire the requisite rating.
  • collegian
    collegian about 11 years
    Maybe cache was not written to disk. Did you shut down the VM cleanly?