<?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>Linux Game Publishing Blog &#187; compiler</title>
	<atom:link href="http://blog.linuxgamepublishing.com/tag/compiler/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.linuxgamepublishing.com</link>
	<description>Commercial gaming for Linux</description>
	<lastBuildDate>Mon, 05 Apr 2010 14:50:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting gcc to warn you when you mess up stdargs</title>
		<link>http://blog.linuxgamepublishing.com/2010/01/20/getting-gcc-to-warn-you-when-you-mess-up-stdargs/</link>
		<comments>http://blog.linuxgamepublishing.com/2010/01/20/getting-gcc-to-warn-you-when-you-mess-up-stdargs/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 12:02:48 +0000</pubDate>
		<dc:creator>Michael Simms (CEO and head of Development)</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[printf]]></category>
		<category><![CDATA[stdargs]]></category>
		<category><![CDATA[warnings]]></category>

		<guid isPermaLink="false">http://blog.linuxgamepublishing.com/?p=405</guid>
		<description><![CDATA[Sometimes, you may write functions in C that do things in the same way as printf, using stdargs.
An example of this would be something like this short debug function
int ptf(const char *fmt,...)
{
  va_list varlist;
  FILE *fp=fopen("/tmp/debug","a");
  va_start(varlist,fmt);
  vfprintf(fp,fmt,varlist);
  va_end(varlist);
  fclose(fp);
}
This function isn&#8217;t rocket science, it just simply appends your [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you may write functions in C that do things in the same way as printf, using stdargs.</p>
<p>An example of this would be something like this short debug function</p>
<pre>int ptf(const char *fmt,...)
{
  va_list varlist;
  FILE *fp=fopen("/tmp/debug","a");
  va_start(varlist,fmt);
  vfprintf(fp,fmt,varlist);
  va_end(varlist);
  fclose(fp);
}</pre>
<p>This function isn&#8217;t rocket science, it just simply appends your string into a file. It is a simple time saver utility.</p>
<p>However, using it can be a problem. You can do something like this</p>
<pre>int x=1;
ptf("Error %s\n",x);</pre>
<p>And gcc will say &#8217;sure, no problem&#8217;.</p>
<p>But running the code will always crash. It tries to interpret the integer as a string.</p>
<p>This is the kind of thing that should be picked up on by the compiler. And in fact it can be, quite easily.</p>
<p>In your prototype for the function, you would have something like</p>
<pre>extern int ptf(const char *,...);</pre>
<p>This is pretty standard, and no surprises there. However, gcc has the capability to be given a hint as to how this function should be handled. You can instead prototype the function using</p>
<pre>extern int ptf(const char *,...) __attribute__ ((format (printf, 1, 2)));</pre>
<p>This tells gcc to treat the parameters 1 and 2 as the parameters to printf (which it knows how to check for errors). It will then check parameter 1 (the format string) against what is passed in starting at parameter 2 (the &#8230;). If an incorrect data type is used, this will now be detected and flagged up as a warning, in exactly the same way as an incorrect type used in a printf.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.linuxgamepublishing.com%2F2010%2F01%2F20%2Fgetting-gcc-to-warn-you-when-you-mess-up-stdargs%2F&amp;linkname=Getting%20gcc%20to%20warn%20you%20when%20you%20mess%20up%20stdargs"><img src="http://blog.linuxgamepublishing.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.linuxgamepublishing.com/2010/01/20/getting-gcc-to-warn-you-when-you-mess-up-stdargs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Some gotchyas in porting from Visual C++ to g++</title>
		<link>http://blog.linuxgamepublishing.com/2009/12/03/some-gotchyas-in-porting-from-visual-c-to-g/</link>
		<comments>http://blog.linuxgamepublishing.com/2009/12/03/some-gotchyas-in-porting-from-visual-c-to-g/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 18:17:04 +0000</pubDate>
		<dc:creator>Michael Simms (CEO and head of Development)</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[forceinline]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[g++]]></category>
		<category><![CDATA[inline]]></category>
		<category><![CDATA[malloc]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[visual c++]]></category>

		<guid isPermaLink="false">http://blog.linuxgamepublishing.com/?p=385</guid>
		<description><![CDATA[Todays technical article is going to go into a couple of issues that we have run across porting from Visual C++ to g++.
I will say right now that I am an application developer, I don&#8217;t know the fine details of how the inner workings of the compilers work, so some of this is merely an [...]]]></description>
			<content:encoded><![CDATA[<p>Todays technical article is going to go into a couple of issues that we have run across porting from Visual C++ to g++.</p>
<p>I will say right now that I am an application developer, I don&#8217;t know the fine details of how the inner workings of the compilers work, so some of this is merely an educated guess as to WHY the problem happens, but the effect and solution is correct.</p>
<p>The first issue for today is a fairly rare situation where g++, upon hitting a certain piece of code that builds fine under Visual C++, gives the helpful error message:</p>
<pre><em>some/source/file.cpp:172: sorry, unimplemented: called from here</em></pre>
<p>Now, I don&#8217;t know about you, but I find that error message singularly unhelpful. It took some time to run this one down the first time. It seems  that g++ and VC++ perform some symbol resolution in different passes to each other. Because what this message is saying is that &#8216;at line 172, you have tried to use __forceinline, or something you reference has a member function that has __forceinline in it, but I do not know what the thing is, so I cannot inline it&#8217;.</p>
<p style="padding-left: 30px;"><span style="color: #ff6600;">EDIT: g++ does not have the __forceinline keyword. There is an equivalent which does the same job however</span></p>
<pre style="padding-left: 30px;"><span style="color: #ff6600;">#define __forceinline inline __attribute__((always_inline))</span></pre>
<p>As VC++ has no problem with the same calls, the only real answer must be a compiler difference that allows VC++ to get away with something that g++ doesn&#8217;t. The simple solution is to just change __forceinline to inline and let the compiler inline as it sees fit. Otherwise, you will need to hunt down the exact problem and resolve the order of events for g++</p>
<p>The second issue I thought I would raise today is one that actually happens quite commonly on porting from Windows to Linux. This is the case of bad memory management.</p>
<p>On Windows, the way memory is allocated allows for a certain sloppiness in code. While HIGHLY unrecommended, this is the sort of thing that happens in Windows code all the time. The following is a highly simplified example of the problem.</p>
<pre>char *str;
str=(char *)malloc(6)
strcpy(str,"hello");
free(str)
switch (str[0])
{
  ...
}</pre>
<p>Now, as you can see here, the application uses the variable str right after it has been free()&#8217;d. On Windows it seems that this kind of thing can be gotten away with. The memory manager on Windows is highly unlikely to either assign this memory address somewhere else, or to mark it as unavailable. On Linux however, you will often find that this kind of code leads immediately to a segmentation fault.</p>
<p>The example above is highly simplified, but illustrates what some of the more complex segmentation faults we have seen boil down to.  If you see an error like this, which works on Windows and not on Linux, check your order of accessing. I know it sounds obvious, but it happens so often in commercial code that I feel it is worth stressing &#8211; <strong>free memory only when you have REALLY finished with it</strong>.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fblog.linuxgamepublishing.com%2F2009%2F12%2F03%2Fsome-gotchyas-in-porting-from-visual-c-to-g%2F&amp;linkname=Some%20gotchyas%20in%20porting%20from%20Visual%20C%2B%2B%20to%20g%2B%2B"><img src="http://blog.linuxgamepublishing.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://blog.linuxgamepublishing.com/2009/12/03/some-gotchyas-in-porting-from-visual-c-to-g/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
