Skip to content

Inline functions in several headers to fix ODR violations - #360

Merged
tremblap merged 3 commits into
flucoma:mainfrom
fearn-e:inline-functions-lnk2005
Dec 2, 2026
Merged

tremblap merged 3 commits into
flucoma:mainfrom
fearn-e:inline-functions-lnk2005

Conversation

@fearn-e

@fearn-e fearn-e commented Nov 28, 2026

Copy link
Copy Markdown
Member

As per issue #9 and PR #20 that fixed it, there are more functions that produce LNK2005 errors when including their header files in more than one TU, as FluCoMa is set as an INTERFACE library in the CMakeLists.txt.

ScalerUtils.hpp, DistanceFuncs.hpp, KMeans.hpp -- all have a few instances of this. FluidJSON.hpp is littered with the error on every single check_json, to_json, and from_json.

A build can be forced through with /FORCE:MULTIPLE, and considering the definitions will be identical in this case, shouldn't cause any undefined behaviour at runtime. However, adding inline to all the offending functions is a simple fix and, as far as I can tell, should generally be what is done for an interface library.

On a sidenote; This seemingly doesn't come up that often, I'd guess because the default FluCoMa examples contain only one TU per .exe target, so these functions are never defined more than once there. But I am surprised to not see any more bug reports about this in the 5 years since that last PR #20!

There's likely more of these present in the codebase, but it could be checked by going through and including every header file in two different TUs.

@tremblap

Copy link
Copy Markdown
Member

@weefuzzy will be the person to approve this - I don't understand the assumptions here

@fearn-e, did you do what you suggested, i.e. "could be checked by going through and including every header file in two different TUs", and if not, why?

@weefuzzy weefuzzy changed the title Inline functions in several headers to fix LNK2005 errors Inline functions in several headers to fix ODR violations Nov 30, 2026
@weefuzzy

Copy link
Copy Markdown
Member

Thanks @fearn-e. There are certainly more of these lurking. I think I will make a new test that does as you suggest and just builds two TUs that include all of core. Then we can catch at test time (clang tidy will also pick these up in most cases too)

@weefuzzy

Copy link
Copy Markdown
Member

Note that function templates shouldn't need to be inlined. I'll give this a proper review when I have time.

@fearn-e

fearn-e commented Nov 30, 2026 •

Copy link
Copy Markdown
Member Author

@tremblap I did not yet because I wasn't 100% sure this was the right way to go about things in the first place! It's been a while since I committed something outside of my own acorex bubble 😅

Now that I do, I could add something to the examples folder that has multiple TUs in it. In fact, what I'm making for the OF tutorial could probably go in the examples folder anyway. And I wouldn't mind doing the test case @weefuzzy, that's still a blind spot for me.

And it does indeed look like function templates don't need the inline keyword, as they are already implicitly inline get the same treatment as "inline" functions with regards to ODR. I'll revert those back.

@fearn-e
fearn-e force-pushed the inline-functions-lnk2005 branch from 987c6aa to 2dd8ba4 Compare December 1, 2026 20:21
@fearn-e

fearn-e commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Haven't written a proper test, but did a quick and dirty run through everything in include/flucoma/algorithms and include/flucoma/data, and IncrementalMeanVar was the only remaining linker error after all the others that were already fixed. Now it compiles fine with every single .hpp from algorithms and data.

I'm not sure what the situation is with include/flucoma/clients and if those should also be checked?

@weefuzzy

weefuzzy commented Dec 2, 2026

Copy link
Copy Markdown
Member

Excellent, many thanks for going above and beyond. It's highly unlikely that flucoma/clients would need this treatment: first, there aren't many (any?) free, non-templated functions in those headers; second, they only really get used alongside the CCE specific wrappers where we're generating the TUs at build time anyway.

@tremblap

tremblap commented Dec 2, 2026

Copy link
Copy Markdown
Member

@weefuzzy do we want to 'rebase and merge' here, or 'squash and merge'

@weefuzzy

weefuzzy commented Dec 2, 2026

Copy link
Copy Markdown
Member

@weefuzzy do we want to 'rebase and merge' here, or 'squash and merge'

squash and merge

@tremblap
tremblap merged commit af355be into flucoma:main Dec 2, 2026
3 checks passed
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.

3 participants