tags:

views:

169

answers:

5

I am trying to compile simple program under linux. These are the set of operations I performed.

[mypc@localhost programs]$ vim heap.cpp
[mypc@localhost programs]$ g++ -c heap.cpp 
[mypc@localhost programs]$ chmod 777 heap.*
[mypc@localhost programs]$ g++ -c heap.cpp 
[mypc@localhost programs]$ ./heap.o
bash: ./heap.o: Permission denied
[mypc@localhost programs]$ ls
heap.cpp  heap.o
[mypc@localhost programs]$ ls -l
total 8
-rwxrwxrwx. 1 mypc mypc   67 2009-12-28 12:01 heap.cpp
-rw-rw-r--. 1 mypc mypc 1548 2009-12-28 12:02 heap.o
[mypc@localhost programs]$ chmod 777 heap.o
[mypc@localhost programs]$ ./heap.o
bash: ./heap.o: cannot execute binary file
[mypc@localhost programs]$ 

What kind of error is this ?

Here is a program

#include<iostream>

using namespace std;
int main(){

        return 0;
}
+3  A: 

It means the file you generated is not an executable.

You can either use the linker (ld), or compile and link in one step (preferred).

To do the latter:

g++ -o <output name> -I/include/page -Llibpath -llibone -llibtwo source1.cpp source2.cpp

That will generate an executable with everything needed to run linked to it.

Mark Renouf
ldd is not the build linker. It's a front end to the runtime linker (ld.so.1) which is used to show runtime dependencies.Also, it's not recommended to directly invoke the linker as you would then need to manually specify various support objects. It's much easier to use g++ as the link-driver even when doing separate compilation and linking.
R Samuel Klatchko
You want to use the link editor, `ld`, to create an executable not `ldd`. If you want to see exactly what is going on _under the hood_, try `g++ -v heap.cpp -o heap`. It will display all of the various steps that `gcc` does.
D.Shawley
Whoops, ld not ldd... my mistake (a little tired).
Mark Renouf
+1  A: 

The .o file is not an executable. It still needs to be linked.

Shmoopty
+9  A: 

The -c option tells the compiler to generate an object file, not the final binary. You still need to link your code.

If you only have a single file, you can do a compile and link in one step:

g++ heap.cpp -o heap

As you get to bigger programs, you will want to separate compilation from linking. Let's say you want to split your code between heap.cpp and main.cpp. First you would do a compilation step and later you would link them together:

g++ -c heap.cpp
g++ -c main.cpp
g++ -o program_name heap.o main.o

Finally, by default, the linking step creates a file named a.out. If you want so specify the name, make sure to use the -o option (which isn't necessary when compiling as the default is to convert NAME.EXTENSION to NAME.o).

R Samuel Klatchko
+1  A: 

You get the error since you're trying to execute a file which isn't an executable. The gcc(1) man page shows that the -c object merely compiles the given source file into an object file, but the object file is not linked into an executable.

Try running without -c to produce an executable file.

Frerich Raabe
+2  A: 

As everyone else has said, you need to both compile and link your C++ program for it to run. You can do this with GCC like this:

g++ -o heap heap.o

The compilation and link steps exist in both C and C++.

You also need to set proper permissions in order for your file to run. Assuming your program compiles and links without error, you should get a binary with the proper permissions, which should look something like this:

[mypc@localhost programs]$ ls -l
-rwxrwxrwx. 1 mypc mypc   67 2009-12-28 12:01 heap.cpp
-rw-rw-r--. 1 mypc mypc 1548 2009-12-28 12:02 heap.o
-rwxrwxr-x. 1 mypc mypc 1548 2009-12-28 12:02 heap

The letters on the left-hand side stand for read, write or execute. The first set of three are for the owner of the file (named mypc), the next set of three are for the group owner of the file (also named mypc, but a separate entity), and the last set of three stand for all other users of the filesystem on which these files live (aka "the world").

If you don't have the execute bit set, you can set it in Unix with this command:

chmod o+x heap

This will add the execute bit for the owner.

With that introduction provided, you might want to do this to remove execute the execute bit from your C++ source code:

chmod a-x heap.cpp. 

There's no good reason for the source code to your C++ program to be executable.

James Thompson