Skip to content

Detecting macro definitions mismatches at link time#1335

Merged
wolfpld merged 3 commits intowolfpld:masterfrom
siliceum:feature/check-macros-mismatch
Apr 21, 2026
Merged

Detecting macro definitions mismatches at link time#1335
wolfpld merged 3 commits intowolfpld:masterfrom
siliceum:feature/check-macros-mismatch

Conversation

@Lectem
Copy link
Copy Markdown
Collaborator

@Lectem Lectem commented Apr 14, 2026

Note: This is ABI breaking and a potential API breaking change too if users had mismatching defines in the list. This was never supported and code was likely wrong, but users may complain about it. Given the number of users reporting "bugs" due to mismatched defines (especially TRACY_ON_DEMAND) I believe this is still worth implementing anyway.

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 via tracy::ScopedZone, when linking with mismatched defines you'd get an error such as (MSVC):

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 (GCC)

[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. (opposite is detected though, if tracy was built without TRACY_ENABLE and consumer with) 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).

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.

@Lectem Lectem force-pushed the feature/check-macros-mismatch branch from 84ac588 to f0f7d53 Compare April 14, 2026 13:23
Lectem added 2 commits April 21, 2026 13:28
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
@Lectem Lectem force-pushed the feature/check-macros-mismatch branch from 2a61bbb to ca076b4 Compare April 21, 2026 11:29
@Lectem Lectem changed the title Macro incompatibility experiment Detecting macro mistmaches at compile time Apr 21, 2026
@Lectem Lectem changed the title Detecting macro mistmaches at compile time Detecting defines mismatches at compile time Apr 21, 2026
@Lectem Lectem changed the title Detecting defines mismatches at compile time Detecting defines mismatches at link time Apr 21, 2026
@Lectem Lectem changed the title Detecting defines mismatches at link time Detecting macro definitions mismatches at link time Apr 21, 2026
@Lectem Lectem marked this pull request as ready for review April 21, 2026 12:12
@wolfpld wolfpld merged commit 68ee870 into wolfpld:master Apr 21, 2026
7 checks passed
@Lectem Lectem deleted the feature/check-macros-mismatch branch April 21, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants