'Force compiler to use static definition from a specific C file

Two C files have the same variable name declared differently.

Can I force the compiler (Visual Studio or gcc) to prefer one declaration over the other?

The scenario is that I have a C file where I would not like to change anything and my own C file with several overlapping definitions, and that's where I would like to make my changes.

For example, I have the following definition in the original c file :

static unsigned char key[27 + 1] = "<put ur key here>";

And I have the following definition in my own c file :

static unsigned char key[27 + 1] = "abcde...";

When compiling, I would like the compiler to use my declaration instead of the original cpp file.



Solution 1:[1]

It isn't clear from the comments whether you have a C++ file as the original code that you are (not unreasonably) reluctant to change. I'm going to assume it is a C file, not a C++ file. It doesn't make a lot of difference except to the file name you create.

As pointed out in the comments, you cannot achieve that which appears to be your goal by defining a different static variable in a different source file.

The static variable key defined in own.c is wholly unrelated to the static variable key defined in original.c. You cannot make them equivalent. If the original.c code does not provide a mechanism to change the value in its key, you are hosed until you change the code to provide such a mechanism.

One gruesome but effective way is to create a source file wrapper.c along these lines:

#include "wrapper.h"
#include "original.c"
void set_original_key(const char *new_key)
{
    if (strlen(new_key) < sizeof(key))
        strcpy(key, new_key);
}

and a header file wrapper.h which contains a declaration of set_original_key():

#ifndef WRAPPER_H_INCLUDED
#define WRAPPER_H_INCLUDED

extern void set_original_key(const char *key);

#endif /* WRAPPER_H_INCLUDED */

Then compile wrapper.c to wrapper.o and link that into your executable instead of original.o (which you used to use). And include wrapper.h in the code that needs to set the key and call set_original_key() to change the key to the value of your choosing.

This subterfuge effectively adds code to the original.c file without altering it. The extra code has access to the variables in the original file, including the file-scope static variables.

The function shown doesn't report an error when the new key is too long to fit into the space for the key defined in original.c. It could easily be revised to return zero on success and -1 on failure. Note that it is fortuitous but extremely convenient that original.c does not define key as a constant (as it should). If the data to be changed was in fact constant (or if the key was hidden in a function instead of being defined at file scope), you'd be stuck!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonathan Leffler