How do I link object files in C? Fails with “Undefined symbols for architecture x86_64”

前端 未结 5 446
误落风尘
误落风尘 2020-12-02 08:53

So I\'m trying trying to use a function defined in another C (file1.c) file in my file (file2.c). I\'m including the header of file1 (file1.h) in order to do this.

H

5条回答
  •  执念已碎
    2020-12-02 09:44

    The existing answers already cover the "how", but I just wanted to elaborate on the "what" and "why" for others who might be wondering.

    What a compiler (gcc) does: The term "compile" is a bit of an overloaded term because it is used at a high-level to mean "convert source code to a program", but more technically means to "convert source code to object code". A compiler like gcc actually performs two related, but arguably distinct functions to turn your source code into a program: compiling (as in the latter definition of turning source to object code) and linking (the process of combining the necessary object code files together into one complete executable).

    The original error that you saw is technically a "linking error", and is thrown by "ld", the linker. Unlike (strict) compile-time errors, there is no reference to source code lines, as the linker is already in object space.

    By default, when gcc is given source code as input, it attempts to compile each and then link them all together. As noted in the other responses, it's possible to use flags to instruct gcc to just compile first, then use the object files later to link in a separate step. This two-step process may seem unnecessary (and probably is for very small programs) but it is very important when managing a very large program, where compiling the entire project each time you make a small change would waste a considerable amount of time.

提交回复
热议问题