Multiple definition errors

Technical questions regarding the XTC tools and programming with XMOS.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Multiple definition errors

Post by Folknology »

I have had this sort of error several times:

Code: Select all

/path/file.xc: Error: Multiple definition of 'myvar'
if the var 'myvar' is defined in a header file that is included via a module.

Code: Select all

#ifndef _MYHEADER_H_
#define _MYHEADER_H_
...
int myvar;
...
#endif
If I add the header file directly into my source tree rather than via USED_MODULES in my make file the issues resolves itself.

any ideas?

Code: Select all

$> xcc --version
Community_14.1.2 (build 17961, Dec-04-2015)
Compiler version: 14.1.1
Copyright (C) XMOS Limited 2008-2015. All Rights Reserved.
regards
Al
User avatar
andrewxcav
Verified
Experienced Member
Posts: 76
Joined: Wed Feb 17, 2016 5:10 pm

Post by andrewxcav »

I am guessing that this header is included in multiple source files.

While the include guards prevent multiple, conflicting, declarations of the variable, there is still some ambiguity at link-time as to whether you want myVar to exist in one and only one place, or whether you want a local copy to be instantiated in each implementation that uses it.

I guess it is also possible that you only use it in one place, and putting the file in the MODULES folder is telling the linker something that you didn't intend.

Try putting it back in the MODULES folder and declare it with the static keyword to tell the linker that there is one version of this variable for each source file that includes it.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Thanks Andrew that is interesting.

I made it static and the error disappeared, as it happens I wanted a const struct but simplified the example to and int to make it easier to read for this post. So now I am using a static const structure which all works just fine.

AFAIK I am only including this header once BTW

regards
Al
User avatar
andrewxcav
Verified
Experienced Member
Posts: 76
Joined: Wed Feb 17, 2016 5:10 pm

Post by andrewxcav »

Awesome.

So perhaps there is something going on behind the scenes to everything in USED_MODULES...
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

Yes indeed, I would love to understand what is actually happening behind the scenes, anyone at Xmos care to enlighten us?

regards
Al