Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to get the path of a .c file which is added as the Resource file



  • Hi,

    I am new to qt. I have created a sample project for accessing and reading the .c file. I have added the .c file in the resource file location but when I am trying to access the file I am unable to access the file. I have tried with below 3 methods but no use. Please help me to solve this. Below is the code which I am using for accessing the C File.

    //Method 1
    QString program("cmd.exe");
    QStringList parameters;
    parameters << " /k" << "gcc " << QUrl("qrc:/hex/hexFile/HV2.c").toString()<<" 1 " <<"test";
    QProcess::startDetached(program, parameters);

    //Method 2

    QProcess process2;
    process2.start("gcc", QStringList() << QUrl("qrc:/hex/hexFile/HV2.c").toString()<<" 1 " <<"test";

    //method 3

    QProcess process3;
    process3.start("gcc", QStringList() << ":/hex/hexFile/HV2.c"<<" 1 " <<"test";

    I am trying to execute the c file with two parameters "1" and "test" but it is not success.


  • Lifetime Qt Champion

    Hi,

    Qt resources are not accessible to outside application like you are trying to do. If you want to compile that file you first have to copy the resource to a known location and then you can pass it to gcc to build.

    Note that this is a pretty strange idea. Why do you want to build a c file embedded in your application ?



  • @SGaist said in How to get the path of a .c file which is added as the Resource file:

    ccessible to outside application like you are trying to d

    Hi,

    My Idea is I am having a Android device. I am creating the android app in QT for browsing the files in the device and while selecting the file I need to call the c file which is having two argument(one is the selected file). I don't know how to do this. I need to show the demo to my Manager regarding this. Can you please help me with some sample code for doing this.



  • @kabdul said in How to get the path of a .c file which is added as the Resource file:

    I need to call the c file which is having two argument(one is the selected file)

    .c files are text files, you can't execute them. I have a feeling you might be talking about compiling an executable from a .c source file, and then executing that as a sub-process?

    You can'r show a demo to your manager without some clear idea of what is going on.



  • @JonB Thank you for your reply. Is it possible to execute a .bin file using the QProcess.



  • @kabdul
    I am not an Android expert/programmer. If the .bin file is an executable file, then in principle, yes, you can use QProcess to execute it as a sub-process. This sounds to me more like what you are wanting to achieve.

    The gcc command you show is for compiling a .c file. You get some executable from that, and that is what you run, passing your arguments. You seem to be confusing those two steps.



  • @JonB How to do that can you please share any sample code for this. I am trying this for more than 2 weeks still now I didn't get any solution for this. Please help me on this.



  • @JonB How to run this bin file. Which I have added in the resource file. Is there any way to access this bin file. And can you please share me some sample code to execute this bin file with some arguments using QProcess. If you share some code it will be big help for me.



  • @kabdul
    You cannot run/execute a file while it is in a Qt resource. It is your job to write code, using QFile to access the resource path, to extract and copy it to an external file somewhere, and then execute that, assuming Android allows that.

    Then

    qDebug() << QProcess::execute("/path/to/extracted/file.bin", QStringList() << "1" << "test");
    

    would be a start.



  • @JonB said in How to get the path of a .c file which is added as the Resource file:

    You cannot run/execute a file while it is in a Qt resource. It is your job to write code, using QFile to access the resource path, to copy it to an external file somewhere, and then execute it, assuming Android allows that.
    Then
    qDebug() << QProcess::execute("/path/to/extracted/file.bin", QStringList() << "1" << "test");

    would be a start.

    Thank You very much for your kindly help. I will try this code and get back to you if I need any help.



  • To strictly address the bare title of this thread, here is code showing how to create an on-disk path for a file from the QRC resources, and then read that file from disk:

      // note the ":" at the front of the path
      std::string path_inside_qrc = ":/data/sim_rpc_replies_immediate_scores.pbtxt";
      std::unique_ptr<QTemporaryFile> temporary_file;
    
      temporary_file.reset(
            QTemporaryFile::createNativeFile(QString::fromStdString(path_inside_qrc))
      );
      std::string file_name = temporary_file->fileName().toUtf8().constData();
      
      // TODO: add code to use the temporary file.
     
      // Delete the temp file after using it
      temporary_file.reset();
    

    This thread caught my eye because once upon a time it took me an hour or two to figure out that the above code solves that problem.

    However, having said that...

    Like everyone else who has responded here, it seems extremely unlikely that invoking gcc from a QProcess on Android is going to bring anyone a firm solution to anything in the end.

    Unless your Android device is comprehensively rooted, I'm 99% sure that the system will not allow your application or service to invoke a compiler. And even if your device is rooted, this seems like a highly advanced topic for someone who appears to be new at Qt, new at Android, and new at gcc. I'm reasonably versed in all 3, and I would not for a minute volunteer to attempt this. (At least, I would require a lot of well-reasoned rationale to be presented first.)


  • Lifetime Qt Champion

    @KH-219Design said in How to get the path of a .c file which is added as the Resource file:

    Like everyone else who has responded here, it seems extremely unlikely that invoking gcc from a QProcess on Android is going to bring anyone a firm solution to anything in the end.

    Which reminds me that they have switched to clang if memory serves well.


Log in to reply