Detecting macro definitions mismatches at link time#1335
Merged
wolfpld merged 3 commits intowolfpld:masterfrom Apr 21, 2026
Merged
Detecting macro definitions mismatches at link time#1335wolfpld merged 3 commits intowolfpld:masterfrom
wolfpld merged 3 commits intowolfpld:masterfrom
Conversation
84ac588 to
f0f7d53
Compare
We redirect GetProfiler() (most likely used by any project consuming tracy, since it's used by `tracy::ScopedZone`) to its implementation which now has a different function name based on the macros that can impact ABI (and enabled/disabled). That way, when linking with mismatched defines you'd get an error such as > main.obj : error LNK2019: unresolved external symbol "int __cdecl GetProfiler_CFG_E0_OD0_DI0_ML0_F0_DHT0_TF0(void)" (?GetProfiler_CFG_E0_OD0_DI0_ML0_F0_DHT0_TF0@@yahxz) referenced in function "int __cdecl GetProfiler(void)" (?GetProfiler@@yahxz) Or >[build] /usr/bin/ld: CMakeFiles/app.dir/main.cpp.o: in function `GetProfiler()': [build] /..../TracyProfiler.hpp:143: undefined reference to `GetProfiler_CFG_E1_OD0_DI0_ML0_F0_DHT0_TF0()' Reason for going with acronym+0/1 instead of just acronym when enabled is for us to be able to tell users easily which define is wrong by just looking at the error if needed. The only thing we don't really detect is user not having TRACY_ENABLE but tracy having been built with it. This is because macros become noops in that case, with no reference to `GetProfiler`. There may be a way to do it by introducing a local variable into each TU, but I don't really like that idea. We could also add pragma detect mismatch for a more user-friendly error on windows (https://learn.microsoft.com/en-us/cpp/preprocessor/detect-mismatch?view=msvc-170).
Also rename MANGLED_NAME_BASED_ON_DEFINES => MANGLED_NAME_BASED_ON_CONFIG
2a61bbb to
ca076b4
Compare
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.
The issue
People keep having mismatches between macro definitions how tracy is built and where it's used
The solution presented here
The changes in this PR aim at detecting mismatches at link time by encoding important defines into the name of the implementation of
GetProfiler(). We only encode the ones that can impact ABI or would create incompatible behaviour (such as mixing time sources). It is now redirected to the implementation which is mangled by appending each macro's abbreviation and 0/1 depending on whether it's defined or not.How it works
Since
GetProfiler()is most likely used by any project consuming Tracy viatracy::ScopedZone, when linking with mismatched defines you'd get an error such as (MSVC):Or (GCC)
Reason for going with acronym+0/1 instead of just acronym when enabled is for us to be able to tell users easily which define is wrong by just looking at the error if needed.
The only thing we don't really detect is user not having
TRACY_ENABLEbut tracy having been built with it. (opposite is detected though, if tracy was built withoutTRACY_ENABLEand consumer with) This is because macros become noops in that case, with no reference toGetProfiler. There may be a way to do it by introducing a local variable into each TU, but I don't really like that idea. We could also add pragma detect mismatch for a more user-friendly error on windows (https://learn.microsoft.com/en-us/cpp/preprocessor/detect-mismatch?view=msvc-170).There may be better ways to do that I'm unaware of, but most I've seen rely on introducing new globals or would discard the diagnostic under optimizations, which I didn't really like.