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.
-
Hi,
What OS are you building on ?
-
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 -
- Click on "Projects" icon on sidebar
- See the "Build Steps" in the "Build Settings"
- Click "Add Build Step" button
- Select "Custom build step"
- In the "Commands" field enter your copy command. Command only without args.
- In "Arguments" field enter %{sourceDir}/YourFile %{buildDir}
- 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.
-
-
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' -
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".