4
gcc (GCC) 4.7.2
c89

Hello,

I have the following in my service.h file

enum service_state_code {
    NO_ERROR_OK,
    ERROR_INCORRECT_STATE,
    ERROR_EMPTY_STRING,
    ERROR_NO_COMMAND_FOUND
};

const char *service_state_msg[] = {
    "OK:",
    "ERROR: Incorrect state for modifying service channel state",
    "ERROR: No command found",
    "ERROR: No command parameters",
    NULL
};

get_channel_service_state(channel_t *channel, const char *msg);

And I have 2 other *.c files that will include the service.h file.

network.c and socket.c

And I use it something like this:

get_channel_service_state(channel, ss7_service_state_msg[ERROR_INCORRECT_STATE]);

However, I get a linker error complaining about the:

multiple definition of service_state_msg first defined here

I know the reason why I am getting this error. As the service_state_msg is being defined twice as a global in service.h for each time it is included in a *.c file.

I am just asking what is the best way to being about to use service_state_msg across multiple *.c source files?

Many thanks for any suggestions,

2 Answers 2

7

You could make service_state_msg extern in the header file:

extern const char *service_state_msg[];

And then move this:

const char *service_state_msg[] = {
    "OK:",
    "ERROR: Incorrect state for modifying service channel state",
    "ERROR: No command found",
    "ERROR: No command parameters",
    NULL
};

to any one of your C files. Alternatively, you could leave the initialization in the header file but make service_state_msg static:

static const char *service_state_msg[] = {
    "OK:",
    "ERROR: Incorrect state for modifying service channel state",
    "ERROR: No command found",
    "ERROR: No command parameters",
    NULL
};

But be aware that this means every object file will have a copy of the service_state_msg array, and will all need to be recompiled if it changes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I used the first example with extern. However, I don't really like to duplicate code as you will have to change all the source files. Using the static method is just another global.
1

Define and initialize in a .C file. Use following in the header file.

extern char ** service_state_msg; // in the header file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.