0

Basically my prob is this :

I have a header file - foo.h with a structure pointer human *person = NULL in it. The definition for structure human is in another header included within foo.h . I am creating a shared object file game.so using foo.h and few other headers and cpps.

Now , I have two different cpp files - a.cpp and b.cpp which include the header foo.h. I am creating an object file a.o separately and b.o separately. I am linking both the object file and game.so for creating another shared object file tennis.soand while doing so , am getting multiple definition for the "person". I am aware that the multiple definition error is because a.o contains the structure definition for person , so does b.o.

I have used #pragma once in foo.h already. a.o is getting compiled separately and b.o is getting compiled separately. so i dont think #pragma once or ifdef will be useful here because both a.cpp and b.cpp includes foo.h

I cant change the structure defintion in foo.h to any other cpp file due to some dependencies while creating game.so

Is there any other way to resolve the multiple definition error I get while creating tennis.so ?

7
  • There is no such thing as multiple definition link error for a struct. Please post exact code and build commands to reproduce the situation, plus the exact compiler output. Commented Jul 27, 2017 at 4:17
  • I couldn't post the exact code as it is little confidential. Is there anything else I can do ? Commented Jul 27, 2017 at 4:34
  • You can create an MCVE (minimal reproducible example). That will reproduce the problem but not contain anything confidential. It sounds like your header doesn't declare the variable; it defines the variable. Therefore, each file that includes the header contains a definition, preventing them being linked together. Simple rule of thumb: variables declared in headers should be declared with keyword extern. Commented Jul 27, 2017 at 4:38
  • @M.M I have modified the question a bit. Please look at it now and see if u can help me. Commented Jul 27, 2017 at 4:38
  • @JonathanLeffler Its a structure pointer Joanathan . Commented Jul 27, 2017 at 4:40

2 Answers 2

6

You need to make sure the person variable is only defined once. To do that, you need to only declare that variable in foo.h:

extern human *person;

Then, in foo.cpp, you define it:

human *person = NULL;

You then include foo.o in the object files that make up game.so.

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

2 Comments

Let me try this out Nikos.
This has worked out Nikos. Thanks a ton for the help :)
1

You appear to be confusing DECLARATION with DEFINITION. Your header should DECLARE the structure, your C/C++ will then use such declaration to define variables or refer to instances of such definition[s].

4 Comments

No Ron . Am clear with DEFINITION and DECLARATION. Structure definitions can be within header and it shouldn't create multiple definition error as pointed out by M.M. I very well go with your point that definitions shouldnt be in headers and thats why I have mentioned I am unable to remove the definitions from header due to some dependencies.
Your revised question shows that you actually duplicate VARIABLE definition (human). The human variable should be defined just once. References to it should use extern to avoid the redundancy. Why is the variable definition (i.e. assignment) takes place in a header and not in the module which should "own" the variable? If/when a variable is defined in a header, it should normally be protected with an #ifdef _FROM_DEFINE_MODULE (definition) #else (extern declaration) #endif
Yeah ok Ron. Thanks for the help :)
Yeah the extern usage has helped Ron. Thank you :)

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.