Thread: Compiling with GTK+

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    129

    Compiling with GTK+

    I already tried typing this twice but this New Thread page had expired and I didn't learn my lesson the first time. The problem is kinda huge, so I'll try to mention everything but probably won't.

    My program has 3 layers: the bash wrapper, the makefile, and the source. The wrapper will be discarded for deployment. I'm not totally sure which one contains the bug; I think it's in the source because the other 2 are pretty big, but I already scoured it. I provide CLI.Makefile below; GUI.Makefile is basically the same.

    The bug manifests as unresponsiveness. C prompts me for some input, but then allows me to keep typing as much as I want - even newlines. After that it just stops without even exiting; when I close the terminal, the wrapper redirects Valgrind's output, which complains of still-reachable blocks from gtk_init. This happens regardless of whether I ask for the CLI or GUI.

    If you would like, I can post a short version of Valgrind's output.

    I'm sorry to post bash code here, but I don't see a better alternative...
    Code:
    #!/bin/bash
    
    face="CLI";
    comm="valgrind";
    
    if [ "$1" != "" ]; then
    	if [ "$1" == "GUI" ] || [ "$1" == "CLI" ]; then
    		face="$1";
    	else
    		echo "Invalid argument; should be either \"CLI\" or \"GUI\".";
    		exit;
    	fi
    fi
    
    function compile(){
        make -f "$face.Makefile"
        echo;
    }
    
    function mingw(){
    	make;
    	echo;
    }
    
    function gnu(){
        compile;
        gdb ./outliner -q;
        prompt;
    }
    
    function val(){
        compile;
        valgrind --show-reachable=yes --leak-check=full --suppressions=Outliner.supp ./outliner --gen-suppressions=yes &> ./output;
        prompt;
    }
    
    function run(){
        compile;
        ./outliner;
        prompt;
    }
    
    function prompt(){
        local new="";
        local valid=1;
        
        echo;
        read -p "Enter a command [quit / gdb / valgrind / run] (default: $comm): " new;
    
        if [ "$new" = "quit" ]; then
            exit;
        fi
    
        if [ "$new" = "" ]; then
            new="$comm"
            valid=$(( $valid & ~1 )); # Flip the first bit off.
        fi
    
        if [ "$new" = "gdb" ]; then
            valid=$(( $valid | 2 )); # Flip the second bit on.
        fi
        
        if [ "$new" = "valgrind" ]; then
            valid=$(( $valid | 4 )); # Flip the third bit on.
        fi
        
        if [ "$new" = "run" ]; then
            valid=$(( $valid | 8 )); # Flip the fourth bit on.
        fi
        
        if [ $(( $valid & 14 )) != 0 ]; then # Is the command valid?
            if [ $(( $valid & 1 )) != 0 ]; then # Was it entered at this prompt?
                comm="$new";
            fi
        else
            if [ $(( $valid & 1 )) == 0 ]; then
                echo "Error: Invalid command.";
                prompt;
                return;
            fi
        fi
        
        if [ $(( $valid & 2 )) != 0 ]; then
            gnu;
        fi
        
        if [ $(( $valid & 4)) != 0 ]; then
            val;
        fi
        
        if [ $(( $valid & 8)) != 0 ]; then
            run;
        fi
    }
    
    cd ~/NetBeansProjects/Outliner;
    prompt;
    exit;
    CLI.Makefile
    Code:
    # Makefile
    CC = gcc
    CFLAGS = -ggdb -ansi -lm
    OBJECTS = main.o interface.o outline.o ../Jesdisciple/filesystem.o ../Jesdisciple/list.o ../Jesdisciple/toolkit.o
    
    outliner: $(OBJECTS)
    	$(CC) `pkg-config --cflags --libs gtk+-2.0` -o $@ $^ $(CFLAGS) -D INTERFACE=0
    
    main.o: main.c interface.h
    
    interface.o: interface.c interface.h
    interface.h: outline.h
    
    outline.o: outline.c outline.h
    outline.h: ../Jesdisciple/filesystem.h
    
    include ../Jesdisciple/Makefile
    main.c
    Code:
    /* 
     * File:   main.c
     * Author: Jesdisciple
     *
     * Created on December 23, 2008, 3:00 PM
     */
    
    #ifdef	__cplusplus
    extern "C" {
    #endif
    
    #include "../Jesdisciple/toolkit.h"
    #include "outline.h"
    #include "interface.h"
    #include <stdlib.h>
    
    int main(int argc, char **argv){
    #if INTERFACE == GRAPHICAL
    	gtk_init(&argc, &argv);
    #endif
    	
    	struct outline_interface face = {
    		(struct outline *)NULL,
    
    		die,
    		error,
    		alert,
    		prompt,
    		menu,
    		confirm,
    		
    		help,
    		start,
    		make,
    		graft,
    		show,
    		open,
    		save,
    		load,
    		(void (*)(struct outline_interface *))NULL
    	};
    	
    	outline_init(&face);
    
    #if INTERFACE == GRAPHICAL
    	gtk_main();
    #endif
        
    	free(outline_home);
    	return 0;
    }
    
    #ifdef __cplusplus
    }
    #endif
    Thanks!!!

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    (bump) Should I post another C file?

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    Update: To help diagnose the problem, I changed my GTK if's to always evaluate as 0. The problem went away, but now I can't use a GUI...

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    I found the bugs, and can't believe how simple they were... Neither INTERFACE nor GRAPHICAL was actually defined, so they were always equal. I thought I had defined GRAPHICAL in outline.h, but it wasn't there. I only defined INTERFACE when all the object files were combined, which didn't do any good. So I changed the definition of CC in the makefiles to define INTERFACE in every source file.

    Thanks!

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    Further update: I just fixed yet another makefile bug. I kept getting undeclared errors about GTK+ identifiers; the pkgconfig segment needs to be included for all compilations, so I also moved it to the definition of CC

    Thanks again!
    Last edited by Jesdisciple; 04-09-2009 at 06:26 PM.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You are very welcome! Glad to have helped!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-22-2008, 04:49 AM
  2. Compiling Issues
    By pc_doctor in forum C Programming
    Replies: 3
    Last Post: 11-30-2007, 10:00 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. QT or GTK
    By mart_man00 in forum Linux Programming
    Replies: 4
    Last Post: 03-24-2003, 10:42 PM
  5. Compiling in Unix vs Visual C++
    By stimpyzu in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 06:41 AM