I'm trying to get a C++ program that uses XWindows to build on Bash on Ubuntu on Windows. I understand that Bash on Ubuntu on Windows does not officially support graphical Linux applications, but I would like the program to build regardless if possible. I have tried to reduce my issue as much as possible, hopefully I have provided enough information.
First, I installed the libx11-dev package and the packages associated with it using the command:
sudo apt-get install libx11-dev
Second, my Makefile is the following:
CXX = g++
CXXFLAGS = -Wall -MMD -lX11
EXEC = exe
OBJECTS = main.o window.o
DEPENDS = ${OBJECTS:.o=.d}
${EXEC}: ${OBJECTS}
${CXX} ${CXXFLAGS} ${OBJECTS} -o ${EXEC}
-include ${DEPENDS}
.PHONY: clean
clean:
rm ${EXEC} ${OBJECTS} ${DEPENDS}
Third, the window.h file is the following:
#ifndef __WINDOW_H__
#define __WINDOW_H__
#include <X11/Xlib.h>
class Xwindow {
Display *d;
public:
Xwindow();
};
#endif
Fourth the window.cc file is the following:
#include "window.h"
Xwindow::Xwindow() {
d = XOpenDisplay(NULL);
}
Finally the main.cc file is the following:
#include "window.h"
int main() {
Xwindow xw();
return 0;
}
When I run my Makefile I get the following output:
g++ -Wall -MMD -lX11 -c -o main.o main.cc
g++ -Wall -MMD -lX11 -c -o window.o window.cc
g++ -Wall -MMD -lX11 main.o window.o -o exe
window.o: In function `Xwindow::Xwindow()':
window.cc:(.text+0x12): undefined reference to `XOpenDisplay'
collect2: error: ld returned 1 exit status
Makefile:8: recipe for target 'exe' failed
make: *** [exe] Error 1
Any help is appreciated.
In some environments, including Ubuntu, you need to pass -l
arguments after the object files that use these libraries.
Also, -l
don't belong in CXXFLAGS
, they should be in LIBS
which goes after ${OBJECTS}
.