1

I am trying to compile a basic window with an OptionMenu Motif Widget.

This is what I tried:

#include <Xm/Xm.h>
#include <Xm/PushB.h>

XtAppContext    context;
XmStringCharSet char_set=XmSTRING_DEFAULT_CHARSET;

Widget          toplevel;
Widget          optionMenu;
Widget          optionMenuItem1;
Widget          optionMenuItem2;

Widget addMenuItem(char *itemName, XtPointer clientData, Widget menuWidget) {

        int ac;
        Arg al[10];
        Widget item;

        ac=0;
        XtSetArg(al[ac],XmNlabelString, XmStringCreateLtoR(itemName, char_set)); ac++;
        item=XmCreatePushButton(menuWidget,itemName,al,ac);
        XtManageChild(item);
        /*XtAddCallback(item,XmNactivateCallback,menuCB,clientData);*/
        XtSetSensitive(item,True);
        return(item);

}

int main(int argc, char *argv[]){

        Arg             al[10];
        int             ac;

        /* CREATE TOP LEVEL */
        toplevel = XtAppInitialize(&context, "test1", NULL, 0, &argc, argv, NULL, NULL, 0);

        /* RESIZE TOP LEVEL */
        ac=0;
        XtSetArg(al[ac], XmNheight, 300); ac++;
        XtSetArg(al[ac], XmNwidth, 300); ac++;
        XtSetValues(toplevel, al, ac);


        /* CREATE OPTIONS MENU */
        ac=0;
        optionMenu = XmCreateOptionMenu(toplevel, "optionMenu", al, ac);
        XtManageChild(optionMenu);
        optionMenuItem1 = addMenuItem("item1", "Item 1", optionMenu);
        optionMenuItem2 = addMenuItem("item2", "Item 2", optionMenu);


        /* DISPLAY TOP LEVEL */
        XtRealizeWidget(toplevel);
        XtAppMainLoop(context);
}

This is the compilation warning I get and I do not know why it is giving it:

$ cc -std=c89 test1.c -I/usr/local/include -L/usr/local/lib -lXm -lXt -lX11
test1.c:45:13: warning: incompatible integer to pointer conversion assigning to 'Widget' (aka 'struct _WidgetRec *') from 'int' [-Wint-conversion]
        optionMenu = XmCreateOptionMenu(toplevel, "optionMenu", al, ac);
                   ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.

And it generates a core dump when I try to execute it:

$ ./a.out
Segmentation fault (core dumped)

As I always compile things with no warnings, I want to understand why that warning appears and get rid of it first.

Edit:

As mentioned in the comments, adding <Xm/RowColumn.h>, which is specified in the man page of xmcreateoptionmenu fixes the issue.

Additionally, code to generate the option menu is wrong, I attach the right one. Note that there is no label assigned to the widget:

#include <Xm/Xm.h>
#include <Xm/PushB.h>
#include <Xm/RowColumn.h>

XtAppContext    context;
XmStringCharSet char_set=XmSTRING_DEFAULT_CHARSET;

Widget          toplevel;
Widget          pulldownMenu, optionMenu;
Widget          optionMenuItem1;
Widget          optionMenuItem2;

Widget addMenuItem(char *itemName, XtPointer clientData, Widget menuWidget) {

        int ac;
        Arg al[10];
        Widget item;

        ac=0;
        XtSetArg(al[ac],XmNlabelString, XmStringCreateLtoR(itemName, char_set)); ac++;
        item=XmCreatePushButton(menuWidget,itemName,al,ac);
        XtManageChild(item);
        /*XtAddCallback(item,XmNactivateCallback,menuCB,clientData);*/
        XtSetSensitive(item,True);
        return(item);

}

int main(int argc, char *argv[]){

        Arg             al[10];
        int             ac;

        /* CREATE TOP LEVEL */
        toplevel = XtAppInitialize(&context, "test1", NULL, 0, &argc, argv, NULL, NULL, 0);

        /* RESIZE TOP LEVEL */
        ac=0;
        XtSetArg(al[ac], XmNheight, 300); ac++;
        XtSetArg(al[ac], XmNwidth, 300); ac++;
        XtSetValues(toplevel, al, ac);


        /* CREATE OPTIONS MENU */
        pulldownMenu=XmCreatePulldownMenu(toplevel, "pulldownMenu",NULL,0);
        optionMenuItem1 = addMenuItem("item1", "Item 1", pulldownMenu);
        optionMenuItem2 = addMenuItem("item2", "Item 2", pulldownMenu);
        ac=0;
        XtSetArg(al[ac], XmNsubMenuId, pulldownMenu); ac++;
        optionMenu = XmCreateOptionMenu(toplevel, "optionMenu", al, ac);
        XtManageChild(optionMenu);


        /* DISPLAY TOP LEVEL */
        XtRealizeWidget(toplevel);
        XtAppMainLoop(context);
}
M.E.
  • 4,955
  • 4
  • 49
  • 128
  • 2
    For the warning try adding `#include ` as specified by the [man page](https://linux.die.net/man/3/xmcreateoptionmenu). – kaylum Jan 17 '21 at 10:42
  • 1
    Add `-Wall -Wextra`. Notice that **C89** allows implicit function declarations and they're not an error! I am not sure **why'd** you even want to compile with -std=c89 unless there is some really poor code that relies on implicit function declarations *and* you can't be *rsed to fix it... Suggest -std=c99, c11, c17... – Antti Haapala -- Слава Україні Jan 17 '21 at 10:43
  • 1
    as per the answer since you didn't include the header, the C**89** compiler helpfully deduced that you want to call a function **implicitly** declared as `extern int XmCreateOptionMenu();`. And since the return types do not match, the C compiler thought an `int` was returned and tries to coerce the 32-bit int to a 64-bit? pointer. – Antti Haapala -- Слава Україні Jan 17 '21 at 10:48
  • Thanks for your comments and explanations, edited original question contains fixed code, both for compile and for the OptionMenu itself, which was wrongly coded. – M.E. Jan 17 '21 at 12:37

0 Answers0