 | |
03-23-2005
|
#1 (permalink)
| | Linux User
Join Date: Oct 2004 Location: Serbia&Montenegro
Posts: 281
| X program compilation Did anyone work with programs in C that use X? I would really appreciate it if you could tell me how to compile this: Code: *
* simple-text.c - demonstrate drawing of text strings on a window. All
* drawings are done in black color over a white background.
*/
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h> /* getenv(), etc. */
#include <unistd.h> /* sleep(), etc. */
/*
* function: create_simple_window. Creates a window with a white background
* in the given size.
* input: display, size of the window (in pixels), and location of the window
* (in pixels).
* output: the window's ID.
* notes: window is created with a black border, 2 pixels wide.
* the window is automatically mapped after its creation.
*/
Window
create_simple_window(Display* display, int width, int height, int x, int y)
{
int screen_num = DefaultScreen(display);
int win_border_width = 2;
Window win;
/* create a simple window, as a direct child of the screen's */
/* root window. Use the screen's black and white colors as */
/* the foreground and background colors of the window, */
/* respectively. Place the new window's top-left corner at */
/* the given 'x,y' coordinates. */
win = XCreateSimpleWindow(display, RootWindow(display, screen_num),
x, y, width, height, win_border_width,
BlackPixel(display, screen_num),
WhitePixel(display, screen_num));
/* make the window actually appear on the screen. */
XMapWindow(display, win);
/* flush all pending requests to the X server. */
XFlush(display);
return win;
}
GC
create_gc(Display* display, Window win, int reverse_video)
{
GC gc; /* handle of newly created GC. */
unsigned long valuemask = 0; /* which values in 'values' to */
/* check when creating the GC. */
XGCValues values; /* initial values for the GC. */
unsigned int line_width = 2; /* line width for the GC. */
int line_style = LineSolid; /* style for lines drawing and */
int cap_style = CapButt; /* style of the line's edje and */
int join_style = JoinBevel; /* joined lines. */
int screen_num = DefaultScreen(display);
gc = XCreateGC(display, win, valuemask, &values);
if (gc < 0) {
fprintf(stderr, "XCreateGC: \n");
}
/* allocate foreground and background colors for this GC. */
if (reverse_video) {
XSetForeground(display, gc, WhitePixel(display, screen_num));
XSetBackground(display, gc, BlackPixel(display, screen_num));
}
else {
XSetForeground(display, gc, BlackPixel(display, screen_num));
XSetBackground(display, gc, WhitePixel(display, screen_num));
}
/* define the style of lines that will be drawn using this GC. */
XSetLineAttributes(display, gc,
line_width, line_style, cap_style, join_style);
/* define the fill style for the GC. to be 'solid filling'. */
XSetFillStyle(display, gc, FillSolid);
return gc;
}
void
main(int argc, char* argv[])
{
Display* display; /* pointer to X Display structure. */
int screen_num; /* number of screen to place the window on. */
Window win; /* pointer to the newly created window. */
unsigned int display_width,
display_height; /* height and width of the X display. */
unsigned int win_width,
win_height; /* height and width for the new window. */
char *display_name = getenv("DISPLAY"); /* address of the X display. */
GC gc; /* GC (graphics context) used for drawing */
/* in our window. */
XFontStruct* font_info; /* Font structure, used for drawing text. */
char* font_name = "*-helvetica-*-12-*"; /* font to use for drawing text. */
/* open connection with the X server. */
display = XOpenDisplay(display_name);
if (display == NULL) {
fprintf(stderr, "%s: cannot connect to X server '%s'\n",
argv[0], display_name);
exit(1);
}
/* get the geometry of the default screen for our display. */
screen_num = DefaultScreen(display);
display_width = DisplayWidth(display, screen_num);
display_height = DisplayHeight(display, screen_num);
/* make the new window occupy 1/9 of the screen's size. */
win_width = (display_width / 3);
win_height = (display_height / 3);
printf("window width - '%d'; height - '%d'\n", win_width, win_height);
/* create a simple window, as a direct child of the screen's */
/* root window. Use the screen's white color as the background */
/* color of the window. Place the new window's top-left corner */
/* at the given 'x,y' coordinates. */
win = create_simple_window(display, win_width, win_height, 0, 0);
/* allocate a new GC (graphics context) for drawing in the window. */
gc = create_gc(display, win, 0);
XSync(display, False);
/* try to load the given font. */
font_info = XLoadQueryFont(display, font_name);
if (!font_info) {
fprintf(stderr, "XLoadQueryFont: failed loading font '%s'\n", font_name);
exit(-1);
}
/* assign the given font to our GC. */
XSetFont(display, gc, font_info->fid);
{
/* variables used for drawing the text strings. */
int x, y;
char* text_string;
int string_width;
int font_height;
/* find the height of the characters drawn using this font. */
font_height = font_info->ascent + font_info->descent;
/* draw a "hello world" string on the top-left side of our window. */
text_string = "hello world";
x = 0;
y = font_height;
XDrawString(display, win, gc, x, y, text_string, strlen(text_string));
/* draw a "middle of the road" string in the middle of our window. */
text_string = "middle of the road";
/* find the width, in pixels, of the text that will be drawn using */
/* the given font. */
string_width = XTextWidth(font_info, text_string, strlen(text_string));
x = (win_width - string_width) / 2;
y = (win_height + font_height) / 2;
XDrawString(display, win, gc, x, y, text_string, strlen(text_string));
}
/* flush all pending requests to the X server. */
XFlush(display);
/* make a delay for a short period. */
sleep(4);
/* close the connection to the X server. */
XCloseDisplay(display);
}
__________________
Linux registered user #358842
Human knowledge belongs to the world.
|
| |
03-23-2005
|
#2 (permalink)
| | Linux Enthusiast
Join Date: Jan 2005
Posts: 575
| I think cc <file> -lX11
By the way on line 56 I get a warning for the gc < 0
part. |
| |
03-23-2005
|
#3 (permalink)
| | Linux User
Join Date: Jul 2004 Location: Poland
Posts: 368
| This is better Code: gcc -Wall -o simple-text simple-text.c -lX11 -L/usr/X11R6/lib
__________________
"I don't know what I'm running from
And I don't know where I'm running to
There's something deep and strange inside of me I see"
|
| |
03-23-2005
|
#4 (permalink)
| | Linux User
Join Date: Oct 2004 Location: Serbia&Montenegro
Posts: 281
| I tried but it didn't work. It gives a bunch of mistakes about size_t in library.Do you know some other way?
Anyway, thanks.
__________________
Linux registered user #358842
Human knowledge belongs to the world.
|
| |
03-23-2005
|
#5 (permalink)
| | Linux Enthusiast
Join Date: Jan 2005
Posts: 575
| Try putting #include <malloc.h> along with the other includes. |
| |
03-24-2005
|
#6 (permalink)
| | Linux User
Join Date: Jul 2004 Location: Poland
Posts: 368
| Quote: |
Originally Posted by Fallen Angel I tried but it didn't work. It gives a bunch of mistakes about size_t in library.Do you know some other way?
Anyway, thanks. | Do you have a complete environment? I was able to compile you program without any mistakes after removing the incorrect comment at the top, adding #include <string.h> and changing main's return type from void to int. I'm using gcc 3.3.5. It could be helpful if you pasted compiler output so that we could see what exactly went wrong.
__________________
"I don't know what I'm running from
And I don't know where I'm running to
There's something deep and strange inside of me I see"
|
| |
03-24-2005
|
#7 (permalink)
| | Linux User
Join Date: Oct 2004 Location: Serbia&Montenegro
Posts: 281
| I changed things you said kyku and there are less mistakes. Here is what I get now: Code: /usr/lib/gcc-lib/i486-slackware-linux/3.3.4/../../../../i486-slackware-linux/bin/ld: cannot find -lX11
collect2: ld returned 1 exit status
__________________
Linux registered user #358842
Human knowledge belongs to the world.
|
| |
03-24-2005
|
#8 (permalink)
| | Linux User
Join Date: Jul 2004 Location: Poland
Posts: 368
| This should vanish if you add -L/usr/X11R6/lib flag to commandline as I suggested in my previous post.
__________________
"I don't know what I'm running from
And I don't know where I'm running to
There's something deep and strange inside of me I see"
|
| |
03-25-2005
|
#9 (permalink)
| | Linux User
Join Date: Oct 2004 Location: Serbia&Montenegro
Posts: 281
| Thanks kyku, it worked now.
__________________
Linux registered user #358842
Human knowledge belongs to the world.
|
| |
08-20-2005
|
#10 (permalink)
| | Just Joined!
Join Date: Aug 2005 Location: Slovenia
Posts: 18
| Quote: |
Originally Posted by kyku This is better Code: gcc -Wall -o simple-text simple-text.c -lX11 -L/usr/X11R6/lib
| That suggestion solved a bunch of 'undefined reference' errors. But there is still one left: Code: [simon@localhost test]$ gcc -o test hello1.c -lX11 -L/usr/X11R6/lib
/home/simon/tmp/ccaKofH8.o(.text+0x2ad): In function `main':
: undefined reference to `sin'
collect2: ld returned 1 exit status
[simon@localhost test]$
Any idea how to solve this one? And is there a way to somehow include those switches automatically so I don't have to type them every time? |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | Free Magazines | Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business. subscribe | Systems Management News, the newspaper for IT systems administration and data center managers!
Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field. subscribe | The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business. subscribe | Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company. subscribe | Total Telecom Total Telecom is "The Economist of the communications industry". subscribe | | More free magazines » | All times are GMT. The time now is 03:22 AM. |
| |