Important: Please read the Qt Code of Conduct -

Include file not working. Getting "undefined reference to "

  • I'm trying to create a GUI and i'm having issues with some header files that the program needs and uses.

    If i click on the function in the main.cpp it takes me to the header file. But for some reason when i compile the program I get "undefined reference to " the function in main.cpp. Ive tried to add the path of the include folder where the header file in the .pro file but it didnt work. It seems to me that Qt sees the function but somehow it doesnt compile.

    Any suggestions? Thank you

  • Hi @Count-Vlad,

    An "undefined reference" error does not suggest a problem with the compiler finding the header (which declares the function), but more likely a problem with the compiler (or linker) not seeing the *.cpp file or object file containing the definition for the function.

    If the function is defined in your local project somewhere, make sure its *.cpp files is included in the *.pro file.

    If the function comes from some external library, make sure the library is included in the *.pro file.


  • Lifetime Qt Champion

    @Count-Vlad What header file is it? One of the Qt header files?

  • HI @Count-Vlad

    you have mentioned that, *.h header file is added, but you have not specified *.cpp file or related library is included.
    Try to add *.cpp file in *.pro file. If same issue, delete build folder and re build the project.

  • for some reason the compiler did not see the library that the header file needed. it was specified in the makefile but it didnt work.

    in the .pro file i added

    LIBS += -lmylib

    and that fixed the issue. Thank you for your help

  • Lifetime Qt Champion

    @Count-Vlad: Glad you figured it out.

    LIBS += -lmylib is indeed the correct way to tell the linker to link to your lib. If you modify the Makefiles created by Qmake, your changes will be overwritten the next time qmake is run.

    So please mark this thread as solved now.

Log in to reply