How to embed an executable file (which will be executed at runtime) in a Qt program?

I am writing a cross-platform program in C ++ using Qt , and I want to pack / paste several executables in binary program format. The program should be able to execute these binaries at runtime.

I figured I would need QResource and QProcess using start () and the notation ": / ...", but it seems to me that the process is not working. Is there something I can't see? Should this work like that? Should the binary be installed as an executable?

Reference Information. I am writing a tool that uses Git , and I do not want to require the end user to install Git manually.

(Trying to do this on Mac OS X, BTW.)

Update:

I use the following code (C ++, Qt on Mac OS X):

QString program = ":/git"; QStringList arguments; arguments << "help" << "commit"; myProcess->start(program, arguments); 

Executable Git on the project path, my .qrc resources reference it like this:

 <qresource prefix="/"> <file>git</file> </qresource> 

I do not receive an error message, but the program is not running. The behavior is the same when I install program in a nonexistent file. Replacing ":/git" with an absolute path to Git works fine.

+4
source share
4 answers

You cannot execute the program directly from the resource.

If you had a program on a resource and you wanted to execute it, first you will need to read it from the resource, write to a file, make an executable file and execute it.

Also, when you say that you are not getting an error, it probably means that you are not checking errors correctly.

+8
source

I do not think that resources will work. Processes are created by the operating system, and resources are processed by the application.

One possible solution would be to combine additional executables somewhere in your application directory and execute them from that path.

+1
source

A few years later, but the question still matters. I had the same problem when you wanted to insert rclone .

In the .pro file add

 # From http://stackoverflow.com/a/37561981 defineReplace(copyToDir) { files = $$1 DIR = $$2 LINK = for(FILE, files) { LINK += $$QMAKE_COPY $$shell_path($$FILE) $$shell_path($$DIR) $$escape_expand(\\n\\t) } return($$LINK) } defineReplace(copyToBuilddir) { return($$copyToDir($$1, $$OUT_PWD)) } # Copy the binary files dependent on the system architecture win32 { message("Windows") QMAKE_POST_LINK += $$copyToBuilddir($$PWD/rclone/windows/rclone.exe) }else: unix:!macx { message("Linux") QMAKE_POST_LINK += $$copyToBuilddir($$PWD/rclone/linux/rclone) }else: macx: { # Here we want to place the binaries inside the application bundle, so the # QMAKE_POST_LINK approach will not work because it places them in the same # directory as the bundle and not inside it. Instead, use QMAKE_BUNDLE_DATA. message("macOS") MediaFiles.files += $$PWD/rclone/macOS/rclone MediaFiles.path = Contents/MacOS QMAKE_BUNDLE_DATA += MediaFiles } 

Please note that for each platform there are slight differences, but in general the approach is the same.

qmake will copy these files to the target directory, and they will be available, just calling the process in the local relative directory.

In the following code, I call the executable from QML, and it will also be very similar to C ++:

 var rcloneCommand if (Qt.platform.os === "windows") { console.log("Windows") rcloneCommand = "rclone.exe" } else { console.log("OSX/Linux") rcloneCommand = "./rclone" } qProcess.start(rcloneCommand, ["--config", "rclone.conf", "-v", "copy", "--stats", "1s", source, destination]); 
+1
source

So the problem is not to extract the git executable from the resource in the same way as it does?

The git program correctly generates a don disk, can you check its permissions?

0
source

All Articles