LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2006, 04:22 AM   #1
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
help me to check whether this is the correct way to compile c source to library ...


i try to compile a gtk example program i copied from the net into a share library ... it did managed to compile , i can also run the main app calling the function to run the gtk program ... but i'm still not sure whether this is the "correct" way to do it ... if anybody found anything weird , please give me some hints and directions ...

callgtk02.h
Code:
#ifndef _CALLGTK02_H_
#define _CALLGTK02_H_ 1

#undef __BEGIN_DECLS
#undef __END_DECLS
#ifdef __cplusplus
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }
#else
# define __BEGIN_DECLS 
# define __END_DECLS 
#endif

__BEGIN_DECLS
extern char *call_gtk02(char *pass_strings);
__END_DECLS

#endif
callgtk02.c
Code:
#include <stdio.h>
#include <gtk/gtk.h>
#include <glib.h>
#include "callgtk02.h"

GtkWidget *file_selection_box;
gchar *filename;

gint destroyapp (GtkWidget *widget, gpointer gdata)
{
  g_print ("\n\nQuitting gtk ...\n\n");
  gtk_main_quit();
  return (FALSE);
}

void store_filename(GtkFileSelection *file_selection, gpointer data)
{
  filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(file_selection_box));
  g_print (filename);
}

void create_file_selection(void) 
{
  file_selection_box = gtk_file_selection_new("Please select a file for editing.");
  gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION(file_selection_box)->ok_button), "clicked", GTK_SIGNAL_FUNC (store_filename), NULL);
  gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(file_selection_box)->ok_button), "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) file_selection_box);
  gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION(file_selection_box)->cancel_button), "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy), (gpointer) file_selection_box);
  gtk_widget_show (file_selection_box);
}
  
gint ClosingAppWindow (GtkWidget *widget, gpointer gdata)
{
  g_print ("\n\nQuitting gtk ...\n\n");
  gtk_main_quit();
  return (FALSE);
}

char *call_gtk02(char *pass_strings)
//int main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *menuFile;
  GtkWidget *menuEdit;
  GtkWidget *menuHelp;  
  GtkWidget *menubar;
  GtkWidget *menu;
  GtkWidget *menuitem;
  GtkWidget *vbox;
  GtkWidget *handlebox;  
  GtkWidget *text;

    gchar *buffer = (char *) pass_strings;
              
  /*--  Initialize GTK --*/
  //gtk_init (&argc, &argv);
gtk_init (0, NULL);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  vbox = gtk_vbox_new(FALSE, 0);
  text = gtk_text_new(NULL, NULL);
  handlebox = gtk_handle_box_new();
  menubar = gtk_menu_bar_new();
  gtk_text_set_editable(GTK_TEXT (text), TRUE);
  gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(destroyapp), NULL);

  menuFile = gtk_menu_item_new_with_label ("File");
  gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuFile);
  gtk_widget_show(menuFile);
  menu = gtk_menu_new();
  gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuFile), menu);
  menuitem = gtk_menu_item_new_with_label ("New");
  gtk_menu_append(GTK_MENU(menu), menuitem);
  gtk_widget_show (menuitem);
  menuitem = gtk_menu_item_new_with_label ("Open");
  gtk_menu_append(GTK_MENU(menu), menuitem);
  gtk_signal_connect(GTK_OBJECT (menuitem), "activate", GTK_SIGNAL_FUNC (create_file_selection), NULL);
  gtk_widget_show (menuitem);
  menuitem = gtk_menu_item_new_with_label ("Exit");
  gtk_menu_append(GTK_MENU(menu), menuitem);
  gtk_signal_connect(GTK_OBJECT (menuitem), "activate", GTK_SIGNAL_FUNC (ClosingAppWindow), NULL);
  gtk_widget_show (menuitem);
    
  menuEdit = gtk_menu_item_new_with_label ("Edit");
  gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuEdit);
  gtk_widget_show(menuEdit);
  menu = gtk_menu_new();
  gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuEdit), menu);
  menuitem = gtk_menu_item_new_with_label ("Undo");
  gtk_menu_append(GTK_MENU(menu), menuitem);
  gtk_widget_show (menuitem);
  menuitem = gtk_menu_item_new_with_label ("Copy");
  gtk_menu_append(GTK_MENU(menu), menuitem);
  gtk_widget_show (menuitem);
  menuitem = gtk_menu_item_new_with_label ("Cut");
  gtk_menu_append(GTK_MENU(menu), menuitem);
  gtk_widget_show (menuitem);
    
  menuHelp = gtk_menu_item_new_with_label ("Help");
  gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuHelp);
  gtk_widget_show(menuHelp);
  menu = gtk_menu_new();
  gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuHelp), menu);
  menuitem = gtk_menu_item_new_with_label ("About");
  gtk_menu_append(GTK_MENU(menu), menuitem);
  gtk_widget_show (menuitem);
    
  gtk_container_add(GTK_CONTAINER(handlebox), menubar);
  gtk_box_pack_start(GTK_BOX(vbox), handlebox, FALSE, TRUE, 0);    
  gtk_container_add(GTK_CONTAINER(vbox), text);  
  gtk_container_add(GTK_CONTAINER(window), vbox);
  gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL, buffer, strlen(buffer));
  gtk_container_border_width (GTK_CONTAINER (window), 0);
  gtk_window_set_default_size (GTK_WINDOW(window), 640, 200);
  gtk_window_set_title(GTK_WINDOW (window), "File Dialog");

  gtk_widget_show(handlebox);
  gtk_widget_show(vbox);
  gtk_widget_show(text);
  gtk_widget_show(menuitem);
  gtk_widget_show(menubar);      
  gtk_widget_show(window);

    gtk_main();
    
    /*-- Return 0 if exit is successful --*/
    //return 0;
    
   char *todest = pass_strings;

   return todest;
}
note :: is it the correct way to do "gtk_init (0, NULL);" as shown above ??

main.c
Code:
#include<stdio.h>
#include<string.h>
#include "callgtk02.h"

int main ()
{
printf("\n\nget from gtk  =  %s\n\n",call_gtk02("strings from there ...\n"));
}
this is how i compile ::
Code:
gcc `pkg-config --cflags --libs gtk` callgtk02.h -c callgtk02.c
gcc `pkg-config --cflags --libs gtk` -shared -o libcallgtk02.so callgtk02.o
gcc `pkg-config --cflags --libs gtk` -lcallgtk02 -o main main.c
./main
i link the resulting .so library to "/usr/local/lib" and run "ldconfig"

hint and guide me if found redundancies and mistakes ...


//thanks in advance


.

Last edited by alred; 04-29-2006 at 04:50 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I check eagle-usb is correct James_the_Great Linux - Newbie 2 08-15-2005 05:55 PM
How can you check if video is correct? roswell1329 Linux - General 9 10-12-2004 12:16 PM
Is the output of (chapter 6)gcc-3.3.3 make -k check correct?????? linuxbh Linux From Scratch 1 09-05-2004 08:37 AM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM
Can you check my grub boot loader code and correct? Bensoft Kill MS Linux - Newbie 1 10-07-2003 04:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration