1

In my scenario, I have two C files. First file is user and the second file is the provider.

Example:

user.c
const int config_user;
void user(void) {
if (config_user == 1) {
    ... do something ...
    }
}

provider.c
cont int config_provider = 1;

In the above scenario the address of config_provider should be same as that of config_user. I made several attempts in linker script but I was not successful. I can not use extern, I have to do it using memory linking.

  • 4
    Any specific reason you cannot name them the same and use extern on one of them? – Ajay Brahmakshatriya Mar 14 '20 at 18:21
  • @AjayBrahmakshatriya, provider is essentially used for configure the user. The example if pretty simple, configuration is a complex structure, also the configuration has to be a constant. Now, HOOD concept does not allow, extern const in user and const in provider. Please let me know if the last line is not clear, I will write it in detail. – Rachit Kumar Mar 14 '20 at 18:31
  • 1
    Does this answer your question? [Mixing extern and const](https://stackoverflow.com/questions/2190919/mixing-extern-and-const) – Tarek Dakhran Mar 14 '20 at 18:43
  • 1
    Why not using a function like `const int *getConfigProviderPtr(void)` and use it all over your project/files? – Alex Lop. Mar 14 '20 at 19:10

1 Answers1

0

This doesn't directly answers your question but I would suggest defining a geter function in provider.h:

const int *getConfigProvider(void);

and in provider.c

const int *getConfigProvider(void)
{
    static const int config_provider = 1;

    return &config_provider;
}

This somehow encapsulates the implementation to the consumer and achieves what you are looking for eventually.

Note:

In case of int the return type of getConfigProvider may be just int. In case of a more complex type (struct), it is better to use const pointer to it.

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
  • To provide more clarity, the configuration is task configuration, used by the user to configure and create tasks in the RTOS. The entry point of the user is directly called by the RTOS, hence the configuration has to be memory linked. Also it is possible to call the service and get the configuration as you have mentioned, but it is restricted by the HOOD concept. – Rachit Kumar Mar 15 '20 at 03:00