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. How to use headers from another project?
Forum Updated to NodeBB v4.3 + New Features

How to use headers from another project?

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 10.3k Views 1 Watching
  • 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.
  • Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Do you mean use the class source, link to a library (statically or dynamically) and create an instance of a class exported from there or dynamically load a library at runtime and get a class instance from it?

    In the first case just include the files in your project just as you would your own.

    To link a lib (statically or dynamically) include header and feed linker the lib location (how to do that depends on the compiler and/or IDE you're using).

    For the last option you can use either "QLibrary":http://qt-project.org/doc/qt-5/qlibrary.html or a platform specific function like "LoadLibrary":http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175.aspx on Windows.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Exotic_Devel
      wrote on last edited by
      #3

      My case is the first. The class was created in another project and is separated into source and header.
      How to include in the project, since they are in different directories?

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #4

        They are just files no different from your own project files. Add them the same.
        You havent said what build system and compiler you're using but here's an example for qmake:
        @
        INCLUDES += my_header.h
        my_other_header.h
        ../some_lib/using_relative_path/foo.h
        c:/yet_another_lib/using_absolute_path/bar.h

        SOURCES += my_source.cpp
        my_other_source.cpp
        ../some_lib/using_relative_path/foo.cpp
        c:/yet_another_lib/using_absolute_path/bar.cpp
        @

        1 Reply Last reply
        0
        • jeremy_kJ Online
          jeremy_kJ Online
          jeremy_k
          wrote on last edited by
          #5

          Some definition of "project" is needed. Creator has been taken off the table, but that doesn't mean much. Is this a qmake/cmake/qbs/ant driven build project? Is this a software configuration management question?

          If the goal really is limited to accessing header files, do the two projects have fixed relative paths? #include "../other_project/file.h" is a simple solution for compilation. It won't help with linking that will almost certainly follow.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Exotic_Devel
            wrote on last edited by
            #6

            Yes, I using qmake. Is necessary full path? example (/home/user/Projects/....)
            Both projects are in / home / user / Project /

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #7

              It depends.
              If you move around that project or use source control or share it with others absolute paths are not a good idea (because they can vary from machine to machine).
              You can either use relative paths or an environment variable that everyone opening that project will set.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Exotic_Devel
                wrote on last edited by
                #8

                Hi, sorry for the delay. make not found headers files.

                @
                ######################################################################

                Automatically generated by qmake (3.0) ter jun 3 16:58:25 2014

                ######################################################################

                TEMPLATE = app
                TARGET = unitytest
                INCLUDEPATH += .

                QT += widgets
                QT += uitools
                QT += testlib
                QMAKE_CXXFLAGS += -std=c++11

                Input

                SOURCES += LoadUiTest.cpp \ ../kadernoapp/Sources/LoadUi.cpp
                HEADERS += ../kadernoapp/Headers/LoadUi.hpp
                @

                Reading the qmake documentation, found the "include() function":https://qt-project.org/doc/qt-4.8/qmake-project-files.html
                But do not know how it applies to my case

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  Exotic_Devel
                  wrote on last edited by
                  #9

                  Resolved. Basically lacked in INCLUDEPATH set the path variable.
                  I also had problems like MOC, I left the delcração and definition in a .cpp file.
                  What is the purpose of INCLUDEPATH variable? Since I also have to put the path to the SOURCES and HEADERS variables?

                  @
                  ######################################################################

                  Automatically generated by qmake (3.0) ter jun 3 19:47:19 2014

                  ######################################################################

                  TEMPLATE = app
                  TARGET = unitytest
                  INCLUDEPATH += . ../kadernoapp/Headers/

                  QT += widgets
                  QT += uitools
                  QT += testlib
                  QMAKE_CXXFLAGS += -std=c++11

                  Input

                  HEADERS += LoadUiTest.hpp ../kadernoapp/Headers/LoadUi.hpp
                  SOURCES += ../kadernoapp/Sources/LoadUi.cpp
                  @

                  1 Reply Last reply
                  0
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #10

                    INCLUDEPATH lets you skip the directory in the HEADERS section i.e. instead of
                    @
                    HEADERS +=
                    ../some/long/dir/foo.h
                    ../some/long/dir/bar.h
                    @
                    you can write
                    @
                    INCLUDEPATH += ../some/long/dir/
                    HEADERS += foo.h bar.h
                    @
                    I think in your first listing the error could be because of the \ in the SOURCES section (it skips to the next line after that), or maybe you didn't re-run qmake after the changes.

                    As a side note you can specify multiple Qt modules in one line eg.
                    @
                    QT += widgets uitools testlib
                    @
                    Also, instead of compiler specific flag for c++11, you should use more general
                    @
                    CONFIG += c++11
                    @
                    The include() function is for including other .pro files in your project. It doesn't apply here.

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      Exotic_Devel
                      wrote on last edited by
                      #11

                      [quote author="Chris Kawa" date="1401840629"]INCLUDEPATH lets you skip the directory in the HEADERS section i.e. instead of
                      @
                      HEADERS +=
                      ../some/long/dir/foo.h
                      ../some/long/dir/bar.h
                      @
                      you can write
                      @
                      INCLUDEPATH += ../some/long/dir/
                      HEADERS += foo.h bar.h
                      @
                      [/quote]

                      If I remove the path from the HEADERS variable,
                      @HEADERS += LoadUiTest.hpp LoadUi.hpp@

                      I get a warning.

                      @5~WARNING: Failure to find: LoadUi.hpp@

                      In my view, supposed to work, since IncludePath variable is set to also search the current directory (.)

                      1 Reply Last reply
                      0
                      • Chris KawaC Offline
                        Chris KawaC Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        Ups, sorry, I accidentaly mislead you.
                        INCLUDEPATH is not for HEADERS but for #include statements in your code, so you can skip path there.

                        1 Reply Last reply
                        0
                        • E Offline
                          E Offline
                          Exotic_Devel
                          wrote on last edited by
                          #13

                          Well that's what I'm not understand is. If INCLUDEPATH is the way because I must repeat in HEADERS? That seems redundant.

                          1 Reply Last reply
                          0
                          • Chris KawaC Offline
                            Chris KawaC Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on last edited by
                            #14

                            The HEADERS says which files belong to your project.
                            The INCLUDEPATH says where are any dependent headers located.

                            As an example - if you use boost in your project you usually don't want to see it in your project tree. You just want the compiler to know where to look for it when you say #include <boost/any.hpp>. That's what the INCLUDEPATH is for.

                            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