UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » How to use #include to place file's contents into a string at compile time?

#1 2007-08-24 08:50 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: How to use #include to place file's contents into a string at compile time?

Hi all,

In order to make bumping my project's version number less error-prone, I want to have the version number specified in exactly one place in the source tree.  For various reasons, that one place needs to be a plain ASCII text file called version.txt.  For example:

[email protected]:~> cat version.txt
1.2.3

... but I also want the version number to be available to my code programatically, and I want the version number to be compiled into the executable itself (not read dynamically at run time).  So I wrote this little function:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned int GetVersion()
{
   static unsigned int version = 0;
   static const char * releaseVerString = "
#include "version.txt"
   ";

   if (version == 0)
   {
      // Parse the appropriate value out of the text string
      const char * p = releaseVerString;
      while((p)&&(*p)&&(!((*p>='0')&&(*p<='9')))) p++;
      for (int i=0; i<3; i++)
      {
         if ((p)&&(*p))
         {
            version |= (atoi(p)<<((2-i)*8));
            p = strchr(p, '.');
            if (p) p++;
         }
         else break;
      }
   }
   return version;
}

Unfortunately it won't compile, because it doesn't like having a #include in the middle of my releaseVerString definition.

Is there some tricky way to make this work?

Thanks,
Jeremy

Offline

#2 2007-08-24 10:23 PM

HectorLasso
Administrator
From: Colombia
Registered: 2002-06-12
Posts: 353

Re: How to use #include to place file's contents into a string at compile time?

That is a weird idea... LOL

What if your version.txt file contains the line:

static const char * releaseVerString = "1.2.3";

And you do this:

#include "version.txt"

Offline

#3 2007-08-24 10:26 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: How to use #include to place file's contents into a string at compile time?

Can you modify the format of the "version.txt" file a bit?  If you could make the
contents be something like:

#define MY_VERSION "1.2.3"

Then, you could just directly #include it, and it'd be available as a macro...  (But, I'd
call it "version.h", in that case...)

If you can't do so for some reason, then I would suggest modifying your makefile to
do the necessary trickery of reading the file and defining the macro on the "cc"
command-line via "-D"...

Offline

#4 2007-08-24 10:38 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: How to use #include to place file's contents into a string at compile time?

Offline

#5 2007-08-24 11:24 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: How to use #include to place file's contents into a string at compile time?

Just to follow up, my solution (for now anyway) is to modify the version.txt file to look like this:

[email protected]: cat version.txt
"1.2.3"

With the quotes around it, I can then do this:

static const char * releaseVerString =
#include "version.txt"
   ;

Hopefully the addition of the quote marks won't break the other software too badly....

Offline

#6 2007-08-25 07:34 AM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: How to use #include to place file's contents into a string at compile time?

Just curious...

I realize this would add a bit of overhead but... if you are talking about 'shared code' then why not write it as such... ?!

What I mean is instead of putting all the overhead in to read in a text file, parsie it, hope everything has been typed in ( and distributed ) correctly... why not just make the code ( or part of it ) a shared library ( so ) / dll / whatever... and expose a / the 'get version' method... ?

I can't imagine the overhead going that way would be any greater than file io and text parsing...   ?!


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#7 2007-08-27 03:44 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: How to use #include to place file's contents into a string at compile time?

Offline

#8 2007-08-27 07:09 AM

eltoncold
Member
Registered: 2007-08-16
Posts: 5

Re: How to use #include to place file's contents into a string at compile time?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned int GetVersion()
{
   static unsigned int version = 0;
   static const char * releaseVerString = "
#include "version.txt"
   ";

   if (version == 0)
   {
      // Parse the appropriate value out of the text string
      const char * p = releaseVerString;
      while((p)&&(*p)&&(!((*p>='0')&&(*p<='9')))) p++;
      for (int i=0; i<3; i++)
      {
         if ((p)&&(*p))
         {
            version |= (atoi(p)<<((2-i)*8));
            p = strchr(p, '.');
            if (p) p++;
         }
         else break;
      }
   }
   return version;
}

Offline

#9 2007-08-27 12:30 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: How to use #include to place file's contents into a string at compile time?

If you're using a central Makefile you can let that read the version file and do whatever is needed for different languages (e.g. making a define for C code).

Offline

  • Index
  • » C
  • » How to use #include to place file's contents into a string at compile time?

Board footer

Powered by FluxBB