How to include a existing file into a different project?



  • Right clicking on the project and selecting a file from another project I know.
    What happens after the file is there that eludes me.
    How can the selected file be assimilated into this project?


  • Moderators

    What happens after the file is there that eludes me.

    Nothing extraordinary happens. All files are added the same in the .pro file. The one will just have a path outside of your project directory, e.g.

    SOURCES += main.cpp \
               some_file_in_your_project_dir.cpp \
               ../../../some_other_project/file_from_that_other_project_dir.cpp
    

    To compiler it makes no difference where the file is.

    How can the selected file be assimilated into this project?

    What exactly do you mean by "assimilated "?



  • @Chris-Kawa
    The file is included but it is fully qualified.
    Ex: C:/a/b/file

    Should I copy it into the receiving project and then add it?


  • Moderators

    It depends what you want to do.

    • You can leave the absolute path. It's a bad idea in general and of course will not work on other computers.
    • You can change the path to be relative to the project directory e.g. if file is in directory C:/a/b/ and your project is in C:/a/c/ then you can use ../c/file. This is a good idea if you can ensure that this other project will always be located in the same relative directory to yours. This is usually the case when you're working with subprojects.
    • You can copy the file to your project dir and include it. This basically means your project now has no ties to the other project. You have two copies of the file and any changes to one will have no reflection in the other. This is basically a fork and you'll have to manually keep changes in sync if you need to.

    Your choice.



  • @Chris-Kawa
    Thank you very much for your helpfulness Chris, your da-man!


Log in to reply
 

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