e06b Problem compiling an Xlib program. - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Development > Userland Programming & Scripting

Userland Programming & Scripting C, Shell, Perl, Sed & Awk

Reply
 
Thread Tools Display Modes
  #1  
Old August 7th, 2012, 22:49
neilms's Avatar
neilms neilms is offline
Member
 
Join Date: Jun 2010
Location: Cydonia
Posts: 123
Thanks: 17
Thanked 2 Times in 2 Posts
Default Problem compiling an Xlib program.

Hello, I am attempting to compile a simple C++ program that produces an Xlib window. Here is the program which is from a tutorial:

Code:
#include <X11/Xlib.h> // Every Xlib program must include this
#include <assert.h>   // I include this to test return values the lazy way
#include <unistd.h>   // So we got the profile for 10 seconds

#define NIL (0)       // A name for the void pointer

main()
{
      // Open the display

      Display *dpy = XOpenDisplay(NIL);
      assert(dpy);

      // Get some colors

      int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
      int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));

      // Create the window

      Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 
				     200, 100, 0, blackColor, blackColor);

      // We want to get MapNotify events

      XSelectInput(dpy, w, StructureNotifyMask);

      // "Map" the window (that is, make it appear on the screen)

      XMapWindow(dpy, w);

      // Create a "Graphics Context"

      GC gc = XCreateGC(dpy, w, 0, NIL);

      // Tell the GC we draw using the white color

      XSetForeground(dpy, gc, whiteColor);

      // Wait for the MapNotify event

      for(;;) {
	    XEvent e;
	    XNextEvent(dpy, &e);
	    if (e.type == MapNotify)
		  break;
      }

      // Draw the line
      
      XDrawLine(dpy, w, gc, 10, 60, 180, 20);

      // Send the "DrawLine" request to the server

      XFlush(dpy);

      // Wait for 10 seconds

      sleep(10);
}
When I invoke the compilation commands, I get the following errors:
Code:
wind.cpp:1:21: error: X11/Xlib.h: No such file or directory
wind.cpp: In function 'int main()':
wind.cpp:8: error: 'Display' was not declared in this scope
wind.cpp:8: error: 'dpy' was not declared in this scope
wind.cpp:8: error: 'XOpenDisplay' was not declared in this scope
wind.cpp:11: error: 'DefaultScreen' was not declared in this scope
wind.cpp:11: error: 'BlackPixel' was not declared in this scope
wind.cpp:12: error: 'WhitePixel' was not declared in this scope
wind.cpp:15: error: 'Window' was not declared in this scope
wind.cpp:15: error: expected `;' before 'w'
wind.cpp:18: error: 'w' was not declared in this scope
wind.cpp:18: error: 'StructureNotifyMask' was not declared in this scope
wind.cpp:18: error: 'XSelectInput' was not declared in this scope
wind.cpp:21: error: 'XMapWindow' was not declared in this scope
wind.cpp:24: error: 'GC' was not declared in this scope
wind.cpp:24: error: expected `;' before 'gc'
wind.cpp:25: error: 'gc' was not declared in this scope
wind.cpp:25: error: 'XSetForeground' was not declared in this scope
wind.cpp:29: error: 'XEvent' was not declared in this scope
wind.cpp:29: error: expected `;' before 'e'
wind.cpp:30: error: 'e' was not declared in this scope
wind.cpp:30: error: 'XNextEvent' was not declared in this scope
wind.cpp:31: error: 'MapNotify' was not declared in this scope
wind.cpp:35: error: 'XDrawLine' was not declared in this scope
wind.cpp:36: error: 'XFlush' was not declared in this scope
I have checked to ensure that libX11 is installed (it is) but cannot figure out why the compiler and linker cannot see the library and headers, even when explicitly linked.

Any ideas would be most welcome.

Last edited by SirDice; August 8th, 2012 at 07:52. Reason: Read about formatting tags: http://forums.freebsd.org/misc.php?do=bbcode
Reply With Quote
  #2  
Old August 8th, 2012, 07:53
SirDice's Avatar
SirDice SirDice is online now
Moderator
 
Join Date: Nov 2008
Location: Rotterdam, Netherlands
Posts: 13,703
Thanks: 47
Thanked 2,022 Times in 1,861 Posts
Default

Show us the complete command you used to compile this.
__________________
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
Reply With Quote
  #3  
Old August 8th, 2012, 13:05
neilms's Avatar
neilms neilms is offline
Member
 
Join Date: Jun 2010
Location: Cydonia
Posts: 123
Thanks: 17
Thanked 2 Times in 2 Posts
Default

compile code:
$ g++ -o -window wind.cpp -lX11 -L/usr/X6R11/lib -I /usr/X6R11/include
Reply With Quote
  #4  
Old August 8th, 2012, 14:17
SirDice's Avatar
SirDice SirDice is online now
Moderator
 
Join Date: Nov 2008
Location: Rotterdam, Netherlands
Posts: 13,703
Thanks: 47
Thanked 2,022 Times in 1,861 Posts
Default

X6R11 doesn't exist. If anything it should be X11R6. But even that shouldn't exist anymore.

Use /usr/local/lib/ and /usr/local/include/.
__________________
Senior UNIX Engineer at Unix Support Nederland
Experience is something you don't get until just after you need it.
Reply With Quote
  #5  
Old August 8th, 2012, 18:01
neilms's Avatar
neilms neilms is offline
Member
 
Join Date: Jun 2010
Location: Cydonia
Posts: 123
Thanks: 17
Thanked 2 Times in 2 Posts
Default

Thanks, this compiles now. However,if I run the program from the console I get a segmentation fault (core dumped) error. Why is this?
Reply With Quote
  #6  
Old August 9th, 2012, 13:57
neilms's Avatar
neilms neilms is offline
Member
 
Join Date: Jun 2010
Location: Cydonia
Posts: 123
Thanks: 17
Thanked 2 Times in 2 Posts
Default

It's ok, I have solved my problem.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shared memory and Linux program problem cutter FreeBSD Development 3 March 25th, 2012 09:27
[Solved] Problem compiling php5-dba on 7.4 gregober Installation and Maintenance of FreeBSD Ports or Packages 2 February 13th, 2012 15:09
[Solved] problem with C program psycho Userland Programming & Scripting 3 May 6th, 2010 14:02
FreeBSD Program Compiling-Installing ligregni FreeBSD Development 2 March 8th, 2010 09:15
[Solved] Problem with a linux program using java (on FreeBSD) cell Installation and Maintenance of FreeBSD Ports or Packages 10 July 25th, 2009 20:12


All times are GMT +1. The time now is 10:34.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0