error undefined reference to function that declared in header and dynamically linked



  • I'm trying to use FTDI libMPSSE library in my QT application. I have made sure that I have imported my external libraries properly and I have made sure that I have include the header files into the project. When I go to build the application referencing a function I get and undefined reference to error. My project can be found here


  • Moderators

    Drop the "lib" prefix in your linker options i.e. win32: LIBS += -L$$PWD/./ -lMPSSE instead of win32: LIBS += -L$$PWD/./ -llibMPSSE.



  • @Chris-Kawa I have tried with and without lib in front of MPSEE already and I still get the same error


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you sure that your version of libMPSSE is built for the same architecture as the Qt version your are using ?


  • Moderators

    @SGaist said in error undefined reference to function that declared in header and dynamically linked:

    Hi and welcome to devnet,

    Are you sure that your version of libMPSSE is built for the same architecture as the Qt version your are using ?

    @tony2383 This is almost always the problem.. Especially in windows. People like to mix visual c++ libs and mingw and cygwin and any other type you can imagine. Needless to say it doesn't work however there are no errors thrown except the one you are seeing.

    Make sure you are linking libs that were built with the same compiler (i.e. mingw) and the same addressing i.e. 64 bit/32 bit, etc.

    If all else fails you can use the ar command to see if your library contains the symbols you think it does. ar only works with libs compiled with mingw/cygwin. Also to get the ar command you'll probably need cygwin unless you have a mingw install that has it as well.

    Edit:

    I checked your files for you. You have 3 libs in your directory dll/lib/a. Try removing the dll & lib and build again using the static library (the .a). That should resolve ambiguity. My guess is the dll and lib are not build with mingw and are causing the issue.

    Here is the result of the ar:

    [shockwave] ~/FTDI_QT > ar t libMPSSE.a
    ftdi_infra.o
    ftdi_mid.o
    ftdi_i2c.o
    

    So it has those 3 objects in there.

    Also please show the actual output of the build so we can see the exact error.



  • @SGaist Yes the libMPSSE was built with MINGW 5.3.0 same as the compiler that I'm using within QT. I'm not sure that it would make a difference that I used a sepreate install of MINGW to compile the library outside the one that is provided with QT. @ambershark I build the library using MINGW32. Does it make a difference that the library was compiled using a C compiler (GCC ) instead of a C++ compiler (G++). I thought it wouldn't being that C code can be compiled using a C++ compiler. Also, I tried removing the the DLL and the LIB and compiling with a static link and I still receive the same error. The errors that I recieve are

    • list item"C:\qt_projects\build-new_sample-Desktop_Qt_5_7_1_MinGW_32bit-Debug\debug\mainwindow.o:-1: In function `ZN10MainWindow21on_pushButton_clickedEv':"

    • list item C:\qt_projects\new_sample\mainwindow.cpp:21: error: undefined reference to `Init_libMPSSE()'

    • list item collect2.exe:-1: error: error: ld returned 1 exit status


  • Moderators

    @tony2383 said in error undefined reference to function that declared in header and dynamically linked:

    @SGaist Yes the libMPSSE was built with MINGW 5.3.0 same as the compiler that I'm using within QT. I'm not sure that it would make a difference that I used a sepreate install of MINGW to compile the library outside the one that is provided with QT. @ambershark I build the library using MINGW32. Does it make a difference that the library was compiled using a C compiler (GCC ) instead of a C++ compiler (G++). I thought it wouldn't being that C code can be compiled using a C++ compiler. Also, I tried removing the the DLL and the LIB and compiling with a static link and I still receive the same error. The errors that I recieve are

    As long as you use g++ as the actual linker you should be fine. Easy to test though and just build the lib with g++. But you definitely need to use g++ and not gcc to do the linking step or it won't work.

    • list item"C:\qt_projects\build-new_sample-Desktop_Qt_5_7_1_MinGW_32bit-Debug\debug\mainwindow.o:-1: In function `ZN10MainWindow21on_pushButton_clickedEv':"

    • list item C:\qt_projects\new_sample\mainwindow.cpp:21: error: undefined reference to `Init_libMPSSE()'

    • list item collect2.exe:-1: error: error: ld returned 1 exit status

    Which object that my ar command listed is the Init_libMPSSE() function in?


  • Qt Champions 2016

    Hi
    Small question.
    If compiled with c compiler, will it not need
    extern "c" {
    }
    To be used from c++ ?
    Or is that automatically with g++ ?


  • Moderators

    @mrjj said in error undefined reference to function that declared in header and dynamically linked:

    Hi
    Small question.
    If compiled with c compiler, will it not need
    extern "c" {
    }
    To be used from c++ ?
    Or is that automatically with g++ ?

    Extern c goes the other way. It's for a c++ compiler to not name mangle the function so it can be used from c code. So since his end product will be in c++ it wouldn't need the extern c's since c++ would understand the name mangled functions.


  • Qt Champions 2016

    @ambershark
    Ok, I most likely read it wrong then.
    As i saw
    LIB created with c compiling
    No name mangling.

    We link this to c++ program and
    to make it see the c names, we
    do
    extern "c" {
    void libFunc1(p1, p2);
    void libFunc2();
    }

    Sorry. Too much coffee I guess :)


  • Lifetime Qt Champion

    Again, did you check that the architecture of your built library is indeed 32bit like Qt ?



  • @SGaist the library was built with MINGW 5.3.0, same as the compiler that I'm using within QT. @ambershark the init_libMPSSE function was removed from the library in the most recent version of the library source code. But the effects is still the same when I use any other function call. I have updated my repo to use function I2C_GetNumChannels and still receive the same error. The function can be found in the ftdi_i2c.o.


  • Qt Champions 2016

    You compiled said library (libMPSSE) with C or C++ compiler? If you compiled it with a c++ compiler, then you need to match it and use the same compiler in your application code. If you compiled with a C compiler, then you need to instruct your C++ compiler (the one you're using in the application) to not put references to decorated functions. That means you need to extern "C" {} the includes. E.g.:

    extern "C" 
    {
         #include <mpsse.h>
         // ...
    }
    

    C-linkage is compatible across all compilers, so there's no need to match the compiler (but you do need to match architecture) in this case.


  • Moderators

    @tony2383 Since you said you compiled with the same version of mingw for the lib as well as your project then the above comment probably won't help you.

    Continuing down that line of thought and assuming (huge assumption here) that your libs are correctly matched, please post the output of make VERBOSE=1 for me to take a look at.

    Just so you know this problem is almost always a wrongly built library though. So it's more likely that the MPSSE lib is built different than your program.

    Do you potentially have a copy of libMPSSE somewhere else in your lib path that is getting pulled in instead of the one you built? Are you absolutely sure your build of libMPSSE uses the same bit depth and compiler as your application?


  • Qt Champions 2016

    @ambershark said in error undefined reference to function that declared in header and dynamically linked:

    Since you said you compiled with the same version of mingw for the lib as well as your project then the above comment probably won't help you.

    mingw does compile C code (with C linkage), you know ...


  • Moderators

    @kshegunov said in error undefined reference to function that declared in header and dynamically linked:

    @ambershark said in error undefined reference to function that declared in header and dynamically linked:

    Since you said you compiled with the same version of mingw for the lib as well as your project then the above comment probably won't help you.

    mingw does compile C code (with C linkage), you know ...

    Yea, it would be a lot easier to diagnose his problem if he actually posted his build output. It would be pretty obvious if he was mixing C/C++ at that point.

    The reason I said that extern C'ing his stuff wasn't the solution is he assured us he was using the same compiler for all his stuff. That to me means he knew the difference between a C compiler and a C++ one. It definitely doesn't hurt to throw the extern c around the includes though to test it. I wasn't meaning to downplay your answer at all. Sorry if it seemed like that. :)


  • Qt Champions 2016

    @ambershark said in error undefined reference to function that declared in header and dynamically linked:

    Yea, it would be a lot easier to diagnose his problem if he actually posted his build output. It would be pretty obvious if he was mixing C/C++ at that point.
    The reason I said that extern C'ing his stuff wasn't the solution is he assured us he was using the same compiler for all his stuff.

    He might've stumbled on some "default behavior" (how can you not love C/C++) where the C compiler is invoked when it sees .c extension. So it may be really transparent from afar, and yet to happen so there's mixing of C and C++ compiled code.

    I wasn't meaning to downplay your answer at all. Sorry if it seemed like that.

    No offence taken, I have thicker skin than that. :)


  • Moderators

    @kshegunov said in error undefined reference to function that declared in header and dynamically linked:

    He might've stumbled on some "default behavior" (how can you not love C/C++) where the C compiler is invoked when it sees .c extension. So it may be really transparent from afar, and yet to happen so there's mixing of C and C++ compiled code.

    Absolutely true! I have myself stumbled across that a few times. It always throws me off for a second too.

    No offence taken, I have thicker skin than that. :)

    Oh good! You never know with written word. I never mean to insult people but sometimes I do online, so now I try to apologize if I feel I might have. :)



  • @kshegunov @ambershark that worked. Still having a problem with FTDI libraries, but I will hop on one of there forums to address those issues. Thanks everyone for their help.


  • Qt Champions 2016

    So in the end
    my hopeless formulated question
    about use extern "c" , turned out to be the case ?
    My very old compiler does like that. hence I asked :)


  • Moderators

    @mrjj said in error undefined reference to function that declared in header and dynamically linked:

    So in the end
    my hopeless formulated question
    about use extern "c" , turned out to be the case ?
    My very old compiler does like that. hence I asked :)

    Indeed it was. :) I was quick to shrug it off too thinking no that can't be it... But then you and @kshegunov proved me quite wrong there. ;)


  • Qt Champions 2016

    @ambershark

    well, i would (also) have bet my money
    on mixed libs/compiler or incorrect LIBS +=
    and not extern "c" so that is very understandable.

    Also, very informative as i did not knew mingw would do that :)
    Cheers


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.