1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

この記事誰得? 私しか得しないニッチな技術で記事投稿!

Command to compile and specify the name of the exe file simultaneously (C language) (Windows):

Posted at

Why I made this

I guess it depends on your environment tho, everytime I compile a c file with gcc, an execute file named a.exe is generated. This annoys me a lot. The name doesn't tell me what the execute file is for.
(If you use command option flag "-o", this problem would be solved tho)

When multiple c files are in a directry, it makes it even worse. For example, if you compile source1.c first and then compile source2.c, the executable file of source1.c will be overwritten by the executable file of source2.c.

but I don't wanna rename the file everytime I compile...

the entire code is at the end

Main part

create a command "mygcc" to compile a c file and rename a.exe simultaneously
The flowchart of this process is simple like below

I want make a command line mygcc hoge1.c hoge2 to execute the entire process at once.

The code I'm introducing you assumes that a.exe is generated when you use the gcc command(for Windows users).
For Mac or Linux users, a.out would be genrated.

when the argument count is 3

Specify which c file to compile using argv[1]
Specify a name of an excecutable file with argv[2]

    if (argc == 3) {
        // Compile the C source file
        char compileCommand[100];
        snprintf(compileCommand, sizeof(compileCommand), "gcc %s", argv[1]);
        system(compileCommand);

        // Rename the compiled output to the desired executable filename
        char moveCommand[100];
        snprintf(moveCommand, sizeof(moveCommand), "move a.exe %s.exe", argv[2]);
        system(moveCommand);

        printf("A file named %s.exe is created\n", argv[2]);
    }

I used the move command to rename the file tho you might need to use mv,ren or rename depending on your environment, I guess.

In this code, the original C file is specified with the extension .c, while the executable file is specified only up to .exe. This is to specify multiple C files using wildcards like ho*.c, and to avoid potential errors when there are files with similar names but different extensions in the same directory, such as "hoge1.c" and "hoge1.html"

when the argument count is 2

Specify which C file to compile first, then name the executable file.
It is is for when you accidentally missed argv[3] when running the command.

else if (argc == 2) {
        char exefilename[100];
        puts("Decide filename");
        scanf("%s", exefilename);

        // Compile the C source file
        char compileCommand[100];
        snprintf(compileCommand, sizeof(compileCommand), "gcc %s", argv[1]);
        system(compileCommand);

        // Rename the compiled output to the user-specified executable filename
        char moveCommand[100];
        snprintf(moveCommand, sizeof(moveCommand), "move a.exe %s.exe", exefilename);
        system(moveCommand);

        printf("A file named %s.exe is created\n", exefilename);
    }

The entire code, including standard phrases, is as follows:
The entire code

mygcc.c
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[]){
    if (argc == 3) {
        // Compile the C source file
        char compileCommand[100];
        snprintf(compileCommand, sizeof(compileCommand), "gcc %s", argv[1]);
        system(compileCommand);

        // Rename the compiled output to the desired executable filename
        char moveCommand[100];
        snprintf(moveCommand, sizeof(moveCommand), "move a.exe %s.exe", argv[2]);
        system(moveCommand);

        printf("A file named %s.exe is created\n", argv[2]);
    }
    else if (argc == 2) {
        char exefilename[100];
        puts("Decide filename");
        scanf("%s", exefilename);

        // Compile the C source file
        char compileCommand[100];
        snprintf(compileCommand, sizeof(compileCommand), "gcc %s", argv[1]);
        system(compileCommand);

        // Rename the compiled output to the user-specified executable filename
        char moveCommand[100];
        snprintf(moveCommand, sizeof(moveCommand), "move a.exe %s.exe", exefilename);
        system(moveCommand);

        printf("A file named %s.exe is created\n", exefilename);
    }
    else {
        puts("Usage: mygccc target.c exefilename");
    }

    return 0;
}

Even though the process could be done with a simple batch file, I dared to solve with C, cus it is about C compiler. First of all, all I need to do is use gcc -o hoge2.exe hoge1.c tho

Issue

To provide a more helpful error message, instead of falsely stating "A file named hoge.exe is created" when the user mistypes the filename and the compiler failed to compile, it would be more thoughtful to say something like "Compilation failed. Please check the filename."

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?