6

Just reading Effective C++ and he mentions several times "linker error", as opposed to compiler error.

What constitutes a "linker error" and how do they differ from "compiler errors"? Are the rules/explanations based around a set of categories to logically remember this?

user997112
  • 29,025
  • 43
  • 182
  • 361
  • 2
    Information on the difference between a linker (error) and a compiler (error) are widely available I believe. Just google it. – Andy Prowl Feb 18 '13 at 23:16
  • 1
    Linker errors are, broadly speaking, about using things that you have *declared* in your code but not *defined* (or defined too many times). The linking process makes those connections, and that's were things can go wrong. – Kerrek SB Feb 18 '13 at 23:17
  • 2
    You're effectively asking the difference between "compiling" and "linking". Seek to understand those. – Drew Dormann Feb 18 '13 at 23:17
  • Here's a metaphor: Imagine you want to make a line (like the line of real numbers). Now someone can give you pieces of line and you say, "looks alright". If someone gives you something that looks like a cross, you know immediately that that can never be part of a line ("compiler error"). But after you put all the pieces together, you might find that you've made a big circle! You never saw this coming from the small pieces, but you still didn't get what you wanted: "linker error". – Kerrek SB Feb 18 '13 at 23:22

3 Answers3

12

Compiler errors mean the compiler could not translate the source code you provided into object code. It usually means you have a syntactic or semantic error in your own program that you have to resolve before your program exhibits the behavior you're intending it to have.

Linker errors mean the linker could not build an executable program from the object code you provided. It usually means your program does not properly interface with its own dependencies or with the outside world (e.g. external libraries).

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
1

Compiler errors are the class of errors related to the semantics of your code during compilation i.e. the process of conversion of sources to object files. Here, you may have defined certain symbols (for example pthread_create) which are assumed to be available.

Linker errors are errors encountered when these dependencies are verified during the creation of a final object file. Going with the example above, for the creation of the executable, you need the definition of pthread_create which if not found will give a linker error.

Ganesh
  • 5,880
  • 2
  • 36
  • 54
1

gcc -c compiles and does not link:

   -c  Compile or assemble the source files, but do not link.  The linking
       stage simply is not done.

You can compile a file and then link it with -o:

$ gcc -c hello.s
$ gcc -o test hello.o 
$ ./test 
Hi World
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424