How to use headers from another project?



  • How do I use a class that was created in another project?

    I'm not using Qt Creator.


  • Moderators

    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.



  • 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?


  • Moderators

    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
    @



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



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


  • Moderators

    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.



  • 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



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


  • Moderators

    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.



  • [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 (.)


  • Moderators

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



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


  • Moderators

    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.


Log in to reply
 

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