Menu

Undefined reference when linking to library

2008-04-29
2012-09-26
  • David Johnson

    David Johnson - 2008-04-29

    I've got some trouble linking to a library I've compiled. Specifically, trying to create a program to test the wiimote-api: http://code.google.com/p/wiimote-api/
    I compiled it myself in Dev-C++, exactly as the source on the site (with the linker option "-lhid"). I then moved the output, libwiimote-api.a to C:\MinGW\lib and wiimote-api.h to C:\MinGW\include

    This is the top and main function of the testing program (FindHIDs is a function I got from the original author, which doesn't seem to have any problems):


    include "wiimote-tester.h"

    define wVendorID 0x057e;

    define wProductID 0x0306;

    typedef struct _HID_INFO
    {
    BYTE Name[64];
    BYTE DevicePath[256];
    } HID_INFO;

    HID_INFO* hidarray;
    int num_hids;

    HID_INFO FindHIDs(int num)
    {...}

    int main(int argc, char *argv[])
    {
    cout<<"Wiimote controller test .01"<<endl<<"By David Johnson - 4/25/08"<<endl;
    hidarray = FindHIDs(&num_hids);
    int index;
    cout << "Please choose the device that is your wiimote: " << flush;
    cin >> index;
    index--;
    cout << "Connecting to "<< hidarray[index].Name <<endl;

    WiiM_ConnectWiimote(hidarray[index].DevicePath);
    
    cout &lt;&lt; &quot;Done!&quot; &lt;&lt; endl;
    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
    

    }

    And wiimote-tester.h:


    #include <windows.h>
    #include <setupapi.h>
    #include <cstdlib>
    #include <iostream>
    #include "C:\MinGW\include\ddk\hidsdi.h"
    #include <wiimote-api.h>

    using namespace std;


    And finally the compile log:


    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\Makefile.win"
    Executing make...
    mingw32-make -f "C:\Dev-Cpp\Makefile.win" all
    g++.exe wiimote-tester.o -o "Project1b.exe" -L"C:/WINDDK/3790.1830/lib" -lwiimote-api -lhid -lsetupapi

    wiimote-tester.o:wiimote-tester.cpp:(.text+0x544): undefined reference to `WiiM_ConnectWiimote(unsigned char*)'

    mingw32-make: *** [Project1b.exe] Error 1

    Execution terminated


    I've checked every mistake I can think of, including double-checking that I'm spelling the function name correctly about 20 times. I'm very inexperienced with C++, particularly since guides and "how-tos" about programming in C++ are always about writing a program in one file and never have any information on linking or using precompiled libraries. So please, if you see an error in my code or if you know any common mistakes that I could me making, please tell me. As far as I can tell, this should work.

     
    • cpns

      cpns - 2008-05-01

      Ok, the conclusion must be therefore that while the header file contains a declaration of WiiM_ConnectWiimote(unsigned char*), the library does not.

      I took a look at the source code and note that it is C not C++ and that the header is not written to support linkage with C++ code. So it will be looking for the C++ mangled name.

      The simple fix is to modify your code thus:

      extern "C"
      {
      #include <wiimote-api.h>
      }

      A more reusable solution is to modify the header itself this:

      ifdef __cplusplus

      extern "C" {

      endif

      // original header file content here

      ifdef __cplusplus

      }

      endif

      The header file will then work correctly for C and C++ code. You might want to post this fix back to the original author (who frankly should have known better).

      Clifford

       
    • David Johnson

      David Johnson - 2008-04-29

      Oh yeah... and I forgot to mention, I'm using Dev-C++ Ver. 4.9.9.2 running on Vista Home Premium edition

       
    • cpns

      cpns - 2008-04-29

      You never told the linker where to look for the library (with a -L<path> option). You need:

      -LC:\MinGW\lib

      in the linker options.

      Unrelated, but if you specified C:\MinGW\include to the compiler (which you must have done), the line:

      include "C:\MinGW\include\ddk\hidsdi.h"

      can be shortened to:

      include "ddk\hidsdi.h"

      Clifford

       
      • David Johnson

        David Johnson - 2008-04-29

        Yeah, that... that was already specified in the compiler options, just like C:\MinGW\include was.
        I've made sure that if it couldn't find the library, it would have thrown an error as such. But then, I can't find anything to explain why it isn't working, so...
        Still, though, I added that line to be sure, and still the same undefined reference error.

         
        • cpns

          cpns - 2008-04-30

          >> [...]that was already specified in the compiler options, [...]

          Well it may have been but it is a linker option, not a compiler option. I made the inference about -LC:\MinGW\include because to have started to link, it had to compile. Your log does not show the compilation because this is not a Rebuild-All log and wiimote-tester.o was already up-to-date. A Rebuild-All is useful when posting a log since it contains more complete information about your build and settings. Maybe we'd have seen your -L option in the wrong place.

          Your log clearly shows that you have not set the library path (otherwise I would not have mentioned it!).

          This line:

          >> g++.exe wiimote-tester.o -o "Project1b.exe" -L"C:/WINDDK/3790.1830/lib" -lwiimote-api -lhid -lsetupapi

          is the linker invocation; there is no -LC:\MinGW\lib there.

          Clifford

           
          • David Johnson

            David Johnson - 2008-04-30

            I see what you're saying, but I meant "compiler options" as the window that appears when one goes to the Tools menu and selects Compiler Options - in this case, under the tab Directories > Libraries. You're right about the compile log, though, so I must have added that after posting my log (I've also moved the files around a bit).

            Regardless, that's fixed, and I'm still getting the same linker error. After making sure the contents are the same as what I already posted above, here is the current log for Rebuild-All:


            Compiler: Default compiler
            Building Makefile: "C:\Users\Axel\Programming\Wiimote2\Makefile.win"
            Executing make clean
            rm -f wiimote-tester.o wiimote-tester.exe

            g++.exe -c wiimote-tester.cpp -o wiimote-tester.o -I"C:/MinGW/include" -ansi -traditional-cpp

            g++.exe wiimote-tester.o -o "wiimote-tester.exe" -L"C:/MinGW/lib" -lsetupapi -lwiimote-api -lhid

            wiimote-tester.o:wiimote-tester.cpp:(.text+0x544): undefined reference to `WiiM_ConnectWiimote(unsigned char*)'

            mingw32-make: *** [wiimote-tester.exe] Error 1

            Execution terminated


             

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.