Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22035

Is there a method for versioning header and library dependencies in C?

$
0
0

We have a repo that contains library functions for example (gcc is used to compile and link).

//print.h
#ifndef __PRINT_H_
#define __PRINT_H_

#define MAX_ARRAY 10

void print_hex(int cal[]);

#endif

// print.c
#include <stdio.h>

void print_hex(int val[]) {

    for (int I=0;I<MAX_ARRAY;I++)   {
        printf("%i\n",val[I]);
    }

}

The above is compiled into a libprint.a.1.0.0. My app is then compiled using this library

//main.c
#include "print.h"

int main(int argc, int arg[]) {
    int vals[MAX_ARRAY];

    memset(vals,8,MAX_ARRAY*sizeof(int));
    print_hex(vals);
    return 0;
}

And everything is fine and works (assuming have typed the above out correctly). But then someone decides to make a change in the library where someone Makes the following change.

//print.h
...

#define MAX_ARRAY 50
...

The library is recompiled to libprint.a.1.1.0

This new static library is now used when compiling and linking our main.c. However, the new print.h was not copied to the include directory so the main.c is using an old print.h where MAX_ARRAY is smaller.

Now in this case we might get any behaviour as the print function runs off the end of the passed in array. As a user of the library there is no way to necessarily know that the header file is wrong until I compile and run the application, or perhaps even hours of running when the program starts to go crazy.

What is the standard way to avoid this issue?

Is there a way to add a version to a header file to ensure the correct library is linked?

I realise I could do this by creating a new function in print.c called get_version(), and use that in main.c to check against some defines in print.h to ensure veracity but I was wondering if there was a way without the user application having to specifically check library and header versions at run time.


Viewing all articles
Browse latest Browse all 22035

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>