<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>getImage multiply defined (by retarget.o and init.o)</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/37532/getimage-multiply-defined-by-retarget-o-and-init-o</link><description> 
Hello, 

 
I get this error message: 

 
Symbol getImage multiply defined (by retarget.o and init.o) 

 
In the headerfile init.h I have added all include data (like
stdio.h and my own header-files) In one of these headerfiles I
defined the struct variable</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: getImage multiply defined (by retarget.o and init.o)</title><link>https://community.arm.com/thread/48946?ContentTypeID=1</link><pubDate>Sun, 11 Nov 2007 12:41:43 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ca21f460-c9ca-4484-af0e-0ab083a69c5a</guid><dc:creator>Georget Stephane</dc:creator><description>&lt;p&gt;&lt;p&gt;
Both retarget.c and init.c (or .cpp) include somehow one of your
header files, in which you have the code:&lt;/p&gt;

&lt;pre&gt;
struct Image getImage;
&lt;/pre&gt;

&lt;p&gt;
Keep in mind that because a line of code is in a header, doesn&amp;#39;t
change the fact that it will be compiled, as the rest of the code
that follows in the c or cpp file.&lt;/p&gt;

&lt;p&gt;
So when the compiler translates retarget.c, it will create
retarget.o, in which a symbol called getImage (that has type struct
Image) was created.&lt;/p&gt;

&lt;p&gt;
When the compiler translates init.c, it will create init.o, but
since it is going to hit the exact same line of code again (struct
Image getImage), it will create another symbol, getImage, of type
struct Image, in init.o.&lt;/p&gt;

&lt;p&gt;
Then the linker complains that you have the exact same symbol,
both publics, defined in two different object files (or more?).&lt;/p&gt;

&lt;p&gt;
What you need is to have getImage created only once in your
project. So you need to ensure that the line of code:&lt;/p&gt;

&lt;pre&gt;
struct Image getImage;
&lt;/pre&gt;

&lt;p&gt;
appears in one, and only one c file (or cpp).&lt;/p&gt;

&lt;p&gt;
Of course, if you have this line of code in a header, chances are
that many c files will include it, and all those files will tell the
compiler to define the exact same symbol.&lt;/p&gt;

&lt;p&gt;
You need to have, in your header:&lt;/p&gt;

&lt;pre&gt;
extern struct Image getImage;
&lt;/pre&gt;

&lt;p&gt;
That way, when you compile a file that includes this line, you
just make a promise to the compiler that the object getImage is
created somewhere else in your project, and the c code can use
getImage, the compiler won&amp;#39;t complain. getImage is not created, only
the link to a symbol, that is suspected to exist in another .o
file.&lt;/p&gt;

&lt;p&gt;
AND, somewhere in a c file, it could be ANY file of the project
(as long as it is linked at the end, so it could even be in a
library):&lt;/p&gt;

&lt;pre&gt;
struct Image getImage;
&lt;/pre&gt;

&lt;p&gt;
This time, getImage is created, but only once.&lt;/p&gt;

&lt;p&gt;
Usually, headers are used to declare variables and functions, not
to define them. Therefore, you need to use the extern keyword in
headers.&lt;/p&gt;

&lt;p&gt;
If getImage is used in only one c file, then there is no need to
declare it anywhere else, just define it and use it in the same file.
If getImage is used by several c files, then you need to declare
geImage in each file that uses it, and to achieve that, putting the
declaration (with the extern keyword) in a &amp;quot;popular&amp;quot; header (used by
most files), is a common solution.&lt;/p&gt;

&lt;p&gt;
If you want getImage to be accessible from only one translation
unit (c file with headers), then you need to define it with the
static keyword. That way, the symbol won&amp;#39;t be exported to the linker
and other c files won&amp;#39;t be able to use the getImage variable that you
defined in that very translation unit.&lt;/p&gt;

&lt;p&gt;
&amp;#39;hope this helps...&lt;/p&gt;

&lt;p&gt;
Steph-&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: getImage multiply defined (by retarget.o and init.o)</title><link>https://community.arm.com/thread/48947?ContentTypeID=1</link><pubDate>Sun, 11 Nov 2007 11:57:31 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f7704014-25ba-4c5c-b76d-589f0cee8a6c</guid><dc:creator>Dan Henry</dc:creator><description>&lt;p&gt;&lt;p&gt;
Object definitions do not belong in header files. Put definitions
in one C source file and declare the object in a header file.&lt;/p&gt;

&lt;p&gt;
See &lt;a href="http://c-faq.com/decl/decldef.html"&gt;c-faq.com/.../decldef.html&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>