1 – Introduction To Compilation

So far, you’ve been writing code within the Udacity classroom, in an environment that looks like this, but you’re also going to want to be able to run your programs locally on your own computer. And this brings up another important difference between Python and C++. You learned about the first major difference at the beginning of the last lesson. And that’s that Python is dynamically typed while C++ is statically typed, which is why I have to write words like int. Another major difference is that Python is what’s called an interpreted language, whereas C++ is a compiled language. The thing is when you write code in Python or C++, the computer can’t understand directly what you’ve written. The code first needs to be translated into a language the computer understands in Python code, there’s a translator, also known as an interpreter, that goes line by line, first, translating and then executing each line of code. Translate this line, execute it. Translate this line, execute it. This process of translate execute happens until you’ve reached the end of the file, or in this case, the end of the code cell. And running the cell, behaves as expected. The reality is a bit more complex than what I’ve described, but this is the general gist of an interpreted language like Python. With a compiled language like C++, all of the code is translated into a language the compiler can understand before any of it is executed. And this step is called compilation. Only after the program has been compiled, can it later be executed or run. And up until now, we’ve been hiding this compilation step from you. When you’ve written code in the browser, and then scroll down, and pressed test run, we’ve been secretly compiling and then executing the code in the background. So the language has felt interpreted to you up until now. But, when you write C++ code on your own computer, you’re going to have to do the compilation step yourself. And let me show you what that looks like on my computer. Here, I’ve got that same file, I call it demo.cpp. And if I actually want to run this code on my computer, I have to first pull-up something called a terminal. And don’t worry too much about what this terminal is for now. The only thing I want to show you is that when I type LS, which is a command to list all the files in my current directory, the only thing I see is this dem.cpp for now. So, now before I can run this code, I need to first compile it. And for my computer, I do that by typing g++ followed by the file name. In this case, demo.ccp. I hit enter, and nothing happens. But, if I do another LS, I now see this second file called a.out. And this is a translated or compiled version of the code in demo.ccp. If I type./a.out, and hit enter, I get exactly the output I was hoping for. In the rest of this lesson, you’re going to learn more about compilation, and we’ll start working with C++ code on your own computer.

%d 블로거가 이것을 좋아합니다: