0

I am building static libraries with CMake, but the final linking process happens in a custom make script. In one of the components that build a library, there is a table that's not used in the code, but contains version information and has to end up in the final executable.

The compiler here is tasking, but its linker should behave similar to gcc.

Currently there is a makefile that defines a linker option to achieve that:

LINK_OPT += --extern=MyTable myLibrary.a

which I am reasonably sure is the equivalent of gcc's --undefined=MyTable myLibrary.a.

My goal is to get rid of those makefiles, so the component can be defined solely in CMake. The issue I have is that, due to how the build is set up with the linking not happening in the CMake context, simply attaching linker options to the static library targets in CMake won't help, as they aren't available when I actually link.

I have tried using __attribute__((used)) and __attribute__((export)) in the source code, but they didn't help. I checked the compiler supports them, and suspect they would work if I simply linked object files instead of libraries(?)

Is there any meta information I can attach to the library or the source code that tells the linker to please include MyTable in the executable, without me having to specify a linker option on the linker command line?

kutschkem
  • 7,826
  • 3
  • 21
  • 56
  • Usually there's a command in the linker script where you can flag identifiers that must be linked. Other than that, a dirty trick is to add some nonsense code like `*(volatile uint8_t*)&variable_that_should_be_linked;` – Lundin Mar 11 '22 at 09:40
  • If you're using gcc then this may be duplicate of https://stackoverflow.com/questions/29545191/prevent-gcc-from-removing-an-unused-variable, tl;dr is either `__attribute__((used))` or marking variable itself as volatile. Otherwise, there's not much you can do, why would you want a *static* library to not be optimized out where appropriate? You most likely want to be using shared library for things like that, because otherwise it is pretty obvious to the linker - you either are using the variable, or you aren't, and if you are, relinking the executable after source changes will include it. –  Mar 11 '22 at 10:13
  • @Sahsahae The software in question is automotive embedded software, the variable in question is a table which contains version information. Not needed by the code, very much needed to be included in the executable to be able to tell, from the image, what software version it is. – kutschkem Mar 11 '22 at 10:24
  • What scope and linkage has the table? Did you try to give it external linkage (make it globally visible)? – the busybee Mar 11 '22 at 10:31
  • @kutschkem if it's not needed by the software, maybe you have an option to simply append this information directly to the final executable, bypassing the compiler and linker completely? Since whatever needs to read the information will already be searching for raw bytes in the image, you may aswell put it in an obvious place. After that, it is just a matter of parsing a binary data format backwards to figure everything out. –  Mar 11 '22 at 10:42
  • @thebusybee It's just a const array in a c file. I will try defining as extern. Other modifiers are a `__attribute__((section (xyz))` – kutschkem Mar 11 '22 at 10:55
  • For other people finding this question: I did not find a solution and my current assumption is there is no way to do this. – kutschkem Apr 06 '22 at 13:14

0 Answers0