Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

71%
+3 −0
Q&A C Language Standard Linking Specifications

The standard does not talk about object files and/or linking, but it does talk about translation units. In typical compilers, a single translation unit translates into a single object file. Obviou...

posted 3y ago by celtschk‭

Answer
#1: Initial revision by user avatar celtschk‭ · 2022-01-13T22:16:22Z (almost 3 years ago)
The standard does not talk about object files and/or linking, but it does talk about translation units. In typical compilers, a single translation unit translates into a single object file.

Obviously the C standard does not talk about translation units that are not themselves C; that's what a platform's ABI is about.

Now what the C standard tells you is this:

  * If you have the declaration

    ```
    extern int foo(void);
    ```

    in some translation unit, then it makes the name available to the current translation unit. The declaration itself does not imply that this function exists anywhere.

  * If you do call that function from anywhere in your translation unit, then *some* translation unit must define it, and it must define it with exactly that signature. From the view of the C standard, that other translation unit is, of course, another C file compiled with the same C implementation.

    Your compiler's ABI (which is *not* covered by the C standard) then tells what this means in terms of symbols and code. For example, it tells you that there has to be a symbol that's named `foo` (in some ABIs, it's `_foo` instead), which must lead to executable code that leaves some integer value in a specific register (and usually has some further requirements, e.g. on what it does with the stack).

    Obviously some object file generated with another compiler or hand-written assembly will work exactly if the correct symbol exists and points to code fulfilling all the conditions required by the ABI.

    These days, the C ABI is usually fixed by the platform. That is, all C compilers on the same platform are interoperable. Note however that this is not mandated by the C standard, which always assumes a single C implementation. Note also that a compiler may support different ABIs (this was especially the case with early compilers on 8086, which supported different so-called memory models).

Note that usually different languages have different calling conventions. But often languages allow to explicitly use the C calling conventions of the platform. Sometimes it is even part of the language specification of that language (such es `extern "C"` in C++). But obviously the C language standard doesn't tell you anything about other languages.