Linking to Pthreads - Undefined References With Mingw

This is a discussion on Linking to Pthreads - Undefined References With Mingw within the Tech Board forums, part of the Community Boards category; I'm trying to use pthreads and am getting undefined references. Here's the command line Code: C:/Bin/Mingw/bin/../libexec/gcc/mingw32/3.4.2/collect2.exe -Bdynamic -o Stktest.exe C:/Bin/Mingw/bin/../lib/gcc/mingw32/3.4.2/../../../crt2.o ...

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Linking to Pthreads - Undefined References With Mingw

    I'm trying to use pthreads and am getting undefined references. Here's the command line

    Code:
    C:/Bin/Mingw/bin/../libexec/gcc/mingw32/3.4.2/collect2.exe -Bdynamic -o Stktest.exe 
        C:/Bin/Mingw/bin/../lib/gcc/mingw32/3.4.2/../../../crt2.o 
        C:/Bin/Mingw/bin/../lib/gcc/mingw32/3.4.2/crtbegin.o 
    
        -LC:\Bin\Stk\bin -LC:\Bin\Mingw\Lib -LC:\Bin\Pthreads\lib 
        -LC:/Bin/Mingw/bin/../lib/gcc/mingw32/3.4.2 
        -LC:/Bin/Mingw/bin/../lib/gcc 
        -LC:/Bin/Mingw/bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/lib 
        -LC:/Bin/Mingw/bin/../lib/gcc/mingw32/3.4.2/../../.. 
    
        Debug\stdafx.o Debug\Stktest.o 
        -lpthreadGC2 -lstk -lstdc++ -lmingw32 -lgcc -lmoldname -lmingwex -lmsvcrt 
        -luser32 -lkernel32 -ladvapi32 -lshell32 -lmingw32 -lgcc -lmoldname -lmingwex  -lmsvcrt 
    
        C:/Bin/Mingw/bin/../lib/gcc/mingw32/3.4.2/crtend.o
    
    C:\Bin\Stk\bin/libstk.a(RtAudio.o): In function `ZN5RtApiD0Ev':C:/Bin/Stk/src/RtAudio.cpp:263: undefined reference to `_imp__pthread_mutex_destroy'
    C:\Bin\Stk\bin/libstk.a(RtAudio.o): In function `ZN5RtApiD1Ev':C:/Bin/Stk/src/RtAudio.cpp:263: undefined reference to `_imp__pthread_mutex_destroy'
    C:\Bin\Stk\bin/libstk.a(RtAudio.o): In function `ZN5RtApiD2Ev':C:/Bin/Stk/src/RtAudio.cpp:263: undefined reference to `_imp__pthread_mutex_destroy'
    C:\Bin\Stk\bin/libstk.a(RtAudio.o): In function `ZN5RtApiC1Ev':C:/Bin/Stk/src/RtAudio.cpp:258: undefined reference to `_imp__pthread_mutex_init'
    C:\Bin\Stk\bin/libstk.a(RtAudio.o): In function `ZN5RtApiC2Ev':C:/Bin/Stk/src/RtAudio.cpp:258: undefined reference to `_imp__pthread_mutex_init'
    Here's the dumpbin of the pthreads library I'm linking against

    Code:
    C:\Bin\Pthreads\lib>dumpbin libpthreadGC2.a /exports
    Microsoft (R) COFF/PE Dumper Version 8.00.50727.762
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    
    Dump of file libpthreadGC2.a
    
    File Type: LIBRARY
    
         Exports
    
           ordinal    name
    
                      _sem_wait
                      _sem_unlink
                      _sem_trywait
                      _sem_timedwait
                      _sem_post_multiple
                      ...
                      _pthread_mutexattr_destroy
                      _pthread_mutex_unlock
                      _pthread_mutex_trylock
                      _pthread_mutex_timedlock
                      _pthread_mutex_lock
                      _pthread_mutex_init
                      _pthread_mutex_destroy
                      ...
    
      Summary
    
               0 .bss
               0 .data
              14 .idata$2
             1D4 .idata$4
             1D4 .idata$5
             B10 .idata$6
             1DC .idata$7
             398 .text
    
    C:\Bin\Pthreads\lib>
    A similar thread I found was here: http://sourceware.org/ml/pthreads-wi.../msg00056.html
    Answer here: http://sourceware.org/ml/pthreads-wi.../msg00058.html

    But the case of link order doesn't seem to be the issue, but my dumpbin doesn't contain these '_imp_xxx' references, so I'm not exactly sure what to do

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by Tonto View Post
    But the case of link order doesn't seem to be the issue, but my dumpbin doesn't contain these '_imp_xxx' references, so I'm not exactly sure what to do
    Have you actually TRIED putting it last on the link line? Libraries often contain weak symbols which alias other symbols in the library. The _imp_xxx symbols might be there, but dumpbin isn't showing them.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Ah that is very interesting (I only dumped the exports, didn't know I should look at anything else)

    Code:
    String Table Size = 0x5D bytes
    
    COFF SYMBOL TABLE
    000 00000000 SECT1  notype       Static       | .text
    001 00000000 SECT2  notype       Static       | .data
    002 00000000 SECT3  notype       Static       | .bss
    003 00000000 SECT4  notype       Static       | .idata$7
    004 00000000 SECT5  notype       Static       | .idata$5
    005 00000000 SECT6  notype       Static       | .idata$4
    006 00000000 SECT7  notype       Static       | .idata$6
    007 00000000 SECT1  notype       External     | _pthread_mutexattr_init
    008 00000000 SECT5  notype       External     | __imp__pthread_mutexattr_init
    009 00000000 UNDEF  notype       External     | __head_libpthreadGC2_a
    
    
    String Table Size = 0x49 bytes
    
    COFF SYMBOL TABLE
    000 00000000 SECT1  notype       Static       | .text
    001 00000000 SECT2  notype       Static       | .data
    002 00000000 SECT3  notype       Static       | .bss
    003 00000000 SECT4  notype       Static       | .idata$7
    004 00000000 SECT5  notype       Static       | .idata$5
    005 00000000 SECT6  notype       Static       | .idata$4
    006 00000000 SECT7  notype       Static       | .idata$6
    007 00000000 SECT1  notype       External     | _pthread_mutex_destroy
    008 00000000 SECT5  notype       External     | __imp__pthread_mutex_destroy
    009 00000000 UNDEF  notype       External     | __head_libpthreadGC2_a

    >> Have you actually TRIED putting it last on the link line?

    Yeah I don't know how I read it this way but I just thought it meant after the object files on the command line. Sorry about that, it now compiles perfectly. Gahd I'm thickheaded.

    >> Libraries often contain weak symbols which alias other symbols in the library.

    Cool, I didn't know that.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by Tonto View Post
    Yeah I don't know how I read it this way but I just thought it meant after the object files on the command line. Sorry about that, it now compiles perfectly. Gahd I'm thickheaded.
    The exact error message was a clue:

    C:\Bin\Stk\bin/libstk.a(RtAudio.o): In function `ZN5RtApiD0Ev':C:/Bin/Stk/src/RtAudio.cpp:263: undefined reference to `_imp__pthread_mutex_destroy'
    The undefined reference is in libstk.a, NOT your code. That means the pthreads library had to be listed AFTER libstk on the link line.

    Pay attention to the error messages ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21