C++ compiling and linking

谁都会走 提交于 2019-12-12 03:35:30

问题


I found one question about compiling and linking in C++ and I don't know which answer is correct. It was discussed with my friends and opinions are divided. Here is a question:

In order to run program written in C++ language its source code is:

(A) compiled to machine code,
(B) compiled and linked to machine code

In my opinion the correct answer is A but I don't have any source to prove it.


回答1:


Google, first hit.

Linkage is needed as well to create a standalone executable.




回答2:


You need to link the code you have produced to make it into an executable file. For simple programs, the compiler does this for you, by calling the linker at the end of the compilation process.

The compiler proper simply translates C code to either assembler (classic C compiler) which is then assembled with an assembler or directly to machine code (many modern compilers). The machine code is usually produced as "object files", which are not "executable", because they refer to external units - such as when you call printf(). It is possible to write C code that is completely standalone, but you still typically need to combine more than one object file, and it certainly needs to be "formatted" to the right way to make an executable file - which is a different file-format than an object file [although typically fairly SIMILAR].




回答3:


Compilation does nothing except creation of object files which means converting C/C++ source code to machine codes.

Linking process is the creation of executable file from multiple obj files. So for running an application/executable you have to also link it.

During compilation, compiler doesn't complain about non existing functions or broken functions, because it will assume it might be defined in another object (source code file). Linker verifies all functions and their existance, so if you have a broken function, you'll get error in linking process




回答4:


Compiling: Takes input C/C++-code and produces machinecode (object file)

  • gcc –c MyProgram.c

Note that the object file does not contain all external references!

Linking: Combines object file with external references into an executable file

  • gcc MyProgram.o –o MyProgram

Note that no unresolved references!

Illustration:

Where libc.a is the standard C library and it's automatically linked into your programs by the gcc.

I've just noticed that your question was about c++, the same concept is in c++ too, if you understand this, you'll understand how it works in c++ too




回答5:


strictly speaking. Answer A.

But for you to see the whole picture, lets say you have defined some function. Then the compiler writes the machine code code of that function at some address, and puts that address and the name of the function in the object ".o" file where the linker can find it. The linker then take this "machine code" and resolve the symbols as you might heard in some previous error.



来源:https://stackoverflow.com/questions/14536491/c-compiling-and-linking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!