Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

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

    General and Desktop
    2
    4
    815
    Loading More Posts
    • 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
      wangyc77 last edited by

      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 Reply Quote 0
      • M
        melghawi last edited by

        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 Reply Quote 0
        • W
          wangyc77 last edited by

          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 Reply Quote 0
          • M
            melghawi last edited by

            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 Reply Quote 0
            • First post
              Last post