How do I get a text file to copy into a build?



  • Ive added a text file to my Qt Project using Add New > General > Text File. I want that text file to get copied to the output directory when I build the project so I can quit having to manually copy after the build. I can't, for the life of me, figure out how to do this "seemingly" simple task.

    When I added the text file to the project, I saw that QT Creator added a

    OTHER_FILES += \
    MyFile.txt
    (both the above are on separate lines)
    to the .pro file but I see no other way to indicate that the file should get copied with the build. I'm not looking to compile the text file into the executable. Instead, I want that file to be writable outside of the program so I can make changes and then the program will read it in at runtime.

    Would love any help I can get. Thanks in advance.



  • I thought the QT Resource System compiled the files into the executable? If that is the case, then no. I simply want he text file to get copied into the build folder when I build. Thanks


  • Lifetime Qt Champion

    Hi,

    What OS are you building on ?



  • Windows 7



  • If you are using QtCreator then you can add one more build step on the "Projects" tab.
    This step will be written into .pro.user file and will be OS specific.

    If you are going to build your project manually by run qmake and make then you may need to run make install after successful build.
    "Take a look here":http://stackoverflow.com/questions/7795397/copy-a-file-to-the-build-directory-after-compiling-project-with-qt



  • Thank you for your reply andreyc. Can you provide info on exactly what/how to add the build step?



    1. Click on "Projects" icon on sidebar
    2. See the "Build Steps" in the "Build Settings"
    3. Click "Add Build Step" button
    4. Select "Custom build step"
    5. In the "Commands" field enter your copy command. Command only without args.
    6. In "Arguments" field enter %{sourceDir}/YourFile %{buildDir}
    7. Go back to "Edit" and try to build your project

    "Here is more info":https://qt-project.org/doc/qtcreator-3.0/creator-build-settings.html

    PS: I use Linux, so I did not try it to run on Windows. YMMV.



  • Thats great! Now we are making progress. I tried to enter "copy" in the commands field but I got an error: The process copy exited with code of -1.

    What is the correct copy command?

    Sorry for all the trouble... just needing to learn this fast and cant find any good explanations on Google or StackOverflow or in the qt forum and have been searching for days.



  • I don't have an access to windows machine right now and don't remember the format of the copy command.
    You can try to run copy /? in command prompt to see the correct format.

    Could you show the full log message that you get in QtCreator on this error?



  • Wait... are we talking about the OS copy command? Or something in QT Creator?
    Yeah, Ill post



  • 11:38:39: Starting: "COPY" C:\WORKSPACE\MyProject\MyFile.json C:\WORKSPACE\build-MyProject-Desktop_Qt_5_2_1_MSVC2012_OpenGL_64bit-Debug
    qtcreator_ctrlc_stub: Command line failed: COPY C:\WORKSPACE\MyProject\MyFile.json C:\WORKSPACE\build-MyProject-Desktop_Qt_5_2_1_MSVC2012_OpenGL_64bit-Debug
    11:38:39: The process "COPY" exited with code -1.
    Error while building/deploying project MyProject (kit: Desktop Qt 5.2.1 MSVC2012 OpenGL 64bit)
    When executing step 'Custom Process Step'



  • I ran the command directly on the command line and it worked just fine but not from QT Creator



  • Do you have this file and this directory in place during build?
    Another option is try to run dir instead of copy just to make sure that QtCreator sees the file and the directory the same way as you see them when you run command manually.

    And yes these are OS commands.
    You can create a script and put in your project directory and run it.
    You will need to put full path to the script in the "Commands" field.



  • Yes sir, everything is in place. When I run the copy form the command line it performs the copy. Do I need to setup something in QT Creator that tells it where the system OS commands are located? I see in Options > Environment >External tools but not sure what would need to go there. If I click Add >Tool it puts in a New Uncategorized tool with the executable cmd and arguments /c echo Useful text. Is this what QTC is missing? Some reference to where the COPY command is located?



  • No you don't need to change the "External Tools"
    Did you try to run dir command instead of copy?
    I think copy is not located anywhere it is just part of cmd.exe
    Which makes me think that maybe you should call cmd.exe with some options that will allow to execute copy.
    Try cmd.exe /? and see if it has an option to run command from command line.
    Something like
    @
    cmd.exe /c copy src dst
    @

    If it will work in command prompt then put cmd.exe into "Command" field and "/c copy src dst" into "Arguments" field in your "Custom build step".


Log in to reply
 

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