QT creator: failed to make reference to cpp in another project



  • This is similar to this "topic":http://qt-project.org/forums/viewthread/1991

    I use
    @INCLUDEPATH += ../tutorial02-multiple-projects2@
    and this is my main program
    @#include <iostream>
    #include "myclass.h"

    using namespace std;

    int main()
    {
    cout << "Hello World!" << endl;

    MyClass m;
    m.SayHello("Mary");
    
    return 0;
    

    }@
    and my sub program(dependent program)
    @#include "myclass.h"

    MyClass::MyClass()
    {

    }

    int MyClass::SayHello(string myName)
    {
    cout << "Hello " << myName << endl;
    return 1;
    }
    @
    header file
    @#ifndef MYCLASS_H
    #define MYCLASS_H

    #include <string>
    #include <iostream>

    using namespace std;

    class MyClass
    {
    public:
    MyClass();
    int SayHello(string myName);
    };

    #endif // MYCLASS_H
    @
    when I tried to compile them, i keep receiving the error message at the main program. The error is it cannot refer to external symbol;

    @main.obj:-1: error: LNK2019: unable to refer to external symbol "public: __cdecl MyClass::MyClass(void)" (??0MyClass@@QEAA@XZ) ....@
    @ MyClass m;
    m.SayHello("Mary");@

    Thanks



  • Hi wangyc77.

    I assume that you have not added MyClass.cpp to your main application. In that case your main application cannot resolve certain symbols because it simply cannot find them.

    If you are trying to include code from a different project then you need to build your sub-project as a library(dynamic or static) and then link against that library in your main project (this will get rid of those annoying linker errors).



  • I did, but I'm not sure if I'm doing it right
    @unix: LIBS += -L"../build-subproject-Desktop_Qt_5_3_MSVC2013_32bit-Debug/debug"
    win32: LIBS += "C:\Users\Hamilton\Documents\cpp_tutorial\tutorial02-multiple projects\build-subproject-Desktop_Qt_5_3_MSVC2013_32bit-Debug\debug\myclass.obj"@



  • Does your sub project generate a .lib file(on Windows) or .a file(on Linux)?

    If not, make sure you have the following defined in your .pro file:

    TEMPLATE=lib (replace TEMPLATE=app if its there)
    CONFIG=staticlib

    Then instead of linking against the .obj file you can link against the .lib/.a file. If you right click when editing your .pro file you get a nice wizard to help you add libs to your project.


Log in to reply
 

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