[comp.windows.x] Linking Libraries/Undefined variables

jstravis@athena.mit.edu (John S. Travis) (08/15/89)

Once again I cry for help:
1)why does the order of linking libraries affect stuff? I changed the places
of Xw and Xt on young's oneline textwidget and got this. (compile calls
the gnu cc).

w20-575-59% compile oneline.c -lXt -lXw -lX11 -g
Undefined:
_XtDestroyGC
_XtGetGC
_tempnam
w20-575-59% compile oneline.c -lXw -lXt -lX11 -g
Undefined:
_tempnam

	 And why on this short program do I have trouble, whereas
when i compile the larger draw(modified) program of his I can -lXt -lXw -X11
and not have XtGetGC & XtDestroyGC undefined??(I actually use them in
the draw program, but not in oneline.c)
---------------------------------------------

2)What the heck is tempnam????!!! I have fgrep almost every file I can
think of for it and it is NOWHERE! I'll include the code below. But,
I'm going crazy trying to eliminate this error. (Probably a real stupid
mistake...but TELL me I can take it.Thanks)

john travis
jstravis@athena.mit.edu

---------------------------code cut here---------------------------
/*************************************************
* oneline.c : Create a single line editable field
*************************************************/
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <Xw/Xw.h>
#include <Xw/TextEdit.h>

#define FONTHEIGHT(f)  ((f)->max_bounds.ascent + \
                        (f)->max_bounds.descent)

Widget create_one_line_text_widget();


main(argc,argv)
     int argc;
     char *argv[];
{
  Widget toplevel;
  Arg  wargs[1];

  toplevel = XtInitialize(argv[0],"Edit",NULL,0,&argc,argv);

  XtSetArg(wargs[0],XtNeditType,XwtextEdit);
  create_one_line_text_widget("edit",toplevel,wargs,1);

  XtRealizeWidget(toplevel);
  XtMainLoop();
}

/*
* just ring the terminal bell
*/
static void beep(w,event,params, num_params)
     Widget w;
     XEvent *event;
     String *params;
     int num_params;
{
  XBell(XtDisplay(w),100);
}
/*
* associate the action beep with the function
*/
static XtActionsRec actionsTable [] ={
  {"beep",beep},
};
/*
*override all translations that enter a newline.
*/
static char defaultTranslations[] =
  "Ctrl<Key<J:     beep() \n\
   Ctrl<Key>O:     beep() \n\
   Ctrl<Key>M:     beep() \n\
   <Key> Return:   beep()";

Widget create_one_line_text_widget(name,parent,args, nargs)
     char   *name;
     Widget parent;
     Arg args[];
     int nargs;
{
  XFontStruct *font;
  Widget  w;
  Arg wargs[1];
  XtTranslations  trans_table;
  /*
   * Add the actions and compile the translations
   */
  XtAddActions(actionsTable,XtNumber(actionsTable));
  trans_table = XtParseTranslationTable(defaultTranslations);
  /*
   * Create a textEdit widget
   */
  w = XtCreateManagedWidget(name, XwtexteditWidgetClass,parent,args,nargs);
  /*
   * install our tranlstions
   */
  XtOverrideTranslations(w,trans_table);
  /*
   *get the font used by the widget
   */
  XtSetArg(wargs[0],XtNfont,&font);
  XtGetValues(w,wargs,1);
  /*
   * set the widget height according to the font height
   */
  XtSetArg(wargs[0],XtNheight,FONTHEIGHT(font) + 6);
  XtSetValues(w,wargs,1);

return(w);

}

jstravis@athena.mit.edu (John S. Travis) (08/15/89)

I wrote....
	w20-575-59% compile oneline.c -lXw -lXt -lX11 -g
	Undefined:
	_tempnam
	)What the heck is tempnam????!!! I have fgrep almost every file I can
	think of for it and it is NOWHERE! I'll include the code below. But,
	I'm going crazy trying to eliminate this error. (Probably a real stupid
	mistake...but TELL me I can take it.Thanks)

Well, the search narrows. Tmpnam or Tempnam supposedly a program in
the C lib, that is called by the HP widgets. However, tmpnam is not 
in OUR C lib at MIT as far as I can tell. Is is a common function on
most systems. And would someone send the code for it so I can include
it in my program, sincle C lin doesn't have it? Thanks in advance.

john travis
jstravvis@athena.mit.edu

pratap@hpcll01.HP.COM (Pratap Subrahmanyam) (08/16/89)

You are correct. Tempnam/Tmpnam is a routine in HP-UX C library that lets
you create temporary files. It is strange that HP Widgets would use a routine
that is not standardly available ... 

-- pratap.

mlm@odi.com (Mitchell Model) (08/16/89)

In article <13489@bloom-beacon.MIT.EDU> jstravis@athena.mit.edu (John S. Travis) writes:

   1)why does the order of linking libraries affect stuff? I changed the places
   of Xw and Xt on young's oneline textwidget and got this. (compile calls
   the gnu cc).

My theory is the following:

Since widget libraries are based on Xt, they make calls on functions
in libXt, and functions in libXt don't make calls to them.  With the
libraries specified in the wrong order, after the loader finishes
getting what it needs from libXt, it then encounters calls to libXt in
the widget library that it didn't know about when it processed libXt.
The only reason the larger program happened to work is that it itself
called all the Xt functions that the selected pieces of the widget
library called; larger programs tend to call a wider variety of
functions!

The same reasoning tells you to put -lX11 after -lXmu and -lXmu after
-lXt.

	Mitchell L Model
	Director, HeadStart Program
	Object-Design, Inc.
	One New England Park
	Burlington MA 01803
	(617) 270-9797
	mlm@odi.com


-- 

	Mitchell L Model
	Director, HeadStart Program
	Object-Design, Inc.

klee@gilroy.pa.dec.com (Ken Lee) (08/17/89)

In article <3930009@hpcll01.HP.COM>, pratap@hpcll01.HP.COM (Pratap Subrahmanyam) writes:
> You are correct. Tempnam/Tmpnam is a routine in HP-UX C library that lets
> you create temporary files. It is strange that HP Widgets would use a routine
> that is not standardly available ... 

For what it's worth, DEC Ultrix includes both tempnam and tmpnam.  I
don't have my old manuals with me, but I think one is primarily found
in AT&T UNIXs and the other in BSD UNIXs.  Only hybrid UNIXs will have
both.

Ken Lee
DEC Western Software Laboratory, Palo Alto, Calif.
Internet: klee@decwrl.dec.com
uucp: uunet!decwrl!klee

guy@auspex.auspex.com (Guy Harris) (08/18/89)

 >> You are correct. Tempnam/Tmpnam is a routine in HP-UX C library that lets
 >> you create temporary files. It is strange that HP Widgets would use a routine
 >> that is not standardly available ... 
 >
 >For what it's worth, DEC Ultrix includes both tempnam and tmpnam.  I
 >don't have my old manuals with me, but I think one is primarily found
 >in AT&T UNIXs and the other in BSD UNIXs.  Only hybrid UNIXs will have
 >both.

BSD has "tmpnam", and S5 has "tempnam" and "tmpnam".  I don't know
which, if any, of them were in the AT&T UNIXes from which both BSD and
S5 were derived....

"tmpnam" is in POSIX, and "tempnam" isn't.  Given that, it would
probably be a good idea to avoid using "tempnam" if you possibly can,
since it's less likely to be available in any particular system.

jstravis@athena.mit.edu (John S. Travis) (08/18/89)

In article <2369@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes:
>
- >> You are correct. Tempnam/Tmpnam is a routine in HP-UX C library that lets
- >> you create temporary files. It is strange that HP Widgets would use a routine
- >> that is not standardly available ... 
-
-BSD has "tmpnam", and S5 has "tempnam" and "tmpnam".  I don't know
-which, if any, of them were in the AT&T UNIXes from which both BSD and
-S5 were derived....
-
-"tmpnam" is in POSIX, and "tempnam" isn't.  Given that, it would
-probably be a good idea to avoid using "tempnam" if you possibly can,
-since it's less likely to be available in any particular system.

The original problem is that the HPWidgets call the function, not the
application programmer. Although, i just wrote a makeshift function
of the same name, and it seems to do the job, i'm surprised HP
did this.

john travis

john@acorn.co.uk (John Bowler) (08/18/89)

In article <1687@bacchus.dec.com> klee@gilroy.pa.dec.com (Ken Lee) writes:
>In article <3930009@hpcll01.HP.COM>, pratap@hpcll01.HP.COM (Pratap Subrahmanyam) writes:
>> You are correct. Tempnam/Tmpnam is a routine in HP-UX C library that lets
>> you create temporary files. ...
>
> [comment that DEC Ultrix includes both tempnam and tmpnam]

tempnam is the System V interface, defined in both the SVID and the POSIX P1003
stuff.  The current POSIX definition says (approximately):-

    char *tempnam(char *dir, char *pfx)

    generates a pathname that may be used for a temporary file.

    The dir argument points to the name of the directory in which the
    file is to be created.  If dir is NULL or points to a string which
    is not the name of an appropriate directory the P_tmpdir path
    prefix from <stdio.h> is used.  The pfx argument may be NULL or point
    to a string of up to file characters to be used as the first few
    characters of the filename.

Raw BSD systems do not contain this function.  SUNOS systems contain System
V compatibility libraries which have an appropriate definition.  Aegis
(Apollo) systems used to have both BSD and SysV support, so shouldn't cause
problems either.  The following code fragment might be sufficient (caveat
emptor - in particular the SVID is more definitive about the behaviour if
the relevant directories do not exist, this code assumes the caller has
given an accessible directory).

John Bowler (jbowler@acorn.co.uk)

-------------------- cut here -----------------------------------

extern char *mktemp(/*-char *template-*/);	/* BSD routine */

char *tempnam(dir, pfx)
	char *dir, *pfx;
{
	int len, lend;
	char *result, *rtemp;

	if (!dir) dir = "/tmp";	/* Should be P_tmpdir... */
	if (!pfx) pfx = "zz";	/* or anything with no X's in it */

	lend = strlen(dir);
	if (dir[lend-1] != '/') ++lend;
	len = strlen(pfx);

	result = (char *)malloc(lend+len+7);
	strcpy(result, dir);
	/* Should test directory access here */
	if (!result[lend-1]) result[lend-1] = '/';
	strcat(result, pfx);
	strcat(result, "XXXXXX");
	/*
	 * NB - the following runes are probably BSD 4.3 specific -
	 * in 4.3 mktemp returns "/" on error (!!!).  If the directory
	 * does not exist mktemp seems to succeed, although the
	 * name is invalid.
	 */
	rtemp = mktemp(result);
	if (rtemp == result) return result;
	free(result);
	/* Assume errno is set by mktemp... */
	return 0;
}

taylor@THINK.COM (08/18/89)

<Date: 17 Aug 89 23:51:28 GMT
<From: jstravis@athena.mit.edu  (John S. Travis)

<In article <2369@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes:

<- >> You are correct. Tempnam/Tmpnam is a routine in HP-UX C library that lets
<- >> you create temporary files. It is strange that HP Widgets would use a routine
<- >> that is not standardly available ... 

<-"tmpnam" is in POSIX, and "tempnam" isn't.  Given that, it would
<-probably be a good idea to avoid using "tempnam" if you possibly can,
<-since it's less likely to be available in any particular system.
<
<The original problem is that the HPWidgets call the function, not the
<application programmer. Although, i just wrote a makeshift function
<of the same name, and it seems to do the job, i'm surprised HP
<did this.

tempnam *IS* available from Berkeley as well -- it's defined in the src
file lib/libc/compat-sys5/tmpnam.c which is available via anonymous ftp
from uunet.uu.net and probably elsewhere.

David
--
David Taylor
taylor@think.com, ...!think!taylor

guy@auspex.auspex.com (Guy Harris) (08/19/89)

>The original problem is that the HPWidgets call the function, not the
>application programmer.

Yup.  The "you" in my message referred to whoever would be tempted to
use "tempnam", which in this case was a programmer at HP who wrote that
part of the widget(s) in question.

>i'm surprised HP did this.

I presume they just tried it on HP-UX and, perhaps other OSes that have
both functions (Ultrix, SunOS, etc.).  If you want to make sure your
code runs on vanilla 4.xBSD, or systems derived therefrom that *haven't*
picked up large chunks of the S5 C library, you have to try it on those
systems....

I've seen people mention a book about writing portable software -
portable to various flavors of UNIX, anyway, I don't know about other
OSes - by some people from Rabbit Software; this might be a good
reference to sit by the desk of anybody trying to write portable
software, along with the POSIX book.  And, since one of the intents of X
is to let you write portable software to work under a window system....