Pat Ko
Pat Ko

Reputation: 159

C++ compiling and linking

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.

Upvotes: 0

Views: 382

Answers (5)

blackmath
blackmath

Reputation: 252

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.

Upvotes: 0

Maroun
Maroun

Reputation: 96016

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:

enter image description here

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

Upvotes: 1

Vahid Farahmand
Vahid Farahmand

Reputation: 2558

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

Upvotes: 1

Mats Petersson
Mats Petersson

Reputation: 129524

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].

Upvotes: 1

user529758
user529758

Reputation:

Google, first hit.

Linkage is needed as well to create a standalone executable.

Upvotes: 4

Related Questions