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;
}
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:
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:
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).