<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nemediano &#187; makefile</title>
	<atom:link href="http://www.nemediano.com.mx/tag/makefile/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nemediano.com.mx</link>
	<description>Reflexiones de un espiritu curioso</description>
	<lastBuildDate>Fri, 16 Jul 2010 22:07:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Como hacer Makefiles para programar en OpenGL y glut</title>
		<link>http://www.nemediano.com.mx/2007/como-hacer-makefiles-para-programar-en-opengl-y-glut/</link>
		<comments>http://www.nemediano.com.mx/2007/como-hacer-makefiles-para-programar-en-opengl-y-glut/#comments</comments>
		<pubDate>Sat, 01 Sep 2007 15:37:22 +0000</pubDate>
		<dc:creator>nemediano</dc:creator>
				<category><![CDATA[Computación]]></category>
		<category><![CDATA[Mis demonios]]></category>
		<category><![CDATA[Recetario de Ubuntu]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[geany]]></category>
		<category><![CDATA[glut]]></category>
		<category><![CDATA[makefile]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://nemediano.wordpress.com/2007/09/01/como-hacer-makefiles-para-programar-en-opengl-y-glut/</guid>
		<description><![CDATA[Bueno en el post pasado aprendimos como instalar el compilador y las librerías(bibliotecas para los puristas del idioma) en Ubuntu, para poder hacer nuestros propios programas de OpeGL y glut. Supongo que si estas leyendo esto es que todavía no sabes muy bien como programar en OpenGL y probablemente quieres un entorno de desarrollo medio [...]]]></description>
			<content:encoded><![CDATA[<p>Bueno en el post pasado aprendimos como instalar el compilador y las librerías(bibliotecas para los puristas del idioma) en Ubuntu, para poder hacer nuestros propios programas de OpeGL y glut. Supongo que si estas leyendo esto es que todavía no sabes muy bien como programar en OpenGL y probablemente quieres un entorno de desarrollo medio amigable para hacer todo el show de codifico, intento compilar, corrijo errores, va de nuevo.Lo mas recomendable para hacer esto es construir un makefile (por eso en el post pasado instalamos el paquete build-essentials).<span id="more-21"></span></p>
<p>El makefile es un archivo de texto plano, que usa la herramienta make, dicho archivo debe ser escrito por el desarrollador y dice como se compila el programa que escribió. Usualmente los makefiles son complejos y contienen un montón de reglas, sin embargo nosotros <strong>nos vamos a limitar a tres</strong>: una para compilar, una para limpiar y otra para reconstruir.</p>
<p>Así que abrimos nuestro editor de texto plano favoritos y construimos un archivo que tenga por nombre makefile (el nombre es muy importante).</p>
<p>Si nuestro programa solo va tener un archivo fuente esta sera nuestra plantilla:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="make" style="font-family:monospace;">LIBDIR <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>L<span style="color: #004400;">/</span>usr<span style="color: #004400;">/</span>X11R6<span style="color: #004400;">/</span>lib
CC <span style="color: #004400;">=</span> gcc
CFLAGS <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>Wall
LIBRARIES <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>lglut <span style="color: #004400;">-</span>lGL <span style="color: #004400;">-</span>lGLU <span style="color: #004400;">-</span>lm
ejecutable<span style="color: #004400;">:</span> fuente<span style="color: #004400;">.</span>c
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>o <span style="color: #000088; font-weight: bold;">$@</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LIBDIR</span><span style="color: #004400;">&#41;</span> <span style="color: #000088; font-weight: bold;">$?</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LIBRARIES</span><span style="color: #004400;">&#41;</span>
destroy<span style="color: #004400;">:</span>
	rm <span style="color: #004400;">-</span>f <span style="color: #004400;">*.</span>o ejecutable
clean<span style="color: #004400;">:</span>
	rm <span style="color: #004400;">-</span>f <span style="color: #004400;">*.</span>o</pre></td></tr></table></div>

<p>En donde <em>ejecutable</em> es el nombre que queremos que tenga nuestro archivo ejecutable y <em>fuente</em> es el nombre de nuestro <strong>único</strong> archivo fuente.</p>
<p>Analizando un poco vemos que el makefile define unas variables en las primeras lineas, y luego define unas reglas, las reglas siempre son de la forma:</p>
<p>[objetivo]: [dependencia]<br />
[comandos]</p>
<p>Donde <em>objetivo</em> es el nombre de la operación que queremos hacer, <em>dependencia</em> es todo lo que necesitamos para llevarla a acabo y <em>comandos</em> es lo que debemos hacer para llevar a cabo la operación.</p>
<p>Las reglas que defino son <em>ejecutable</em> que sirve para hacer el ejecutable, <em>clean</em> que sirve para borrar todos los los archivos intermedios que hace el compilador (por ejemplo los .o) y <em>destroy</em> que borra todo lo que hace el compilador y nos deja listos para intentar compilar de nuevo (por ejemplo en nuestro periodo de ensayo error).</p>
<p>Se observa que uso gcc como compilador, por que el lenguaje C es usualmente nuestro default, si piensan programar en c++, cambien gcc por g++. Tambien fijense que entra las librerías mando a GL a GLU y a glut, que es por que queremos hacer un programa de OpenGL con glut, pero además mando un misterioso -lm, estas son as demás librerías de default de C, por ejemplo si en nuestro programa queremos usar funciones definidas en math.h.</p>
<p>Si queremos hacer un programa con mas de un código fuente esta es otra plantilla:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="make" style="font-family:monospace;">LIBDIR <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>L<span style="color: #004400;">/</span>usr<span style="color: #004400;">/</span>X11R6<span style="color: #004400;">/</span>lib
CC <span style="color: #004400;">=</span> gcc
CFLAGS <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">COMPILERFLAGS</span><span style="color: #004400;">&#41;</span>
LIBRARIES <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>lglut <span style="color: #004400;">-</span>lGL <span style="color: #004400;">-</span>lGLU <span style="color: #004400;">-</span>lm
OBJ <span style="color: #004400;">=</span> fuente1<span style="color: #004400;">.</span>o fuente2<span style="color: #004400;">.</span>o fuente3<span style="color: #004400;">.</span>o fuente4<span style="color: #004400;">.</span>o
BIN <span style="color: #004400;">=</span> ejecutable
&nbsp;
all<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">BIN</span><span style="color: #004400;">&#41;</span>
<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">BIN</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">OBJ</span><span style="color: #004400;">&#41;</span>
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>o <span style="color: #000088; font-weight: bold;">$@</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LIBDIR</span><span style="color: #004400;">&#41;</span> <span style="color: #000088; font-weight: bold;">$?</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LIBRARIES</span><span style="color: #004400;">&#41;</span>
&nbsp;
fuente1<span style="color: #004400;">.</span>o<span style="color: #004400;">:</span> fuente1<span style="color: #004400;">.</span>c
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>c fuente1<span style="color: #004400;">.</span>c <span style="color: #004400;">-</span>o fuente1<span style="color: #004400;">.</span>o <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span>
&nbsp;
fuente2<span style="color: #004400;">.</span>o<span style="color: #004400;">:</span> fuente2<span style="color: #004400;">.</span>c
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>c fuente2<span style="color: #004400;">.</span>c <span style="color: #004400;">-</span>o fuente2<span style="color: #004400;">.</span>o <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span>
&nbsp;
fuente3<span style="color: #004400;">.</span>o<span style="color: #004400;">:</span> fuente3<span style="color: #004400;">.</span>c
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>c fuente3<span style="color: #004400;">.</span>c <span style="color: #004400;">-</span>o fuente3<span style="color: #004400;">.</span>o <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span>
&nbsp;
fuente4<span style="color: #004400;">.</span>o<span style="color: #004400;">:</span> fuente4<span style="color: #004400;">.</span>c
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>c fuente4<span style="color: #004400;">.</span>c <span style="color: #004400;">-</span>o fuente4<span style="color: #004400;">.</span>o <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span>
&nbsp;
destroy<span style="color: #004400;">:</span>
	rm <span style="color: #004400;">-</span>f <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">OBJ</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">BIN</span><span style="color: #004400;">&#41;</span>
clean<span style="color: #004400;">:</span>
	rm <span style="color: #004400;">-</span>f <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">OBJ</span><span style="color: #004400;">&#41;</span></pre></td></tr></table></div>

<p>En donde <em>fuente1</em>, &#8230; <em>fuentei</em>, son todos los archivos fuentes que componen el programa y <em>ejecutable</em> es el nombre del ejecutable.</p>
<h2><strong>Ahora lo importante:</strong></h2>
<p>Es ampliamente recomendado tener una carpeta donde guardar todo lo referente a nuestro programa y solo eso, es decir mi fuente y mi makefile deben estar en una carpeta para ellos solos (esto para facilitar su transporte) y que no se vayan a volar algo sin querer al escribir una regla mal del make XD<br />
Para compilar nuestro programa simplemente escribimos en la linea de comandos &#8220;make&#8221;: (obviamente debemos estar parados en el directorio donde esta todo):</p>
<pre>$make</pre>
<p>Si nuestro programa compilo correctamente genero el archio ejecutable, <span style="text-decoration: underline;">pero si tuvimos errores, veremos los errores y warnings en la shell</span> y debemos de hacer los siguiente:<br />
ejecutar <strong>make destroy</strong>, para limpiar toda al basura generada por la compilación fallida, corregir nuestro fuente y luego volver a intentar compilar.</p>
<pre>$make destroy<strong> </strong></pre>
<h2>Algunas observaciones:</h2>
<ul>
<li>Estos makefiles son solo recomendaciones y de hecho no siguen las reglas GNU de los makefiles, para aprender mas de eso pueden visitar: http://www.gnu.org/software/make/</li>
<li>Para escribir este post me base en la guia publicada por el area de linux de la fesa, quizas si le dan un ojo les quede mas claro http://www.acatlan.unam.mx/linux/graficacion/compilalinux.html</li>
<li>Si nuestro fuente tiene cabeceras por ejemplo cabecera.h, estas no necesitan ser compiladas y por lo tanto no se ponen en el make, basta que en el fuente sean usadas #include, Siempre que la cabecera no tenga código en C, si no mas bien declaraciones de estructuras y variables globales (que es como debe ser).</li>
</ul>
<a href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.nemediano.com.mx%2F2007%2Fcomo-hacer-makefiles-para-programar-en-opengl-y-glut%2F&amp;t=Como%20hacer%20Makefiles%20para%20programar%20en%20OpenGL%20y%20glut" id="facebook_share_icon_21" style="font-size:11px; line-height:13px; font-family:'lucida grande',tahoma,verdana,arial,sans-serif; text-decoration:none;"><img src="http://b.static.ak.fbcdn.net/images/share/facebook_share_icon.gif" alt="Share on Facebook" /></a>
	<script type="text/javascript">
	var button = document.getElementById('facebook_share_link_21') || document.getElementById('facebook_share_icon_21') || document.getElementById('facebook_share_both_21') || document.getElementById('facebook_share_button_21');
	if (button) {
		button.onclick = function(e) {
			var url = this.href.replace(/share\.php/, 'sharer.php');
			window.open(url,'sharer','toolbar=0,status=0,width=626,height=436');
			return false;
		}
	
		if (button.id === 'facebook_share_button_21') {
			button.onmouseover = function(){
				this.style.color='#fff';
				this.style.borderColor = '#295582';
				this.style.backgroundColor = '#3b5998';
			}
			button.onmouseout = function(){
				this.style.color = '#3b5998';
				this.style.borderColor = '#d8dfea';
				this.style.backgroundColor = '#fff';
			}
		}
	}
	</script>
	]]></content:encoded>
			<wfw:commentRss>http://www.nemediano.com.mx/2007/como-hacer-makefiles-para-programar-en-opengl-y-glut/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
