If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Welcome to the new PC Perspective forums! Have a look around and tell us what you think in our feedback forum. If you notice any bugs or style issues, please report them in this thread.

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.

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.

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.

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.

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.