2

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.

7
  • 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. Commented May 2, 2018 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 minimal reproducible example. And your title is misleading. It seems that you are cross-compiling on Windows (in some WSL 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 Commented May 2, 2018 at 16:17
  • What do you get if you type ls /usr/lib/libX11*? Commented May 2, 2018 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? Commented May 2, 2018 at 17:36
  • 1
    @kylejacobbecker no, you've missed the * Commented May 2, 2018 at 17:37

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.