Re: [Vala] static string variable is empty!



On Wed, Jan 6, 2010 at 10:02 AM, Andrea Bolognani <eof kiyuko org> wrote:
On Tue, Jan 05, 2010 at 11:53:50AM +0330, golnaz nilieh wrote:

Hello everybody!
I'm a newbie to vala language, I faced to a strange problem recently and
couldn't find any help in vala documentations.
In my app, I have declared a class named "Global" in a file "global.vala":

using GLib;
public class MyApp.Global : GLib.Object {
    public static string dataDir = "/usr/local/share/";
}

And I have a "main.vala" file, whith this code inside:

using GLib;
using Gtk;
public class MyApp.MainWindow : Window {
   static int main (string[] args) {
      stdout.printf ("all data is in: " + Global.dataDir);
      return 0;
   }
}

Hi,

I'm really sorry, I can't help you.

But since I'm doing something similar in a software I'm writing, I'll take
the occasion and ask a little piece of advice on the list.

You are hard-coding dataDir into your application; assuming you're using
the GNU autotools, the data directory can be configured using the --prefix
option of configure. If that value is changed, the hard-coded dataDir will
not point to the right directory.

What I've done to work around the issue is to create a small utility class,
where I replace the value of prefix at build time:

       public class AutotoolsSupport : GLib.Object {
               public static const string DATA_DIR = "@prefix@/share/";
       }

The above snippet goes in autotoolssupport.vala.in, so that a suitable
autotoolssupport.vala is generated when AC_SUBS(prefix) is called in
configure.ac.

I would like to have a cleaner way to refer to @datadir@ from inside a Vala
source file. Any suggestion?


Well actually the best way is to add a target to your Makefile.am like this:

BUILT_SOURCES=config.vala
CLEANFILES=config.vala
config.vala: config.h
        echo 'namespace Config {' >$@
        echo '  internal const string PACKAGE = "$(PACKAGE)";' >>$@
        echo '  internal const string PACKAGE_BUGREPORT = "$(PACKAGE_BUGREPORT)";' >>$@
        echo '  internal const string PACKAGE_NAME = "$(PACKAGE_NAME)";' >>$@
        echo '  internal const string PACKAGE_STRING = "$(PACKAGE_STRING)";' >>$@
        echo '  internal const string PACKAGE_TARNAME = "$(PACKAGE_TARNAME)";' >>$@
        echo '  internal const string PACKAGE_URL = "$(PACKAGE_URL)";' >>$@
        echo '  internal const string PACKAGE_VERSION = "$(PACKAGE_VERSION)";' >>$@
        echo '  internal const string PACKAGE_DATADIR = "$(datadir)";' >>$@
        echo '}' >>$@

because it is also possible to specify --datadir= or --datarootdir=
when calling the configure script.

--
Ali



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]