Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QT creator: failed to make reference to cpp in another project
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wangyc77
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      melghawi
      wrote on last edited by
      #2

      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).

      1 Reply Last reply
      0
      • W Offline
        W Offline
        wangyc77
        wrote on last edited by
        #3

        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"@

        1 Reply Last reply
        0
        • M Offline
          M Offline
          melghawi
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved