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. Subdirs : How to include a file from a project to an other project?
QtWS25 Last Chance

Subdirs : How to include a file from a project to an other project?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 6.5k 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.
  • S Offline
    S Offline
    Sun864
    wrote on last edited by
    #1

    Hello, I am kinda new to qt, I have to code a tetris game for my school project, I'm using template subdirs and I couldn't figure out how can i include a file from a project to an other project.

    Here is the composition of my project :
    Project tetris containing 2 projects : core and tests.

    In my tests.pro i added this : INCLUDEPATH += "../core", so I'm able to include a class from my project core to my project tests, but I can't use it.

    Here is the errors :
    scoretest.obj : error LNK2019: unresolved external symbol "public: __cdecl Score::Score(void)" (??0Score@@QEAA@XZ) referenced in function "void __cdecl ____C_A_T_C_H____T_E_S_T____4(void)" (?____C_A_T_C_H____T_E_S_T____4@@YAXXZ)
    debug\tests.exe : fatal error LNK1120: 1 unresolved externals

    K 1 Reply Last reply
    0
    • S Sun864

      Hello, I am kinda new to qt, I have to code a tetris game for my school project, I'm using template subdirs and I couldn't figure out how can i include a file from a project to an other project.

      Here is the composition of my project :
      Project tetris containing 2 projects : core and tests.

      In my tests.pro i added this : INCLUDEPATH += "../core", so I'm able to include a class from my project core to my project tests, but I can't use it.

      Here is the errors :
      scoretest.obj : error LNK2019: unresolved external symbol "public: __cdecl Score::Score(void)" (??0Score@@QEAA@XZ) referenced in function "void __cdecl ____C_A_T_C_H____T_E_S_T____4(void)" (?____C_A_T_C_H____T_E_S_T____4@@YAXXZ)
      debug\tests.exe : fatal error LNK1120: 1 unresolved externals

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Sun864

      HI and welcome to devnet forum

      INCLUDEPATH is only for searching include files (header files). It is a path setting.

      Did you see already this wiki on SUBDIRS template?

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sun864
        wrote on last edited by
        #3

        Of course I did, I already added "tests.depends = core" and "CONFIG += ordered" to my Tetris.pro but I don't know I am missing something.
        I probably missed something, the wiki is about only the main project.pro, did I missed something in Tetris.pro or tests.pro?

        K 1 Reply Last reply
        0
        • S Sun864

          Of course I did, I already added "tests.depends = core" and "CONFIG += ordered" to my Tetris.pro but I don't know I am missing something.
          I probably missed something, the wiki is about only the main project.pro, did I missed something in Tetris.pro or tests.pro?

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @Sun864

          Typically it is best to have already a structure as you want to use with SUBDIRS template, but you set up and test the proejcts independently. This makes it easier to locate the problem. In a next step you can combine both project and compile together.

          In your case this seems to be a library and a main program. When you can compile and link the library successfully, you are already one step further.
          The second project would be the main program which requires to link with the library of the first project.

          Are you using Qt creator as IDE?
          Which OS are you on?

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Sun864
            wrote on last edited by
            #5

            Yes I'm using Qt creator as IDE and I'm on Windows 7.

            My project tests contains unit testing of my project core, I don't get it, I have to compile the first project when I compile the second to make it work? Sorry this is the first time I'm using SUBDIRS and trying to include a file from an other project.

            K 1 Reply Last reply
            0
            • S Sun864

              Yes I'm using Qt creator as IDE and I'm on Windows 7.

              My project tests contains unit testing of my project core, I don't get it, I have to compile the first project when I compile the second to make it work? Sorry this is the first time I'm using SUBDIRS and trying to include a file from an other project.

              K Offline
              K Offline
              koahnig
              wrote on last edited by koahnig
              #6

              @Sun864

              Suppose
              1 folder with a library Lib1 on c:/Source/Lib1

              Lib1.h

              #ifndef LIB1_H
              #define LIB1_H
              
              #include "lib1_global.h"
              
              class LIB1SHARED_EXPORT Lib1
              {
              
              public:
                  Lib1();
              };
              #endif // LIB1_H
              

              lib1_global.h:

              #ifndef LIB1_GLOBAL_H
              #define LIB1_GLOBAL_H
              
              #include <QtCore/qglobal.h>
              
              #if defined(LIB1_LIBRARY)
              #  define LIB1SHARED_EXPORT Q_DECL_EXPORT
              #else
              #  define LIB1SHARED_EXPORT Q_DECL_IMPORT
              #endif
              
              #endif // LIB1_GLOBAL_H
              

              Lib1.cpp

              #include "Lib1.h"
              #include <QDebug>
              
              Lib1::Lib1()
              {
                  qDebug() << "hello!";
              }
              

              Lib1.pro

              #-------------------------------------------------
              #
              # Project created by QtCreator 2017-04-03T20:37:42
              #
              #-------------------------------------------------
              
              QT       -= gui
              
              TARGET = Lib1
              TEMPLATE = lib
              
              # next line is added for getting the libraries off the shadow folder
              DESTDIR = $$PWD
              
              DEFINES += LIB1_LIBRARY
              
              # The following define makes your compiler emit warnings if you use
              # any feature of Qt which as been marked as deprecated (the exact warnings
              # depend on your compiler). Please consult the documentation of the
              # deprecated API in order to know how to port your code away from it.
              DEFINES += QT_DEPRECATED_WARNINGS
              
              # You can also make your code fail to compile if you use deprecated APIs.
              # In order to do so, uncomment the following line.
              # You can also select to disable deprecated APIs only up to a certain version of Qt.
              #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
              
              SOURCES += Lib1.cpp
              
              HEADERS += Lib1.h\
                      lib1_global.h
              

              1 folder with the main application on c:/Source/Main1

              #include <QCoreApplication>
              
              #include "Lib1.h"
              
              int main(int argc, char *argv[])
              {
                  QCoreApplication a(argc, argv);
              
                  Lib1 tst;
              
                  return 0; // a.exec();
              }
              

              and Main1.pro

              QT += core
              QT -= gui
              
              CONFIG += c++11
              
              TARGET = Main1
              CONFIG += console
              CONFIG -= app_bundle
              
              TEMPLATE = app
              
              # the next two lines are added manually
              INCLUDEPATH += ../Lib1
              LIBS += -L$$PWD/../Lib1 -lLib1
              
              SOURCES += main.cpp
              
              # The following define makes your compiler emit warnings if you use
              # any feature of Qt which as been marked deprecated (the exact warnings
              # depend on your compiler). Please consult the documentation of the
              # deprecated API in order to know how to port your code away from it.
              DEFINES += QT_DEPRECATED_WARNINGS
              
              # You can also make your code fail to compile if you use deprecated APIs.
              # In order to do so, uncomment the following line.
              # You can also select to disable deprecated APIs only up to a certain version of Qt.
              #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
              

              1 folder with the subdirs project on c:/Source/Subdirs1
              Subdirs1.pro

              TEMPLATE = subdirs
              
              SUBDIRS = \
                          MyLib1 \
                          MyMain1
              
              # where to find the sub projects - give the folders
              MyLib1.subdir = ../Lib1
              MyMain1.subdir = ../Main1
              
              # what subproject depends on others
              MyMain.depends = MyLib1
              
              CONFIG += ordered
              

              If you put the files into respective folders and open each of those project files (.pro) individually in Qt creator, you shall be able to compile the library and the main application separately. When you have compiled and linked the main application, you can run it.

              Finally you can open also the subdirs project and compile, link and run all at once. That is all the principle magic behind subdirs template. For those peanuts projects it is an overkill, but when you have more complex structures it makes sense and helps to structure your different tasks.

              BTW: I have simply created the lib and main projects through Qt creator and added only minimal changes. After this I have added the subdirs project and added most of it manually.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Are you sure about this path -L$$PWD/../Lib1 ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                K 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  Are you sure about this path -L$$PWD/../Lib1 ?

                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  @SGaist

                  For sure not absolutely required, but it does work.

                  However, this looks more consistent in Main1.pro

                  # the next two lines are added manually
                  INCLUDEPATH += ../Lib1
                  LIBS += -L../Lib1 -lLib1
                  

                  Side note: The libraries of Lib1.pro have been placed in the source folder of Lib1 with:

                  # next line is added for getting the libraries off the shadow folder
                  DESTDIR = $$PWD
                  

                  Certainly I would not do for large projects. For this short demonstration it helps IMHO to focus.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Sorry, I was thinking about to point the project to the actual path where the library were put after being built so rather something like -L$OUT_PWD/lib where all the sub-projects would put their libraries. So there's no need to copy anything in the sources.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    K 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Sorry, I was thinking about to point the project to the actual path where the library were put after being built so rather something like -L$OUT_PWD/lib where all the sub-projects would put their libraries. So there's no need to copy anything in the sources.

                      K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #10

                      @SGaist

                      But how this be useful?
                      OUT_PWD states that this is folder with the makefiles. However, it should be of the other project, shouldn't it?

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Since it's a subdir project, you can create a .pri file that you'll use in all libraries subprojects to put the build result in a known place in the build tree. Known folder that you can then use from your application project.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        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