6

I have written an SFML C++ game, and tried to start using threads, but after a while everything crashes. After searching I found out the fix seems to be to call XInitThreads(); but this does not work somehow.

simplified code:

    #include <X11/Xlib.h>   

    int main() {        
    XInitThreads();
    //other stuff
    return 1337;
    }

The error message I get when i try to compile is "undefined reference to symbol 'XInitThreads'. Could it be that the header file is working but there is no file where that method is implemented?

user3554800
  • 73
  • 1
  • 1
  • 5
  • most likely you didnt link properly – BЈовић May 10 '14 at 19:41
  • The include path is definately correct, because the error message changes when i remove the include line. But since the Xlib library is not created by me and used by a lot of people I would be surprised if the source file is not linked to the header file properly. I actually reinstalled Xlib to make sure that it is not broken. Which is why I am confused. – user3554800 May 10 '14 at 19:54

3 Answers3

11

"undefined reference to symbol" is a linker error, not a compiler error. If you get this message, the compiler has already finished compiled the file into an object file, but is unable to find the shared library which contains the function to link the object file into an executable.

If you're using gcc, it generally means you have to add some -l flags, like so:

$ gcc prog.c -lX11

note that the order of -lX11 in the compiler argument matters, you would get an error if you do this:

$ gcc -lX11 prog.c
/tmp/ccBCxiFT.o: In function `main':
:(.text+0x5): undefined reference to `XInitThreads'
collect2: error: ld returned 1 exit status
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • You are and kEN seem to be right. I am using gcc and haven't done this before so i do not know where exactly i need to put the "-l flags" yet. Anyway thanks for your help. – user3554800 May 10 '14 at 20:25
0

You should add link X11 library setting -lX11 to your project. If you are using Eclipse navigate to projectproperties->C/C++ Build->Settings->Tool Settings->GCC Linker->Libraries and add "X11"

gerbit
  • 902
  • 9
  • 17
  • I seem to have found the correspondent place in Code::Blocks and put "X11" in the "Link libraries:" list. Now XInitThreads always returns 1, like it should be however when i start to create a new std::thread the program imediately quits after the thread gets created, no idea why. Thanks anyway. – user3554800 May 10 '14 at 20:45
-2

Add header-

#include<X11/Xlib.h>

Compile your source code using-

gcc <filename.extension> -lX11

Tested in Ubuntu 16.04 LTS

Akshat
  • 71
  • 1
  • 7