What is an an object file? C Programming.

 

The goal of this particular lab exercise is to understand the meaning of an object file. Okay. So when you look into this code, we are writing a simple Hello, Sanfoundry. A simple program. So the objective is different. The objective is not to understand what the program is. We all know what the program is and what will be the output. Okay. So we are going to do a compilation of this code. So watch out.

When you do a compilation of this code, you are going to get a lot of data. So a.out file always gets created when you compile this code. Okay, let me remove a.out once again and then say ls -l, you can see that a.out is no longer there. And I’m going to do the compilation. So Let’s do the compilation, and you get the a.out. What is the size of this a.out? It is 16 kilobytes, right? 16,704. So it is 16 kilobytes. And what was the size of our original program? What was our program? Our program was this. It’s a very small program. Right. It’s a pretty small program.

Do we think that the program is going to generate such a large object for this main function? The answer is really no. See, this is binary, this is the final output of our binary, the last stage of compilation. So this is the final output is the binary, which is the final output of the last stage of compilation. So how do we go about creating only the object code? So we can always do a cc -c Remember, when you do a cc -c, we are really looking into only our program, our .c object code.

Let me do a cc -c. And then I’m going to do ls -l. When you look at it, whenever you do a creation of an object code, this is the object code that we have created. And then you can see the size. It is hardly 10% of this size. Okay. So what you see now is some sort of dichotomy, where you see that. Yes, I have this. And then this. So what are the other data which is going into this particular output? And what is this? Right. So this is really the machine-level implementation or the compiler implementation or the object code associated with this particular function, that’s pretty much it. And what is this? This contains your runtime environment-related machine instructions.

A lot of functions are getting added to this to make sure that it runs into your runtime environment. Not only the runtime environment, but we also have references for print(). Right. So print() is part of the C library. It’s a lib library. Okay, so your C library code is also getting added here. Now, this kind of linking is definitely dynamic linking. Okay, basically. So when you look into this file, sf0006-obj.o, it just says that it is basically an Elf 64 bit relocatable file. Remember, it’s a relocatable file. It is not an executable file. No, it’s not.

Similarly, when you do a file on your a.out, what you see is there’s a lot of data has been added here. Okay. So a.out. Basically, it’s an Elf 64 bit LSB shared object, dynamically linked. Okay. So when you run this code, remember, is dynamically linked. Okay. So when you run this code a.out. Yes, it just works. Hello, Sanfoundry. Can I run the object code? No, we just can’t run it. As I told you, object code cannot be executed. Right? Permission denied.

Basically, it does not allow you to run. There is no. Permission denied is something different? Some of you are from Linux or Unix background who can understand there’s permission and execute because a.out is executable. This is just an object. There is no permission to execute it at all. Okay, even if I change the permission, for example, chmod +x again, I’m using Linux. Come on. And that’s what I said. Never use an online compiler for serious learning. Always have your own compiler, your own Linux environment. It will help you to learn many things about what I’m doing here. Right on the screen. So let me run this.

I cannot execute. There’s a format error. Right? So remember, you can’t execute your .os your object code. You require the support of run time to generate your a.out. I hope you understood the gist of what I’m trying to convey here. The objective here is to understand what is a.out and what is your dot O Right. So some of these commands are extensively used in your troubleshooting exercises. Okay, remember, that file is a very important command. Okay, so this was in terms of object code. Let’s look into some tidbits, actually. Okay, so this is a tidbit. I repeat it’s not a great code. We just have to understand. So can you look into the code? Let’s pause for just maybe five seconds. So this is a code.

When we try to run this code, we know that it’s a for a loop. So it’s supposed to print Hello Sanfoundry in a for loop up to five times. Right. Okay, so Let’s compile this now. You get your a.out. Absolutely no need to worry, a.out. You compile it like this. You got 0-1-2-3-4-5 times. So it’s a simple code, as I told you, Let’s understand the significance of semicolons. Right? Okay. So some of you who have been coding C pretty much understand what it is. This exercise is just for the benefit of some serious coding, right? Sometimes. See, this is the for a loop. And in this for loop, we are trying to do printf. So this works as expected.

I’ve seen a programmer puts a semicolon here. Okay. If you put the same column here without any change, any idea what would with the output? So if you compile this code and run it. So this is the code, what is the output? The output will be … will be only a single printf. So Hello world. Fine. I think there’s a stark difference here. Okay, so earlier we had, remember, we had earlier. Hello World, 0-1-2-3-4. And this time when I’m running it is five. Any idea why it is? I’m sure. Yes, we are all understanding it basically.

What has happened here is we started a for loop and the for loop. I started with zero up to a count of less than five. I will try to do a plus Y because we have a semicolon. And then once it increments it from zero to 1, then two, then 3, and then four, and then five. So the value of I becomes five. Is 5 less than five? Absolutely not. So the condition fails, and then you are out of this for a loop. When you are out of this for loop, the value of i is five, and then you display five here. Right? Okay. Yeah. Here. The objective is, as I told you, just to explain to you the importance and significance of semicolons, right.

When you look into this, there are two statements here. One is this for statement. It’s a loop statement, and there’s another statement. Right now, when you remove the semicolon like this, then we are really looking into a for loop, in terms of for has to go into this print every time on the iteration. So it’s a continuation of your condition.

If you put the semicolon back, this is not the continuation for your, for the condition because the for a statement or the for loop statement is simply terminated with the semicolon here. Right. So be aware of this fact and just make a note of this. You have to be careful with your semicolons. I’ve seen a lot of programming time are wasted in debugging, debugging a code. Now, this code is hardly 4 or 5 lines, but in an industry, you will have hundreds and thousands of lines of code.

Leave a Comment