GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Dec 20, 2014 1:33 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: C app failing after function call
PostPosted: Thu Dec 27, 2007 6:29 pm 
Offline
Familiar Face

Joined: Tue Dec 25, 2007 4:51 pm
Posts: 10
This is just a code snippet, not a complete app:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#define GLADE_FILE "glade/connmgr.glade"

typedef struct
{
   GtkWidget      *tabbed_notebook;
   GtkWidget       *host_entry;
} MyWidgets;

int create_home_appdir(char *appdir)
{
   mode_t mode = (S_IRWXU);
   
    int isdir = chdir(appdir);
   if(isdir != 0)
   {
      mkdir(appdir, mode);
   }
   
    isdir = chdir(appdir);
   if(isdir != 0)
   {
       printf("***ERROR*** Cannot see directory: %s\n", appdir);
       return 1;
    }
   
    return 0;
}

int main (int argc, char *argv[])
{
   GtkWidget      *main_dialog;
   GladeXML      *gxml;
   MyWidgets      *widgets;
   
   // create the application directory within the homespace
   char *user = getenv("HOME");
   char dotappname[10] = "/.connmgr";
   char appdir[1024];   
   strcpy(appdir, user);
   strcat(appdir, dotappname);
   create_home_appdir(appdir);
   
   widgets = g_slice_new (MyWidgets);
   
   // initialize the GTK+ library
   gtk_init (&argc, &argv);

   // build GladeXML object from the glade XML file
   gxml = glade_xml_new (GLADE_FILE, NULL, NULL);

   // get widgets from GladeXML object
   main_dialog = glade_xml_get_widget (gxml, "main_dialog");
   
   widgets->host_entry = glade_xml_get_widget (gxml, "host_entry");
}



The above code snippet does not compile correctly. Oddly enough, if I remove the line: create_home_appdir(appdir);...the snippet compiles correctly.

However, with it un-commented, it dies with the following error message:
Quote:
(connmgr:22246): libglade-WARNING **: could not find glade file 'glade/connmgr.glade'
...any ideas why?

I'm compiling the above snippet with the following line:
gcc -o connmgr src/connmgr.c `pkg-config --cflags --libs libglade-2.0`-export-dynamic


Please keep in mind, I'm just starting out with C, so feel free to give as much constructive criticism as possible.
Thanks,
-Lup


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 27, 2007 7:01 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
What it's saying is that it's not finding the glade file, "glade/connmgr.glade", relative to the path where the application is run from.

If you application is in /home/username/projects/myapp/src/ then the file 'connmgr.glade' should exist in /home/username/projects/myapp/src/data/

Usually, what we do is our installation scripts (using GNU build tools) create some constants that define various paths and install data to those paths. So a glade file might be installed to /usr/local/share/myapp/data and the make process defines a constant such as PACKAGE_DATA_DIR which is set to /usr/local/share/myapp/data.

However, since you're learning and probably not deploying your application yet, you can change:

#define GLADE_FILE "glade/connmgr.glade"

to the exact path where connmgr.glade file exists.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 27, 2007 7:15 pm 
Offline
Familiar Face

Joined: Tue Dec 25, 2007 4:51 pm
Posts: 10
Ooooooohhhh, and because within the create_home_appdir() function, I am running the line:
Code:
int isdir = chdir(appdir);
it's switching out of the current working directory, so it loses the path to the glade file.

Heh, pretty tricky...and nice catch, thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 27, 2007 7:36 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Also, another little tip since you're using all that home dir stuff, check out GLib's Miscellaneous Utility Functions

You could use g_get_home_dir(), g_build_path(), and other functions. It'll help you write cross-platform code too. By using the G_DIR_SEPARATOR_S constant, it will use "/" on linux and "\" on windows.

Same thing with strcpy and other C standard library function. GLib provides wrappers for a lot of those which may improve on them or simply wrap them but provide better cross-platform code. Check out GLib's String Utility Functions

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 27, 2007 7:40 pm 
Offline
Familiar Face

Joined: Tue Dec 25, 2007 4:51 pm
Posts: 10
I really appreciate it. I'm struggling to learn C the best way possible, and every little bit helps.


Last edited by Lupine on Thu Dec 27, 2007 7:44 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 27, 2007 7:42 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Stick with it. It's a larger learning curve but worth it in my opinion (I use Python as well for GTK+... but love me my C)

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group