Shared structs and different compiler options
Hello. I am dealing with a largish, legacy code base and I am trying to make some common vocabulary structs that contain data passed between major components. These are the system architecture types for communication, not anything used in intensive computation.
We had a new multi-address space RTOS, so we were starting to use separate address spaces to ensure that our code communicates only using known interfaces (no globals, hidden in singletons or whatever). As it so happens, we are moving back to a single address space RTOS, but I am still making these structs as if they are going to be used by separately compiled components. I plan to make each major component compile into a library that is used by the application.
This is all to setup my question! I have recently learned that I may need to protect against different compiler options being used in different components. We might want to change optimization for a particular component, for example, after we get rid of any old UB in it. I have heard that aggressive optimization might do all kinds of interesting stuff with a struct underlying layout. This might not match what another component's compiler options generate.
It seems like I am fighting the compiler a little bit by adding alignas() in some places and having static_assert() to make sure the struct is how we want, i.e. to undo any optimization for that type. It can also be stated that we are using the binary format as a cheap serialization between components, and so that is why I might be fighting the compiler this way. Compiling things independently and allowing different compiler options is like making different programs and then expecting the raw binary to be the serialization still (like you can inside a program compiled all together).
It feels dumb! What are you people doing in these cases?
Thanks!