1

I have a program consisting of 3 files:

main.c; other.c and event.h

event.h has a function called 'event()'.

I'd like to call the event() function from both main.c and other.c. At the top of both main.c and other.c I include the line

#include "event.h"

...and within each of the .c files I have the lines

event();

However I receive the following compile errors (Visual C++)

"fatal error LNK1169: one or more multiply defined symbols found"

"error LNK2005: _event@16 already defined in main.obj"

...What am I doing wrong?

4 Answers 4

2

You have two choices: either don't define the function in the header, or define it as inline. The first is covered in other answers, and the latter is as easy as prepending "inline" to the function definition:

inline
void example() {
  do_stuff();
}

Whether you want to define the function inline or not is, today, all about your convenience and not about optimization. Which is easier for you? Inline functions are almost always easier to begin with (i.e. prototyping and first versions); you can always reverse that decision, if needed, later.

1

The definition of the function ie its code can only be given in one place.

The header should include just the declaration of the event function and the code implementing the function needs to be in a .c file.

e.g.

in event.h

void event();

and in any ONE .c file - could be either of main.c or other.c or probably better a separate file event.c

void event()
{
    printf( "Hello\n" );
}
4
  • Thank you - this is the solution I required May 14, 2011 at 11:31
  • 3
    "The header should include just the [ declaration ] of the event function...". Declaration and definition are not interchangeable terms in C++. You could also have used the term prototype. May 14, 2011 at 13:10
  • @Emile - yes I know - a typo - I got the correct one in the first line and spent time double checking but missed:)
    – mmmmmm
    May 14, 2011 at 19:15
  • Yeah, you gotta type quickly here if you want to get an answer in before the others. :-) May 14, 2011 at 21:12
1

You should not define event() in event.h. Because event.h is included both in main.c and other.c, it ends up being defined twice.

You need to declare it in event.h, with the function signature, such as:

void event();

And put the definition in an event.c file where you will put the function body. The function will be defined only in this event.c file, and the two other .c files will only include the declaration.

1

I suppose you have defined event() in event.h, like this:

void event()
{
 ....
}

But in header you have to declare function and then define it in .cpp So you have to do the following: event.h

// declaration
void event();

event.cpp

#include "event.h"
// implementation
void event()
{
 ....
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.