News for hackers. Stuff that matters.
Re:When static makes your C code 10 times faster by SavantIdiot on Sunday July 04, 2021 @07:01PM (#27733306)
You'd think the compiler would let you know you have a constant that is not labelled as such, like the way `tslint` complains about this incessantly. (I think `splint` for c/c++ may also do this, but I've only briefly used it.)
Re:When static makes your C code 10 times faster by ericbarrett on Sunday July 04, 2021 @07:21PM (#27733417)
The compiler only operates on one unit (file) at a time so it has literally no way of telling this in C. There are legitimate uses for having a non-const global which is never modified by local source: library config options, hooks for external programs, and what not. As you say, this would be a job for the linter.
Re:When static makes your C code 10 times faster by astrange on Sunday July 04, 2021 @11:05PM (#27734310)
You can get pretty far with a compiler warning like "warn if a global isn't preceded by an 'extern' declaration". Also, LTO does have enough information to warn about these things, especially with default hidden visibility.
Re:When static makes your C code 10 times faster by ericbarrett on Monday July 05, 2021 @12:59AM (#27734755)
The global must be declared without "extern" somewhere or else no memory is allocated for it.

LTO could handle this if you're compiling an executable, but not a library.

Re:When static makes your C code 10 times faster by astrange on Monday July 05, 2021 @04:29AM (#27735730)
> LTO could handle this if you're compiling an executable, but not a library.

That's not the correct distinction, that's why I said default vs hidden visibility.

Libraries typically export more symbols but executables can also export them eg for plugins.