<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Question regarding header files</title>
		<link>http://www.allegro.cc/forums/view/617632</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 22 Nov 2018 00:51:29 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This is either a stupid question or a stupid programmer. I am following the tutorials but running into a problem. I copied the code from <a href="https://wiki.allegro.cc/index.php?title=Creating_a_fullscreen_display_with_maximum/minimum_resolution"> this </a> tutorial on creating a full screen display. It works, but I want to make it a header file. When I copy the code to a header file and compile, I get an &quot;defined reference to disp_data&quot; error. I did copy the ALLEGRO_DISPLAY_MODE disp_data statement into the file. What am I doing wrong?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob Keane)</author>
		<pubDate>Wed, 21 Nov 2018 18:38:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Would be helpful if you posted the actual header file.</p><p>Probably the reason is, if you have
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY_MODE"><span class="a">ALLEGRO_DISPLAY_MODE</span></a>   disp_data<span class="k2">;</span>
</pre></div></div><p>
in a header file, that will put a separate definition of <span class="source-code">disp_data</span> into every C file that includes it. So when you link, you will get multiple definition errors. Is that what you&#39;re seeing?<br />The solution would be to declare it as <span class="source-code"><span class="k1">extern</span></span> in the header and have one C that has the actual definition.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Wed, 21 Nov 2018 18:55:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617632/1040151#target">Bob Keane</a> said:</div><div class="quote"><p>This is either a stupid question or a stupid programmer</p></div></div><p>
Well, it seems dubious that you would really need to &quot;make it a header&quot;. The instructions for setting a graphic mode have to end up in a function (main() or any other), and this function has to end up in a C or CPP file that you compile and then link.<br />What a header file typically contains is:<br />- macros and #define-ed constants<br />- function declarations (signatures only: the body { ... }is replaced by a ; )<br />- enums<br />- typedefs and structs (very often used together)<br />- enums<br />- extern declarations, if you need any</p><p>- never a full function<br />- never any variable
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Wed, 21 Nov 2018 20:58:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the replies. The reason I want to put it in a header file is because I want to write a two player game and write duplicate code for the server and client. I guess there are no stupid questions, just stupid programmers.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob Keane)</author>
		<pubDate>Wed, 21 Nov 2018 23:24:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Bob : By your comment I guess you don&#39;t know how networking works.<br />      Don&#39;t worry nobody knows from start, just try some &quot;Chat program in C/C++&quot; on YouTube then you&#39;ll have Idea of what it actually is and how it works.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Doctor Cop)</author>
		<pubDate>Wed, 21 Nov 2018 23:41:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Bob, take a look at this wiki article (doesn&#39;t seem to be on the Github wiki though):</p><p><a href="https://wiki.allegro.cc/index.php?title=Header_file">https://wiki.allegro.cc/index.php?title=Header_file</a></p><p>That explains how and why to use header files. What you effectively want to do is turn your common code into a &quot;library&quot; of sorts. You can do this, but it&#39;s important to understand the purpose of the header file versus the source file. The variables and functions need to go in the source file. The header file is just for declarations (hints to the compiler that things exist either in other source files or later on in this one).</p><div class="source-code"><div class="toolbar"><span class="name">common.h</span><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span><span class="c">// extern makes this a declaration; no variable exists in memory for it yet, but</span>
<span class="number"> 2</span><span class="c">// this declaration promises that there will be one.</span>
<span class="number"> 3</span><span class="k1">extern</span> <a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a> <span class="k3">*</span> display<span class="k2">;</span>
<span class="number"> 4</span>
<span class="number"> 5</span><span class="c">// Similarly, a function declaration promises that the function will exist somewhere</span>
<span class="number"> 6</span><span class="c">// later on, but so far it hasn't been defined.</span>
<span class="number"> 7</span><span class="k1">int</span> setup_allegro<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</div></div><p>

(Header guards withheld to keep things simpler for the discussion)</p><div class="source-code"><div class="toolbar"><span class="name">common.c</span><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="c">// This actually allocates memory for the variable in the program.</span>
<span class="number">  2</span><a href="http://www.allegro.cc/manual/ALLEGRO_DISPLAY"><span class="a">ALLEGRO_DISPLAY</span></a> <span class="k3">*</span> display<span class="k2">;</span>
<span class="number">  3</span>
<span class="number">  4</span><span class="k1">int</span> setup_allegro<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  5</span>    <span class="c">// Contrived example..</span>
<span class="number">  6</span>
<span class="number">  7</span>    <span class="k1">if</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/al_init"><span class="a">al_init</span></a><span class="k2">(</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="number">  8</span>    <span class="k1">if</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/al_install_keyboard"><span class="a">al_install_keyboard</span></a><span class="k2">(</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="number">  9</span>
<span class="number"> 10</span>    display <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_display"><span class="a">al_create_display</span></a><span class="k2">(</span><span class="n">1024</span>, <span class="n">768</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>    <span class="k1">if</span><span class="k2">(</span><span class="k3">!</span>display<span class="k2">)</span> <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 12</span>
<span class="number"> 13</span>    <span class="k1">return</span> <span class="n">1</span><span class="k2">;</span>
<span class="number"> 14</span><span class="k2">}</span>
</div></div><p>

</p><div class="source-code"><div class="toolbar"><span class="name">client.c</span><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="p">#include "common.h"</span>
<span class="number">  2</span>
<span class="number">  3</span><span class="k1">int</span> client<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  4</span>    setup_allegro<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span>
<span class="number">  6</span>    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number">  7</span><span class="k2">}</span>
<span class="number">  8</span>
<span class="number">  9</span><span class="k1">int</span> main<span class="k2">(</span><span class="k1">int</span> argc, <span class="k1">char</span> <span class="k3">*</span> argv<span class="k2">[</span><span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 10</span>    <span class="k1">return</span> client<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span><span class="k2">}</span>
</div></div><p>

</p><div class="source-code"><div class="toolbar"><span class="name">server.c</span><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="p">#include "common.h"</span>
<span class="number">  2</span>
<span class="number">  3</span><span class="k1">int</span> server<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  4</span>    setup_allegro<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  5</span>
<span class="number">  6</span>    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number">  7</span><span class="k2">}</span>
<span class="number">  8</span>
<span class="number">  9</span><span class="k1">int</span> main<span class="k2">(</span><span class="k1">int</span> argc, <span class="k1">char</span> <span class="k3">*</span> argv<span class="k2">[</span><span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 10</span>    <span class="k1">return</span> server<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span><span class="k2">}</span>
</div></div><p>

You can either setup the programs as two separate projects, compiling &quot;common.c&quot; twice, or you can compile common.c into a library (static or dynamic) and link to it from each program. The easiest thing is to just compile it again in each program, but you need to keep in mind that when you make changes to it you need to recompile all the programs that use it again. Whereas if you were to create a dynamic library for it then you could just update that library file and as long as signatures haven&#39;t changed existing programs can load the new code without recompiling.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Thu, 22 Nov 2018 00:51:29 +0000</pubDate>
	</item>
</rss>
