Repository navigation
Write a header's definitions so it can be included from more than one translation unit [patch] - #457
Open
matt-edmondson wants to merge 1 commit into
Open
matt-edmondson wants to merge 1 commit into
matt-edmondson wants to merge 1 commit into
Conversation
… translation unit [patch] A SourceFile marked IsHeader only gained #pragma once, so a free function, a function a method is lowered to, or a mutable file-scope variable in it was defined with external linkage in every translation unit that included it, and the program failed to link with "multiple definition". C++ now writes free functions and namespace-scope variables inline. C writes functions static inline and mutable variables static, one copy per translation unit, since C has no inline variable. Output that is not a header is unchanged. Fixes #423 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW4Rc5sqSEVHr6NgrkT
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #423
What was wrong
A
SourceFilewithIsHeader = trueonly gained#pragma once. That guard works within one translation unit but not across units. A header holding a free function, a function lowered from a method (C), or a mutable file-scope variable defined each one with external linkage in every unit that included it. Linking then failed withmultiple definition of 'twice','limit','P_get','geo::half(int)'and so on.Change
CFamilyGenerator.WritingHeaderreports whether the file being generated is a header. The C and C++ generators check it:inlinestatic inlinestatic inlineinline(C++17 inline variable)static: one copy per translation unitFor the C mutable global the issue offered two options. I chose
static, which is what the C generator already does for a type's static fields (static int P_count). The alternative,externplus a definition somewhere, would need a translation unit that a header-only output doesn't have. The choice is documented in the code and in CLAUDE.md.These stay as they were:
constexprand abstract functions in C++. These are already inline or have no definition.Tests
HeaderLinkageTestsbuilds a header with a mutable variable and a function at file scope, a struct with an instance method, a static method and a static field, and a namespace holding a variable and a function.-std=c11 -Wall -Wextra -pedantic-errors, C++ with-std=c++17 -Wall -Wextra -pedantic-errors.inline/static inline/static, and that output for a file that isn't a header is unchanged.I reverted the generator change and four of the five tests failed with the linker errors from the issue. With the change back in place the full suite passes: 1669 tests.
Generated by Claude Code