LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   strange header problem (https://www.linuxquestions.org/questions/programming-9/strange-header-problem-228356/)

irfanhab 09-08-2004 07:30 PM

strange header problem
 
Hi,


I've got a strange header file problem, because of which i can not compile my program.

Look the concerned files looks like this:

the xkeylock..cpp file

Code:

#include "xkeylock.h"

extern "C"
{
  #include <X11/Xlib.h>
  #include <X11/keysym.h>
  #include <X11/extensions/XTest.h>      //PROBLEM IS HERE
}
     

XKeyLock::XKeyLock(Display *a_display, QObject *parent, const char *name)
  : QObject(parent, name), numlock_mask(0), capslock_mask(0), scrolllock_mask(0)
{
  display = a_display;

  KeyCode keycode;

  keycode = XKeysymToKeycode(display, XK_Num_Lock);
  if (keycode != NoSymbol)
  {
    numlock_mask = getModifierMapping(keycode);
    if (numlock_mask == 0) numlock_mask = setModifierMapping(keycode);
  }
   
  keycode = XKeysymToKeycode(display, XK_Caps_Lock);
  if (keycode != NoSymbol)
  {
    capslock_mask = getModifierMapping(keycode);
    if (capslock_mask == 0) capslock_mask = setModifierMapping(keycode);
  }
 
  keycode = XKeysymToKeycode(display, XK_Scroll_Lock);
  if (keycode != NoSymbol)
  {
    scrolllock_mask = getModifierMapping(keycode);
    if (scrolllock_mask == 0) scrolllock_mask = setModifierMapping(keycode);
  }
}

XKeyLock::~XKeyLock()
{
}

unsigned int XKeyLock::getModifierMapping(KeyCode keycode)
{
  unsigned int mask = 0;
 
  XModifierKeymap* map = XGetModifierMapping(display);
 
  for(int i = 0 ; i < 8; ++i)
    if (map->modifiermap[map->max_keypermod * i] == keycode)
      mask = 1 << i;
 
  XFreeModifiermap(map);
  return mask;
}

unsigned int XKeyLock::setModifierMapping(KeyCode keycode)
{
  unsigned int mask = 0;
  XModifierKeymap* map = XGetModifierMapping(display);
 
  for(int i = 0 ; i < 8; ++i)
    if (map->modifiermap[map->max_keypermod * i] == 0)
    {
      map->modifiermap[map->max_keypermod * i] = keycode;
      XSetModifierMapping(display, map);
      mask = 1 << i;
      break;
    }     

  XFreeModifiermap(map);
  return mask;
}


bool XKeyLock::isNumLockReadable()
{
  return (numlock_mask != 0);
}

bool XKeyLock::isCapsLockReadable()
{
  return (capslock_mask != 0);
}

bool XKeyLock::isScrollLockReadable()
{
  return (scrolllock_mask != 0);
}


bool XKeyLock::getNumLock()
{
  if (!XKeyLock::isNumLockReadable())
    return false;
  else
    return (bool) (getIndicatorStates() & numlock_mask);
}

bool XKeyLock::getCapsLock()
{
  if (!XKeyLock::isCapsLockReadable())
    return false;
  else
    return (bool) (getIndicatorStates() & capslock_mask);
}

bool XKeyLock::getScrollLock()
{
  if (!XKeyLock::isScrollLockReadable())
    return false;
  else
    return (bool) (getIndicatorStates() & scrolllock_mask);
}


void XKeyLock::toggleNumLock()
{
  XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_Num_Lock), true, CurrentTime);
  XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_Num_Lock), false, CurrentTime);
}

void XKeyLock::toggleCapsLock()
{
  XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_Caps_Lock), true, CurrentTime);
  XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_Caps_Lock), false, CurrentTime);
}

void XKeyLock::toggleScrollLock()
{
  XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_Scroll_Lock), true, CurrentTime);
  XTestFakeKeyEvent(display, XKeysymToKeycode(display, XK_Scroll_Lock), false, CurrentTime);
}


void XKeyLock::setNumLock(bool state)
{
  if (numlock_mask !=0 && getNumLock() != state)
    toggleNumLock();
}

void XKeyLock::setCapsLock(bool state)
{
  if (capslock_mask !=0 && getCapsLock() != state)
    toggleCapsLock();
}

void XKeyLock::setScrollLock(bool state)
{
  if (scrolllock_mask !=0 && getScrollLock() != state)
    toggleScrollLock();
}

unsigned int XKeyLock::getIndicatorStates()
{
  Window dummy1, dummy2;
  int dummy3, dummy4, dummy5, dummy6;
  unsigned int indicatorStates;

  XQueryPointer(display, DefaultRootWindow(display), &dummy1, &dummy2,&dummy3, &dummy4, &dummy5, &dummy6, &indicatorStates);
         
  return indicatorStates;
}


The problem is that when I compile I get this error:

Code:

g++ -Wl,-rpath,/usr/lib/qt/lib -o connectMonitor main.o xkeylock.o mainw.o moc_xkeylock.o moc_mainw.o  -L/usr/lib/qt/lib -L/usr/X11R6/lib -lqt-mt -lXext -lX11-lm
xkeylock.o(.text+0x547): In function `XKeyLock::toggleNumLock()':
: undefined reference to `XTestFakeKeyEvent'
xkeylock.o(.text+0x56a): In function `XKeyLock::toggleNumLock()':
: undefined reference to `XTestFakeKeyEvent'
xkeylock.o(.text+0x5a7): In function `XKeyLock::toggleCapsLock()':
: undefined reference to `XTestFakeKeyEvent'
xkeylock.o(.text+0x5ca): In function `XKeyLock::toggleCapsLock()':
: undefined reference to `XTestFakeKeyEvent'
xkeylock.o(.text+0x607): In function `XKeyLock::toggleScrollLock()':
: undefined reference to `XTestFakeKeyEvent'
xkeylock.o(.text+0x62a): more undefined references to `XTestFakeKeyEvent' follow
collect2: ld returned 1 exit status
make: *** [connectMonitor] Error 1

But as it turns out that `XTestFakeKeyEvent' is defined in X11/extensions/XTest.h, and this file is includede and the path is correct. So why is it acting as if I did not add the concerned header file?

aluser 09-08-2004 08:01 PM

ld is the linker; when it gives you "undefined reference" it means you forgot to link something, not that you forgot a header. Some grepping suggests the missing culprit is libXtst, whatever that is.

CroMagnon 09-08-2004 08:01 PM

Can you post the lines that define XTestFakeKeyEvent in the header file? It's not inside an #ifdef that might evaluate false?

CroMagnon 09-08-2004 08:03 PM

oops, aluser is right, silly me - don't paste that definition

irfanhab 09-09-2004 12:22 AM

Quote:

ld is the linker; when it gives you "undefined reference" it means you forgot to link something, not that you forgot a header
I 'copied' this file from KeybLED program, to control Keyboard LEDS, well, I checked for any external references but found that it was totally self-contained, so I copied it to my project and started using it until this happened.

Now the linker thing is a bit curious, because I compiled KeybLED too, and that compiled without any problems
the xkeylock.moc.lo file, which is for the linker is the exact same KeybLED has, but only in mine it gives an error?

Marius2 09-09-2004 09:05 AM

Quote:

Originally posted by irfanhab
I 'copied' this file from KeybLED program, to control Keyboard LEDS, well, I checked for any external references but found that it was totally self-contained, so I copied it to my project and started using it until this happened.

Now the linker thing is a bit curious, because I compiled KeybLED too, and that compiled without any problems
the xkeylock.moc.lo file, which is for the linker is the exact same KeybLED has, but only in mine it gives an error?

I think aluser is right, error message meaning that you missed to link a library. You have a binary of KeybLED? You can
get it's dependencies with ldd (ldd <KeybLED binary>), it's one of the files dumped then that you should add to the list of
object files in your makefile.

HTH

aluser 09-09-2004 12:57 PM

ooor you can just throw -lXtst at the end of your command line and see if that fixes it. lol

irfanhab 09-09-2004 10:11 PM

Quote:

ooor you can just throw -lXtst at the end of your command line and see if that fixes it. lol
What do you mean?
make -lXtst

CroMagnon 09-09-2004 10:57 PM

Add it to your Makefile (there's usually a variable called LIBS or something similar - yours will have qt-mt, Xext, and X11-lm already in it)

irfanhab 09-10-2004 12:29 AM

Quote:

Add it to your Makefile (there's usually a variable called LIBS or something similar - yours will have qt-mt, Xext, and X11-lm already in it)

IT COMPILED IT!!!!!
The trick was that I had to add
-lXtst to the MakeFile under the LIBS tag

Thanx Everyone

Just one question to aluser:
How did you know I had to add -lXtst?

aluser 09-10-2004 01:02 AM

I did a

grep -r XTestFakeKeyEvent /usr/X11R6/lib

Normally you'd check /usr/lib, but this looked like an X11 function.

irfanhab 09-10-2004 01:10 AM

Quote:

grep -r XTestFakeKeyEvent /usr/X11R6/lib
nice...


All times are GMT -5. The time now is 01:16 PM.