Search code examples
cexecution

C Functions not executing in order


I know there may be questions similar to this, its just that they are in C++, and I don't know if they are the same. I have some code

void BuildApp(char *AppName)
{
        char *cmd;
        cmd = combine("mkdir ./Projects/", AppName);
        cmd = combine(cmd, "/Package/");
        // Make the package dir.
        system(cmd);
        cmd = "";
        cmd = combine("mkdir ./Projects/", AppName);
        cmd = combine(cmd, "/Package/DEBIAN");
        system(cmd);
        cmd = "";
        cmd = combine("mkdir ./Projects/", AppName);
        cmd = combine(cmd, "/Package/Applications");
        system(cmd);
        cmd = "";
        cmd = combine("mkdir ./Projects/", AppName);
        cmd = combine(cmd, "/Package/Applications/");
        cmd = combine(cmd, AppName);
        cmd = combine(cmd, ".app");
        system(cmd);
        cmd = "";
        cmd = combine("mkdir ./Projects/", AppName);
        cmd = combine(cmd, "/Package/Applications/");
        cmd = combine(cmd, AppName);
        cmd = combine(cmd, ".app/Inc");
        system(cmd);
        cmd = "";
        cmd = combine("cp ./Projects/", AppName);
        cmd = combine(cmd, "/Assets/app.icon.png ./Projects/");
        cmd = combine(cmd, AppName);
        cmd = combine(cmd, "/Package/Applications/");
        cmd = combine(cmd, AppName);
        cmd = combine(cmd, ".app/Icon.png");
        system(cmd);
        printf("Building application...");
        cmd = "";
        cmd = combine("cd ./Projects/", AppName);
        system(cmd);
        printf(cmd);
        cmd = "";
        cmd = combine("gcc App.c -o ", AppName);
        printf(cmd);system(cmd);
        system(cmd);
}

but it appears to be executing out of order. This is being run on Linux (Actually, cygwin), and compiled with GCC. For some reason, the system(); function that should be executing the GCC compile action is executing before the cd command, giving me this output:

gcc: App.c: No such file or directory
gcc: no input files
Building application...
cd ./Projects/Sample

But, after the cd statement (which I printed to check the order they where getting executed in), the GCC compile command isn't executing, leaving me with an uncompiled App.c, and errors on program execution. Any help as to why this is happening would be appreciated.


Solution

  • You have two "issues":

    • The output you're seeing is both from stderr (the GCC error message) and stdout (the rest of it). You're inferring that the order in which these messages appear on your terminal is the same as the order the code that generated them ran - you can't rely on that. stdout is usually buffered, while stderr generally isn't, so the order in which they appear on your screen isn't an indicator of the order of execution of your code.
    • The system("cd whatever"); call is a bug. It spawns a new shell, that shell changes directories and... promptly exists. It has no side-effect whatsoever on the parent process or the subsequent system calls. You need to use chdir in the parent process, or do the cd and gcc in the same system call.