<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>allegro.h desperation: multiple definitions</title>
		<link>http://www.allegro.cc/forums/view/599393</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 01 Mar 2009 02:43:39 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Good evening Allegro community!</p><p>To introduce myself first: my Name is Paul (19 at this time) and I&#39;m from Germany.<br />I&#39;ve been programming in QB and Assembler since 2001, learned some PHP later (see: <a href="http://mosfetkiller.de/">http://mosfetkiller.de/</a>) and finally learned C like 2 years ago.<br />Thanks to Allegro, I rediscovered the joy of game programming again. :-)</p><p>Nevertheless, I am definately not a C expert yet.<br />I wrote a basic RPG engine several days ago, which worked so far.<br />But it was just one huge &quot;main.c&quot; file, so I decided to split it in parts like &quot;video.c&quot;, &quot;logic.c&quot; and so on.</p><p>Unfortunately I have some serious problems with my header files.</p><p>Since every module (&quot;main.c&quot;, &quot;video.c&quot;, &quot;logic.c&quot;, ...) accesses functions of the Allegro library, I have to include &quot;allegro.h&quot; in every single header file of all the modules.</p><p>But when I compile, I get a huge amount of &quot;multiple definition&quot; errors.<br />What am I doing wrong?</p><p>What is the right way of using header files?</p><p>Believe me, for the last two days I tried everything possible to get the whole thing to work, but I just couldn&#39;t make it. <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>I really hope, someone here can help me. Thanks!</p><p>Greetings from Germany,<br />Paul
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul Wilhelm)</author>
		<pubDate>Sat, 28 Feb 2009 06:21:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you&#39;re reusing global variables in more than a single .C file, your compiler will complain.</p><p>If you have a global that you will use in multiple places, pick one of the files to be its &quot;official declaration&quot; file.  Everywhere else, define those variables as &quot;extern&quot;.</p><p>main.c:
</p><div class="source-code snippet"><div class="inner"><pre>  <span class="k1">int</span> MAP_WIDTH<span class="k2">;</span>
  <span class="k1">int</span> MAP_HEIGHT<span class="k2">;</span>
</pre></div></div><p>

video.c:
</p><div class="source-code snippet"><div class="inner"><pre>  <span class="k1">extern</span> <span class="k1">int</span> MAP_WIDTH<span class="k2">;</span>
  <span class="k1">extern</span> <span class="k1">int</span> MAP_HEIGHT<span class="k2">;</span>
</pre></div></div><p>

logic.c:
</p><div class="source-code snippet"><div class="inner"><pre>  <span class="c">// same as in video.c:</span>
  <span class="k1">extern</span> <span class="k1">int</span> MAP_WIDTH<span class="k2">;</span>
  <span class="k1">extern</span> <span class="k1">int</span> MAP_HEIGHT<span class="k2">;</span>
</pre></div></div><p>

That sounds like one of your bigger problems.</p><p>If you use header files, MAKE SURE that you put in header guards.  At the top of those header files, you want to use the preprocessor &quot;#ifndef&quot; checks:</p><p>main.h:
</p><div class="source-code snippet"><div class="inner"><pre>  <span class="p">#ifndef MAIN_H</span>
  <span class="p">#define MAIN_H</span>

  <span class="c">// Everything found within your main.h file as normal here...</span>

  <span class="p">#endif</span>
</pre></div></div><p>

Now, it doesn&#39;t matter WHICH source files are trying to include that header file... when it gets compiled and linked, it will only include it once.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Sat, 28 Feb 2009 06:29:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If you have a global that you will use in multiple places, pick one of the files to be its &quot;official declaration&quot; file. Everywhere else, define those variables as &quot;extern&quot;.</p><p>main.c:</p><p>  int MAP_WIDTH;<br />  int MAP_HEIGHT;</p><p>video.c:</p><p>  extern int MAP_WIDTH;<br />  extern int MAP_HEIGHT;</p><p>logic.c:</p><p>  // same as in video.c:<br />  extern int MAP_WIDTH;<br />  extern int MAP_HEIGHT;
</p></div></div><p>

No, that&#39;s not correct. If you need to access data from outside the source file, you need to include the header that declares it, and in that header is where it should be  declared as extern. Then, define that data in exactly one source file. The header makes it available for reference, the source file gives it a place to be stored. When the multiple object files consisting of the project are linked together there will only be one actual definition then.</p><p>global_data.h
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//</span>

<span class="p">#ifndef global_data_H</span>
<span class="p">#define global_data_H</span>

<span class="p">#include "Settings_struct.h"</span>

<span class="k1">extern</span> Settings settings<span class="k2">;</span>
<span class="c">// declare other globally accessible data as extern here</span>

<span class="p">#endif // global_data_H</span>

<span class="c">//</span>
</pre></div></div><p>

global_data.c
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//</span>

Settings settings<span class="k2">;</span>

<span class="c">//</span>
</pre></div></div><p>

main.c
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//</span>
<span class="p">#include "global_data.h"</span>

<span class="c">/// In main...</span>
   LoadSettings<span class="k2">(</span><span class="k3">&amp;</span>settings , <span class="s">"config_file.txt"</span><span class="k2">)</span><span class="k2">;</span>

<span class="c">//</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 28 Feb 2009 11:26:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
No, that&#39;s not correct. If you need to access data from outside the source file, you need to include the header that declares it, and in that header is where it should be declared as extern. Then, define that data in exactly one source file. The header makes it available for reference, the source file gives it a place to be stored. When the multiple object files consisting of the project are linked together there will only be one actual definition then.
</p></div></div><p>

Both ways are equally correct.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Sat, 28 Feb 2009 12:17:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Both ways are equally correct.
</p></div></div><p>Both ways look identical to the compiler.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 28 Feb 2009 12:24:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Both ways look identical to the compiler.
</p></div></div><p>

Yea, I should have extended that a bit: Both ways are equally correct in that to the comiler they&#39;re the same so the only real difference is in programming style.</p><p>I do it a similar way to Edgar.  I have a &quot;globals.h&quot; file that contains all the extern declarations. I put the non-extern declarations in main.c and include globals.h in any other .c file that needs access to any of the globals.  Header guards prohibit multiple declarations.</p><p>EDIT:  N.b. I sometimes use declaration when I mean definition, and vice versa.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Sat, 28 Feb 2009 12:31:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, I guess that method would work, but it&#39;s not good practice to do that manually. The extern declaration belongs in a header because it gives you the interface. When the preprocessor runs, the effect would be the same but it&#39;s bound to give you problems trying to remember to manually add every external symbol to every file you want to access them in. Putting them into a header also eliminates unnecessary code replication as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 28 Feb 2009 12:45:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Well, I guess that method would work
</p></div></div><p>

Just for clarification, I assume that by &quot;that method&quot; you mean what OnlineCop described, since the way that I mentioned does put the extern declarations in a header file.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Sat, 28 Feb 2009 12:51:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I suppose I should have been more explicit then. Yes, adding an extern declaration to each source file manually will work, but it is better practice to do that by including a header containing the extern declarations instead.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 28 Feb 2009 12:58:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If one of the multiple definitions is BITMAP, <a href="http://www.allegro.cc/forums/thread/515597/515615#target">remember to do this.</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Sat, 28 Feb 2009 13:13:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for your quick replies and the clarifications on the whole extern / global declarations topic.</p><p>But that doesn&#39;t seem to be my problem, I am sorry if I didn&#39;t express myself good enough.</p><p>GCC complains about the multiple inclusion of allegro.h.</p><p>Let me give you an example, which describes exactly my problem:</p><p>main.c:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include "main.h"</span>

<span class="k1">int</span> main<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>
<span class="k2">{</span>
    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

main.h:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#ifndef MAIN_H_INCLUDED</span>
<span class="p">#define MAIN_H_INCLUDED</span>

<span class="p">#include &lt;allegro.h&gt;</span>

<span class="k1">int</span> abc <span class="k3">=</span> KEY_UP<span class="k2">;</span>

<span class="p">#endif</span>
</pre></div></div><p>

init.c:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include "init.h"</span>

<span class="k1">void</span> foo<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
<span class="k2">}</span>
</pre></div></div><p>

init.h:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#ifndef INIT_H_INCLUDED</span>
<span class="p">#define INIT_H_INCLUDED</span>

<span class="p">#include &lt;allegro.h&gt;</span>

<a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>noob<span class="k2">;</span>

<span class="k1">void</span> foo<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>

<span class="p">#endif</span>
</pre></div></div><p>

I am compiling this with <span class="source-code">gcc <span class="k3">-</span>std<span class="k3">=</span>gnu99 <span class="k3">-</span>Wall <span class="k3">-</span>O2 `allegro-config <span class="k3">-</span><span class="k3">-</span>libs` main.c init.c <span class="k3">-</span>o main</span> and I get a huge list of multiple definition errors:</p><div class="source-code snippet"><div class="inner"><pre><span class="k3">/</span>tmp<span class="k3">/</span>ccU6qa1p.o: In function `bmp_read24<span class="s">':</span>
<span class="s">init.c:(.text+0x0): multiple definition of `bmp_read24'</span>
<span class="k3">/</span>tmp<span class="k3">/</span>cctySkfU.o:main.c:<span class="k2">(</span>.text<span class="k3">+</span><span class="n">0x0</span><span class="k2">)</span><span class="k2">:</span> first defined here
<span class="k3">/</span>tmp<span class="k3">/</span>ccU6qa1p.o: In function `bmp_write24<span class="s">':</span>
<span class="s">init.c:(.text+0x20): multiple definition of `bmp_write24'</span>
<span class="k3">/</span>tmp<span class="k3">/</span>cctySkfU.o:main.c:<span class="k2">(</span>.text<span class="k3">+</span><span class="n">0x20</span><span class="k2">)</span><span class="k2">:</span> first defined here
</pre></div></div><p>

Like I said in my first post, both modules make use of the Allegro library.<br />(Of course the example doesn&#39;t make much sense since main.c makes no use of init.c - but it is just a simplified example.)<br />But how to include allegro.h in both header files without getting the above errors?</p><p>Greetings,<br />Paul
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul Wilhelm)</author>
		<pubDate>Sat, 28 Feb 2009 13:40:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There is one problem in your main.h header. It defines an int named abc instead of declaring it. If more than one source module includes main.h, then each will have defined their own &#39;int abc&#39;. Then when they are linked together it will give you a redefinition error for it. Similarly, your init.h header does the same thing with your &#39;BITMAP* noob&#39; variable definition.</p><p>The redefinition errors you are getting don&#39;t seem related though. Try moving the `allegro-config --libs` option to the end of the compilation command.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 28 Feb 2009 13:59:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That is the thing. I am now actually only compiling these two exact files, which both don&#39;t include each others header files.</p><p>I am aware of the problem you just described, when other modules include the same definitions.</p><p>I moved the allegro-config option to the end of the command, like you said, but it doesn&#39;t seem to matter.</p><p>I really don&#39;t know what to do... <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul Wilhelm)</author>
		<pubDate>Sat, 28 Feb 2009 14:05:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Your code is fine, as I was able to build it with no errors (except for the missing END_OF_MAIN() which I needed since I&#39;m on Windows).</p><p>Try comiling the sources individually and link the object files with the allegro library.</p><p>EDIT:</p><p>I jumped to the command line, and the following worked for me:
</p><pre>gcc -o main.exe main.c init.c -lalleg -Wall -O2 -std=gnu99</pre><p>

If you replace -lalleg with `allegro-config --libs`, does it work?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Sat, 28 Feb 2009 14:08:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, those are linking errors, not compilation errors, so perhaps `allegro-config --libs` is not giving you the correct output somehow.</p><p>Start from the beginning - post which OS and OS version you are compiling on, as well as which version of Allegro you are using. Post the output of `allegro-config --libs` by itself as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 28 Feb 2009 14:19:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Allegro Version: 4.2.2<br />Output of <i>allegro-config --libs</i>: -L/usr/lib -Wl,-Bsymbolic-functions -lalleg-4.2.2</p><p>I am using Ubuntu 8.10 (intrepid).</p><p>If I replace the allegro-config parameter by -lalleg, I get even more error output.</p><p><b>EDIT:</b> WOOT! I experimented, and it works when I leave out the <i>-std=gnu99</i>.</p><p>Any explanations? Thank you all for your help so far. :-)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul Wilhelm)</author>
		<pubDate>Sat, 28 Feb 2009 14:34:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Any explanations?
</p></div></div><p>IIRC C99 changes how &quot;extern inline&quot; (or something like that) works.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 28 Feb 2009 14:39:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
IIRC C99 changes how &quot;extern inline&quot; (or something like that) works.
</p></div></div><p>

So why did it compile without a problem for me with that option? Some difference in the Windows and Linux builds of Allegro?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Sat, 28 Feb 2009 14:44:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>LennyLen: Which GCC version are you using?</p><p>Mine is 4.3.2. I just read that gcc does not implement C99 completely yet.<br />Maybe you use a different version of GCC in which it coincidentally works...but that would be very strange.</p><p>However, I&#39;m using the C89 standard now. Now I can&#39;t do cool stuff like for (int a = 0; ...) anymore, but I can live with that.</p><p>Thanks so far!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul Wilhelm)</author>
		<pubDate>Sat, 28 Feb 2009 15:30:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Mine is 4.3.2. I just read that gcc does not implement C99 completely yet.<br />Maybe you use a different version of GCC in which it coincidentally works...but that would be very strange.
</p></div></div><p>

I&#39;m using GCC 3.4.5, which is the current Windows version. MinGW is getting a little bit behind - even the WIP branch is only GCC 4.3.0.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (LennyLen)</author>
		<pubDate>Sat, 28 Feb 2009 16:07:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">LennyLen said:</div><div class="quote"><p>
I&#39;m using GCC 3.4.5, which is the current Windows version...
</p></div></div><p>

Are you sure?  My mingw version shows:
</p><div class="source-code snippet"><div class="inner"><pre>gcc <span class="k2">(</span>GCC<span class="k2">)</span> <span class="n">4</span>.<span class="n">3</span>.<span class="n">0</span> <span class="n">20080305</span> <span class="k2">(</span>alpha-testing<span class="k2">)</span> mingw-20080502
Copyright <span class="k2">(</span>C<span class="k2">)</span> <span class="n">2008</span> Free Software Foundation, Inc.
This is <a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a> software<span class="k2">;</span> see the source <span class="k1">for</span> copying conditions.  There is NO
warranty<span class="k2">;</span> <span class="k1">not</span> even <span class="k1">for</span> MERCHANTABILITY <span class="k1">or</span> FITNESS FOR A PARTICULAR PURPOSE.

g<span class="k3">+</span><span class="k3">+</span> <span class="k2">(</span>GCC<span class="k2">)</span> <span class="n">4</span>.<span class="n">3</span>.<span class="n">0</span> <span class="n">20080305</span> <span class="k2">(</span>alpha-testing<span class="k2">)</span> mingw-20080502
Copyright <span class="k2">(</span>C<span class="k2">)</span> <span class="n">2008</span> Free Software Foundation, Inc.
This is <a href="http://www.delorie.com/djgpp/doc/libc/libc_350.html" target="_blank">free</a> software<span class="k2">;</span> see the source <span class="k1">for</span> copying conditions.  There is NO
warranty<span class="k2">;</span> <span class="k1">not</span> even <span class="k1">for</span> MERCHANTABILITY <span class="k1">or</span> FITNESS FOR A PARTICULAR PURPOSE.
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Sun, 01 Mar 2009 01:29:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
gcc (GCC) 4.3.0 20080305 (<b>alpha-testing</b>) mingw-20080502
</p></div></div><p>
Emphasis added.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Sun, 01 Mar 2009 02:43:39 +0000</pubDate>
	</item>
</rss>
