two slightly different header files in c++ used (one .lib file though) causing lots of errors but no warning generated. how to prevent it?

StackOverflow https://stackoverflow.com/questions/22445845

Question

I had a library that was compiled with one set of header files and I copied the header files to a folder where I put all my header files as it is in MSVC include search path.

But the compiled .lib was with different set of headers and my program using that header file was using original (unchanged) set of header files. I noticed problem as debugger was showing wrong values. For example I was setting

mystruct->item1 = 1

but after this line debugger was showing mystruct->item1 = 0. Although printing it was fine. But I then noticed passing this structure to library functions have different values. Which made me realize there were different structures.

As size of structures are different, I think compilers should understand this and generate warnings. (which I didnt get any). I spent several days to debug this and I was wondering for similar cases how can I prevent it? For example if there is a compiler switch that helps generating warnings for cases like this. Or how can I prevent nasty errors like this?

Was it helpful?

Solution

As size of structures are different, I think compilers should understand this and generate warnings.

The problem is that by the time you are connecting your code with the code of the library the compiler is out of the picture. The program that makes the connection (the linker) has no idea that the two binary codes that it tries to connect use structures of different size - in fact, linker has no idea that the programs used any data structures at all: it operates on a different level.

That is why it is not possible for the tools in the C++ toolchain to produce a reliable warning for you. This does not mean, however, that you cannot prevent it yourself: all it takes is embedding some sort of a build number into the library and its header, and comparing the two at the time when you initialize the library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top