Results 1 to 9 of 9

Thread: C++ and a.out

  1. #1
    Joined
    May 2002
    Location
    Madison, WI
    Posts
    439

    C++ and a.out

    I am a complete newbie to Linux. I have had experience with UNIX at school. I attend the University of Minnesota. I took a Java programming class last semester, and am now currently enrolled in a C++ programming class. It is going well so far.

    Anyways, we use Red Hat Version 6 on some of the computers at the IT labs here at the UMN. I use that for my C++ programming in the labs. Having the need to reinstall Windows XP, I felt like dual booting, so I installed Red Hat Linux 8.0. I am familiar with Emacs, which comes installed in Red Hat. I used it to write a fairly simple program to display some text. I compiled the .cpp file with the G++ compiler using a terminal window. This creates an a.out file. In the computer labs of the IT building on campus, merely typing in a.out immediately executes the file. In this case, on my computer, running Red Hat 8.0, it does not recognize it as a command within the terminal window. Then if I type "bash a.out" (without quotes) it says it cannot execute it because it is binary.

    I am hoping to solve this issue. Is there any simple way? If anymore information is required, please ask for it. Is there something that the IT department screws with to make the a.out file executable? Thank you.

    BTW - when in the terminal, it lists the directory where the file is stored, with blue text, and the name of the a.out file in green text. All other files are standard black text. I am sure the blue stands for directories, but what does the green stand for? Again, thanks for any assistance.

  2. #2
    Joined
    Jun 2001
    Posts
    875
    Firstly be aware that C++ binary compiled on one compiler will not work on a system using a different version of that compiler and libstdc++ due to differences in the C++ ABI. This means that stuff compiled on RH8 will not work on RH6, although something compiled on RH6 may work on RH8 if you install the very old "compat" library. I don't know if that old compat library is still available.

    That being said, the ABI thing shouldn't be a problem since C++ is supposed to be a portable language, meaning you can simply recompile the source code on any platform and it should work.

    In your case the problem is probably only file path. (I think the compiler by default makes the file executable, so don't have to worry about changing permissions.) In DOS and certain unix setups the current directory "." (a single dot) is included in the PATH variable meaning you can run programs in the current directory. That may be how your Unix lab is setup at school.

    However, most Unix folks absolutely think it is a bad idea to add "." to your PATH for a wide variety of reasons including security. Another reason is to reduce confusion, because adding "." to your PATH would make it the only PATH that changes dynamicallyi depending on your current location (pwd). This can be confusing or potentially disasterous if you happen to have a program in the current directory with the same name as something else you really want to run.

    So anyway, people normally run things in the current directory with:

    ./programname

    Or in your case:

    ./a.out

    However it is not "clean" to use the default output name. You may want to use g++ like the following.

    g++ -g sourcefile.cpp -o programname

    -g makes debugging symbols making it easy to debug using a debugger like gdb (or others if you have them)
    -o allows you to specify an output name, be creative.

    http://cygwin.com
    One more thing, you can have the same bash shell, gcc, g++ and most other Linux tools in Windows if you install Cygwin. Cygwin is a Unix API implementation and tools for Windows. When you install Cygwin be sure to choose things like "make" and "gcc", there are many different package options available.

  3. #3
    Joined
    May 2002
    Location
    Madison, WI
    Posts
    439
    Much appreciated, very thorough, and you explained enough for me to understand. I'll put your advice to use, and see how it goes.

    Thanks.

    Turns out that using the ./a.out worked. Thank you for your assistance. Very nice explanation. BTW - does the green color mean it is an executable file?
    Last edited by Vellucci; 02-03-2003 at 07:35 AM.

  4. #4
    Joined
    Dec 2001
    Location
    Hell 18th
    Posts
    592
    Originally posted by Vellucci
    Much appreciated, very thorough, and you explained enough for me to understand. I'll put your advice to use, and see how it goes.

    Thanks.

    Turns out that using the ./a.out worked. Thank you for your assistance. Very nice explanation. BTW - does the green color mean it is an executable file?
    ya. green means executable.

    and you can set your path too:

    PATH=$PATH:.

    put the dot . at the last after the colon. then you can directly type 'program' to run your program. for some reasons, this is not recommended, but for normal use, it is OK and saves time.

  5. #5
    Joined
    Jan 2002
    Location
    Nashville, TN
    Age
    34
    Posts
    3,369
    Its not recommended because if there is an executable with the same name as something in your path it will run the one in the current directory. This could be a security problem if you download a file and untar it, but there is a worm or something called "make" so when you try make what you downloaded the worm goes off.
    i code therefore i am

  6. #6
    Joined
    May 2000
    Location
    Paris, France
    Posts
    5,298
    Eggs is right, Linux is secure as long as you don't repeat the windows faults, as this or browse the net as root.

  7. #7
    Joined
    Aug 2001
    Location
    Minnesota
    Age
    31
    Posts
    1,798
    Are you in the Introo to c++ programming class? I took that last semester at UMN, pretty good class, except the labs took progressivly longer as the semester moved on. The final project was pretty interesting though, delt with ai, and creatures in an artificial world. I'd recommend working on stuff from home, but ssh'ing to the labs. Feel free to ask questions about problems with any of the labs.

  8. #8
    Joined
    May 2002
    Location
    Madison, WI
    Posts
    439
    You are exactly right nmayotte. I took Intro to Java Programming last semester while you were in C++. I already use Putty for a simple way to logon remotely using ssh. I will let you know if I need any help. Having some programming experience already has enabled me to be the first one out of my labs - so far. I will PM you if I need any help.

    Thanks.

  9. #9
    Joined
    Oct 2001
    Location
    Southern Ontario
    Age
    40
    Posts
    13,183
    I cann't get over just how many times I've had a problem and then found the answer in a current post.

    Thanks Warren! I could figure out why I couldn't get programs to run on my one Linux box... I had to ad ./ to the start of the application's name.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •