delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2012/05/11/14:55:06

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
X-Recipient: djgpp AT delorie DOT com
From: John Wright <john AT wacontrols DOT com>
To: djgpp AT delorie DOT com
Subject: Fwd: Xlib example program compile error
Date: Fri, 11 May 2012 13:54:14 -0500
User-Agent: KMail/1.13.6 (Linux/2.6.38-15-generic; KDE/4.6.5; i686; ; )
MIME-Version: 1.0
Message-Id: <201205111354.14930.john@wacontrols.com>
X-Cloudmark-Analysis: v=1.1 cv=e4+nN7JFyidPqtjm7QRibqntUT6DPKhMpvIMSrLb7TA= c=1 sm=0 a=uxf98jMwF4QA:10 a=JE74YIbCa9EA:10 a=86DmMMQbovAA:10 a=KQjHTzX6FrYA:10 a=ioYcHhEKh_Hqt05efsMA:9 a=tuo-IHi4iBzSPaDeqIwA:7 a=CjuIK1q_8ugA:10 a=HpAAvcLHHh0Zw7uRqdWCyQ==:117
Reply-To: john AT wacontrols DOT com

--Boundary-00=_WBWrPM4HKQqJTS3
Content-Type: text/plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit



--Boundary-00=_WBWrPM4HKQqJTS3
Content-Type: message/rfc822;
  name="forwarded message"
Content-Transfer-Encoding: 7bit
Content-Description: John Wright <jwright63 AT suddenlink DOT net>: Xlib example program compile error
Content-Disposition: inline

From: John Wright <jwright63 AT suddenlink DOT net>
Reply-To: jwright63 AT suddenlink DOT net
To: djgpp AT delorie DOT com
Subject: Xlib example program compile error
Date: Fri, 11 May 2012 10:32:00 -0500
User-Agent: KMail/1.13.6 (Linux/2.6.38-15-generic; KDE/4.6.5; i686; ; )
MIME-Version: 1.0
Content-Type: Text/Plain;
  charset="us-ascii"
Content-Transfer-Encoding: 7bit
Message-Id: <201205111032 DOT 01520 DOT jwright63 AT suddenlink DOT net>


Here is the compiler output for  xctest.c as it is written in the book. The 
same errors show up for three other files that include "xwins.h" Last night I 
went through everything line by line and changed how things were included or 
defined and got it to compile, then it bombed out on "xwinutil.c", showing 
parse error before { 
				    }

just like below, there is no parse error before 'theFGpix'. All of these 
errors go away when I define 

typedef struct XWIN
.
.
.
back in xctest.c

I also explicetly added back some "ifndef" statements that should be picked in 
"xwins.h" and "xwinutil.c". 

Like....

XContext	xwin_context;


Anyway, something ain't right but I bet is is simple. I can't really explain 
all the details here because the compiler is going to give a different set of 
error messages depending on how I rearrange things. Even though I got xctest.c 
compiled, I'm sure there would be issues in linking. initapp.c and app_globb.c 
are fine. They are clean! They were created in the previous chapter. Of course, 
initapp.c sets up all the attributes and gory details of display.



gcc     -c -O   xctest.c
In file included from xctest.c:13:
xwins.h:45: storage class specified for parameter `xwin_context'
In file included from xctest.c:14:
app_glob.c:27: storage class specified for parameter `theDisplay'
app_glob.c:28: storage class specified for parameter `theGC'
app_glob.c:29: storage class specified for parameter `AppDone'
app_glob.c:30: storage class specified for parameter `theEvent'
app_glob.c:31: storage class specified for parameter `theFontStruct'
app_glob.c:32: storage class specified for parameter `theBGpix'
app_glob.c:33: parse error before `theFGpix'
xctest.c: In function `main':
xctest.c:50: `MainXWA' undeclared (first use this function)
xctest.c:50: (Each undeclared identifier is reported only once
xctest.c:50: for each function it appears in.)
xctest.c:57: `theFontStruct' undeclared (first use this function)
xctest.c:57: `quit_label' undeclared (first use this function)
xctest.c:62: `theFGpix' undeclared (first use this function)
xctest.c:62: `theBGpix' undeclared (first use this function)
xctest.c:62: `theMain' undeclared (first use this function)
xctest.c:63: warning: assignment makes pointer from integer without a cast
xctest.c:65: `flip_label' undeclared (first use this function)
xctest.c:67: `FlipButton' undeclared (first use this function)
xctest.c:75: `AppDone' undeclared (first use this function)
xctest.c:77: `theDisplay' undeclared (first use this function)
xctest.c:77: `theEvent' undeclared (first use this function)
xctest.c:89: `theGC' undeclared (first use this function)
xctest.c:90: `messages' undeclared (first use this function)
xctest.c:100: `xwin_context' undeclared (first use this function)
xctest.c:110: request for member `event_handler' in something not a structure 
or union
xctest.c:111: request for member `event_handler' in something not a structure 
or union
xctest.c: In function `quit_action':
xctest.c:137: `AppDone' undeclared (first use this function)
xctest.c: In function `flip_action':
xctest.c:157: `max_messages' undeclared (first use this function)
xctest.c:163: `theDisplay' undeclared (first use this function)
xctest.c:163: `theMain' undeclared (first use this function)
make: *** [xctest.o] Error 1

## Project Makefile for Chapter8, - X Window System Programming 
##
## DEFINES
##

RM = rm -f
CC = gcc

##EXEC= wintest

##
##      -g = Debugging info
##      -O = Optimize (cannot have -g then)
##      -m486 = Optimize for Intel 486 processor
##      -i686 = Optimize for Intel 486 processor
##

CFLAGS= -O 

##
##
## Compiler INCLUDES & LIBRARIES 
##

INCLUDE=        -I. -I/usr/X11R6/include

LIB=    -L/usr/X11R6/lib        # library directory path 
LIBS=   -lX11 -lmalloc          # link libraries 

##
##      Rule to create .o files from .c files

.c.o:
        $(RM) $@
        $(CC)   -c $(CFLAGS) $(INCLUDES) $*.c

## Targets and dependencies

all::   xctest

xctest.o:       xctest.c app_glob.c xwins.h  

initapp.o:      initapp.c app_glob.c

xwinutil.o:     xwinutil.c app_glob.c xwins.h

xbutton.o:      xbutton.c app_glob.c xwins.h

xctest:         xctest.o initapp.o xwinutil.o xbutton.o
        $(RM) $@
        $(CC) -o $@ $(CFLAGS) xctest.o initapp.o xwinutil.o xbutton.o $(LIB) 
$(LIBS)



**** I don't understand why "xwins.h" is included as a dependency in the 
makefile rule?




/*
 *      File: xwins.h
 *
 *      Defines data structures for windows
 *
 */

#ifndef XWINS_H
#define XWINS_H

#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>

typedef struct XWIN
{
        Window          xid;
        Window          parent;
        void            *data;
        int             (*event_handler)();
} XWIN; 

/*
 *      Function prototypes
 */

        void    xwin_init(void);

        XWIN    *MakeXButton(int x, int y, unsigned height, unsigned bdwidth,
                        unsigned long bdcolor, unsigned long bgcolor,
                        Window parent, char *text, int (*button_action)(), 
                        caddr_t action_data;

/*
 *      Global variable
 */

#ifdef  XWIN_UTIL

XContext        xwin_context;

#else

extern XContext         xwin_context;

#endif  /* #ifdef XWIN_UTIL     */
#endif  /* #ifndef XWINS_H      */




/*
 *      File: app_glob.c
 *
 *      Declare the common global variables for X applications.
 */

#ifndef APP_GLOB_C      /* To ensure that it is included once           */
#define APP_GLOB_C

#ifdef  DEF_APP_GLOB    /* Defined in the initapp.c file                */

Display         *theDisplay;    /* Connection to the X display          */
GC              theGC;          /* The graphics context for main        */
int             AppDone = 0;    /* A flag to indicate when done         */
XEvent          theEvent;       /* Structure for current event          */
XFontStruct     *theFontStruct; /* Info on the default font             */
unsigned long   theBGpix;       /* Background and foreground            */
unsigned long   theFGpix;       /* pixel values of the main window      */

char            *theAppName = " ";      /* The applications name        */
Window          theMain;                /* The applications main window */
XWindowAttributes       MainXWA;        /* Attributes of the main window */


#else

extern  Display         *theDisplay;
extern  GC              theGC;
extern  int             AppDone;
extern  XEvent          theEvent;
extern  XFontStruct     *theFontStruct;
extern  unsigned long   theBGpix,
                        theFGpix;
extern  char            *theAppName;
extern  Window          theMain;

extern  XWindowAttributes       MainXWA;

#endif  /* #ifdef       DEF_APP_GLOB            */
#endif  /* #ifndef      APP_GLOB_C              */






/*
 *      File: xwinutil.c
 *
 *      Utility routine for context-managed reusable windows.
 *
 */

#define XWIN_UTIL

#include "xwins.h" 

void    xwin_init(void)
{

/*
 *      Create a unique context to reference data associated
 *      with the windows.
 */

        xwin_context    = XUniqueContext();
}







/*
 *      File: xctest.c
 *
 *      Use Xlib Context management routines to save data
 *      associated with each window.
 *
 */

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "xwins.h"  
#include "app_glob.c"

char    *quit_label = "Quit";
char    *flip_label = "Next Message";

char    *messages[ ] =
{
        "Testing Xlib Context Manager",
        "You can create multiple buttons easily",
        "All buttons share the same code",
        "Each button has its own data"
};

int     max_messages = sizeof(messages) / sizeof(char *);
int     msgnum = 0;


/*
 *      Function prototypes.
 */

void    initapp(int, char **, int, int, unsigned, unsigned);
int     quit_action(caddr_t);
int     flip_action(caddr_t);

/*------------------------------End of page 198-------------------------------
*/

main(int argc, char **argv)
{
        Window  *QuitButton, *flipButton, *which_xwin;
        unsigned quit_width, quit_height, flip_width;

        xwin_init();

        initapp(argc, argv, 20, 20, 300, 100);

        printf("Assigned window geometry: %dx%d+%d+%d\n", MainXWA.width,
                        MainXWA.height, MainXWA.x, MainXWA.y);

/*
 *      Create the two buttons.
 */

        quit_width = XTextWidth(theFontStruct, quit_label, strlen(quit_label)) 
+ 4;
        quit_height = theFontStruct->max_bounds.ascent + 
                theFontStruct->max_bounds.descent + 4;

        QuitButton = MakeXButton(1, 1, quit_width, quit_height,
                        1, theFGpix, theBGpix, theMain,
                        quit_label, quit_action, NULL);

        flip_width = XTextWidth(theFontStruct, flip_label, strlen(flip_label)) + 
4;

        FlipButton = MakeXbutton(quit_width+4, 1, flip_width, quit_height, 1,
                        theFGpix, theBGpix, theMain, flip_label, flip_action, 
NULL);

/*
 *      Event handling loop.
 *
 */

        while (!AppDone)
        {
                XNextEvent(theDisplay, &theEvent);

/*
 *      Main window is handled here
 */

        if (theEvent.xany.window == theMain)
        {
                if (theEvent.type == Expose &&
                                theEvent.xexpose.count == 0)
                {
                        XClearWindow(theDisplay, theMain);
                        XDrawString(theDisplay, theMain, theGC,
                        50, 50, messages[msgnum],
                        strlen(messages[msgnum]));
                }
        }

/*
 *      For all other windows, retrieve the XWIN data from Xlib
 *      using the context manager routine XFindContext.
 */

        if (XFindContext(theDisplay, theEvent.xany.window, xwin_context,
                                (caddr_t *) &which_xwin) == 0)
                {

/*-----------------------------------End of page 
199---------------------------*/

/*
 *      Call the event handler of this XWIN structure.
 */

                        if (*(which_xwin->event_handler) != NULL)
                        (*(which_xwin->event_handler))(which_xwin);
                }

        }

/*
 *      Close connection to display and exit.
 */

        XCloseDisplay(theDisplay);
        exit(0);
}

/*-----------------------------------------------------------------------------
*/

/*
 *              QUIT _ ACTION
 *
 *              This routine is called when a ButtonPress event occurs in the 
 *              quit window.
 */

int     quit_action(caddr_t data)
{
        /* Set the data done flag */

        AppDone = 1;
        return 1;
}

/*-----------------------------------------------------------------------------
*/

/*
 *      FLIP _ ACTION
 *
 *      This routine is called when a ButtPress event occurs the
 *      quit window.
 */

int     flip_action(caddr_t data)
{

/*
 *      Change the message to be displayed.
 */

        msgnum = (msgnum + 1) % max_messages;

/*
 *      Clear the main window, so that new message is displayed.
 */

        XClearArea(theDisplay, theMain, 0, 0, 0, 0, True);
        return 1;
}



--Boundary-00=_WBWrPM4HKQqJTS3--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019