Linker errors in compiling a simple pthread code under MingW

后端 未结 1 977
失恋的感觉
失恋的感觉 2021-01-12 18:16

I am trying to compile a single c++ source file test.cpp, which has a very simple code which demonstrates pthread_create(); pthread_cond_signal/pthread_cond_wait() functiona

相关标签:
1条回答
  • 2021-01-12 18:22

    You need to specify the library on the gcc/g++ command line after the files that depend on the library. So try:

    g++ -IC:/MinGW/include/ test.cpp -lpthread
    

    I kicked myself when I stumbled on the answer (it's kind of a FAQ for libraries and gcc). For most gcc options order doesn't matter, but for libraries it's critical.

    You should not have to specify the library path if the pthread library came with your MinGW distribution (as it seems is the case for you). Also, remember that the command line above will produce an a.exe executable; pass -o test.exe to avoid that.

    0 讨论(0)
提交回复
热议问题