1

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.

  • 4
    The order of libraries on the command line when you build is important. Please edit your question to show the actual commands you use to build (compile and *especially* link) your program. – Some programmer dude May 02 '18 at 16:16
  • 3
    I guess that your compilation is successful, but your linking step is failing. Show it in great details. Actually, provide some [MCVE]. And your title is misleading. It seems that you are cross-compiling on Windows (in some [WSL](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux) environment) a Linux application using `libX11`. Apparently you are *not* compiling on a genuine Linux distribution. So **edit your question** to improve it a lot and give the *exact* commands you are trying, and the actual C++ source code involved – Basile Starynkevitch May 02 '18 at 16:17
  • What do you get if you type `ls /usr/lib/libX11*`? – Galik May 02 '18 at 16:57
  • Galik, I just saw your comment after my edits. I get `ls: cannot access '/usr/lib/libX11': No such file or directory`. I assume that X11 was not installed where g++ expected it to be. How would I resolve this issue? – kylejacobbecker May 02 '18 at 17:36
  • 1
    @kylejacobbecker no, you've missed the `*` – wRAR May 02 '18 at 17:37
  • @wRAR I got the same output either way. My issue has been resolved. Thank you for your time. – kylejacobbecker May 02 '18 at 17:44
  • Note: It is possible to run X11 clients on [tag:bash-on-windows] by running an X server on Windows (or a remote machine) and setting e.g. `DISPLAY=:0` on the unix side to point to that X server. See: https://www.pcworld.com/article/3055403/windows/windows-10s-bash-shell-can-run-graphical-linux-applications-with-this-trick.html – David Conrad May 02 '18 at 18:08

1 Answers1

2

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}.

wRAR
  • 25,009
  • 4
  • 84
  • 97
  • 1
    Thank you! That worked. I wrote the project on my University's environment and my Makefile worked just fine. I didn't anticipate the Makefile being incorrect on my home machine. – kylejacobbecker May 02 '18 at 17:40