0

I was following along with the "Getting Started" tutorial, and when I got to the step to add translations, my project started failing to compile. I can't make sense of the error.

It would be really handy if there were a Github repo (or something) with working code and tagged commits that follow along with the tutorial. Does such a thing exist?

If not, then I need your help! The error I'm getting is this:

$ ninja
[0/1] Regenerating build files.
The Meson build system
Version: 0.48.0
Source dir: /home/nathan/projects/fanboy
Build dir: /home/nathan/projects/fanboy/build
Build type: native build
Project name: com.github.nwallace.fanboy
Project version: undefined
Native C compiler: cc (gcc 8.2.1 "cc (GCC) 8.2.1 20180831")
Native Vala compiler: valac (valac 0.42.2)
Build machine cpu family: x86_64
Build machine cpu: x86_64
Dependency gtk+-3.0 found: YES (cached)
Build targets in project: 5
Found ninja-1.8.2 at /usr/bin/ninja
[3/4] Compiling C object 'com.github.nwallace.fanboy@exe/meson-generated_src_Application.c.o'.
FAILED: com.github.nwallace.fanboy@exe/meson-generated_src_Application.c.o 
cc -Icom.github.nwallace.fanboy@exe -I. -I.. -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/lib/libffi-3.2.1/include -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/uuid -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/gio-unix-2.0 -I/usr/include/libdrm -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -fdiagnostics-color=always -pipe -D_FILE_OFFSET_BITS=64 -w -g -DGETTEXT_PACKAGE=com.github.nwallace.fanboy -pthread  -MD -MQ 'com.github.nwallace.fanboy@exe/meson-generated_src_Application.c.o' -MF 'com.github.nwallace.fanboy@exe/meson-generated_src_Application.c.o.d' -o 'com.github.nwallace.fanboy@exe/meson-generated_src_Application.c.o' -c 'com.github.nwallace.fanboy@exe/src/Application.c'
../src/Application.vala: In function ‘__lambda4_’:
<command-line>: error: ‘com’ undeclared (first use in this function)
/usr/include/glib-2.0/glib/gi18n-lib.h:30:42: note: in expansion of macro ‘GETTEXT_PACKAGE’
 #define  _(String) ((char *) g_dgettext (GETTEXT_PACKAGE, String))
                                          ^~~~~~~~~~~~~~~
../src/Application.vala:35:47: note: in expansion of macro ‘_’
       hello_button.label = _("Hello World!");
                                               ^
<command-line>: note: each undeclared identifier is reported only once for each function it appears in
/usr/include/glib-2.0/glib/gi18n-lib.h:30:42: note: in expansion of macro ‘GETTEXT_PACKAGE’
 #define  _(String) ((char *) g_dgettext (GETTEXT_PACKAGE, String))
                                          ^~~~~~~~~~~~~~~
../src/Application.vala:35:47: note: in expansion of macro ‘_’
       hello_button.label = _("Hello World!");
                                               ^
../src/Application.vala: In function ‘my_app_real_activate’:
<command-line>: error: ‘com’ undeclared (first use in this function)
/usr/include/glib-2.0/glib/gi18n-lib.h:30:42: note: in expansion of macro ‘GETTEXT_PACKAGE’
 #define  _(String) ((char *) g_dgettext (GETTEXT_PACKAGE, String))
                                          ^~~~~~~~~~~~~~~
../src/Application.vala:32:51: note: in expansion of macro ‘_’
     var hello_button = new Gtk.Button.with_label(_("Click me!"));
                                                   ^
ninja: build stopped: subcommand failed.

You can see the full project source on my Github. Here's a link to the relevant commit that broke things: https://github.com/nwallace/fanboy/compare/add-translations

Some relevant files copy/pasted here for ease/future reference:

src/Application.vala:

class MyApp : Gtk.Application {
  public MyApp() {
    Object(
      application_id: "com.github.nwallace.fanboy",
      flags: ApplicationFlags.FLAGS_NONE
    );
  }

  protected override void activate() {
    var window = new Gtk.ApplicationWindow(this);
    var hello_button = new Gtk.Button.with_label(_("Click me!"));
    hello_button.margin = 12;
    hello_button.clicked.connect(() => {
      hello_button.label = _("Hello World!");
      hello_button.sensitive = false;
    });
    window.set_title(_("Welcome to GNOME"));
    window.set_default_size(300, 300);
    window.add(hello_button);
    window.show_all();
  }
}

public int main(string[] args) {
  return new MyApp().run(args);
}

meson.build:

# project name and programming language
project('com.github.nwallace.fanboy', 'vala', 'c')

i18n = import('i18n')

add_global_arguments('-DGETTEXT_PACKAGE=@0@'.format(meson.project_name()), language:'c')

# Create a new executable, list the files we want to compile, list the dependencies we need, and install
executable(
  meson.project_name(),
  'src/Application.vala',
  dependencies: [
    dependency('gtk+-3.0')
  ],
  install: true
)

# Translate and install our .desktop file
i18n.merge_file(
  input: join_paths('data', meson.project_name() + '.desktop.in'),
  output: meson.project_name() + '.desktop',
  po_dir: join_paths(meson.source_root(), 'po'),
  type: 'desktop',
  install: true,
  install_dir: join_paths(get_option('datadir'), 'applications')
)

subdir('po')
1

The issue in your meson.build file is that some parenthesis are missing:

add_global_arguments('-DGETTEXT_PACKAGE=@0@'.format(meson.project_name()), language:'c')

should be

add_global_arguments('-DGETTEXT_PACKAGE="@0@"'.format(meson.project_name()), language:'c')

Notice: "@0@". The format function will replace this with com.github.nwallace.fanboy because that is your project name.

Consider what happens, when the macro _ gets expanded. This is the relevant error from your log:

error: ‘com’ undeclared (first use in this function)
/usr/include/glib-2.0/glib/gi18n-lib.h:30:42: note: in expansion of macro ‘GETTEXT_PACKAGE’

The macro is defined as:

#define  _(String) ((char *) g_dgettext (GETTEXT_PACKAGE, String))

When GETTEXT_PACKAGE=com.github.nwallace.fanboy the macro expands to:

(String) ((char *) g_dgettext (com.github.nwallace.fanboy, String))

Remember that Vala compiles down to C code and that is what the macro is expanding to. The function g_dgettext however expects a String as first argument. Because com.github.nwallace.fanboy is incidentally valid C syntax, you get the error: ‘com’ undeclared.

The solution is to wrap the name of your project with ".

1

This one was difficult for me to solve on my own project. But here's what I've found. GETTEXT_PACKAGE really hates \.

To resolve this issue I had to go through each of my PO files and remove the \n in each string.

So. "Project-Id-Version: org.pantheon.dexter-contacts\n" Became "Project-Id-Version: org.pantheon.dexter-contacts"

In addition to this I had to fix my meson.build add_global_arguments('-DGETTEXT_PACKAGE=\"@0@"\'.format (meson.project_name()), language:'c')

became

add_global_arguments('-DGETTEXT_PACKAGE="@0@"'.format (meson.project_name()), language:'c')

I noticed in your PO files you still have \n. I'd recommend removing those!

  • "\n" in PO files are no problem. A correct add_global_arguments is what fixed the issue. – Oliver Jan Krylow Jan 16 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.