Make C compilation a little easier

Compile C

You can easily do it with gcc.

compile


$ gcc hoge.c -o hoge

Probably, the file name after compilation is the original file.c without the extension .c.

The format has already been decided. It is possible to change the file name at all, but even if you change the file name after compilation, it is only difficult to understand, and you do not bother to do that.


Then, isn't it annoying to write gcc hoge.c -o hoge?

Let's compile with one command

So, let's compile with one command. If you don't have a command, you can make it. To mess with it, create a shell script under ~ / .bash_profile in your home directory or /etc/profile.d. I think this depends on whether you use it as one user of the server or make it available to all users.

If you want to use it as one user, add the following to ~ / .bash_profile. To make it available to all users, create a suitable compile.sh under /etc/profile.d and enter the following there.

compile(){
    out=`echo $1 | awk -F '.' '{print $1}'`
    gcc $1 -o ${out}
}

I will explain it for the time being. $ 1 is an argument. It's the original C file. Multiply it by ʻawk. The delimiter recognition is set to . (Dot), and the first field is extracted. If the file name is hoge.c, the first field will be hogeand the second will bec. Put the extracted hoge in the variable ʻout. After that, the gcc command outputs the original argument hoge.c with the hoge extracted earlier.

After entering, please execute the following.

$ source /etc/profile

Now you can use your own command named compile.

$ com       ←[Tab]Press the key twice
combinedeltarpm      command              compile              complete-ant-cmd.pl
comm                 compgen              complete             compopt

You can see that compile appears as a candidate.

All you have to do now is compile.

$ ls
test.c
$ compile test.c
$ ls
test  test.c

Only this. By the way, I'm not familiar with the gcc command. Please let me know if there is something like "Can you do it with gcc alone?" Or "Is there a command like this?"

Recommended Posts

Make C compilation a little easier
Let's make dependency management with pip a little easier
2. Make import easier
Introducing AE scripts that may make sound matching in C4D a little easier
Make a squash game
Try to make a Python module in C language
Make a function decorator
Make a breakpoint on the c layer with python
Make a distance matrix
I'll make a password!
Make a Nyan button
Make a Tetris-style game!
Make a Base64 decoder
Add a list of numpy library functions little by little --c
Let's make a Discord Bot.
A memorandum of kernel compilation
Make a Blueqat backend ~ Part 2
ABC129 A, B, C commentary
[Django] Make a pull-down menu
Make a LINE BOT (chat)
Make a bookmarklet in Python
Make a fortune with Python
Make Responder a daemon (service)
A little more about FIFO
Let's make a rock-paper-scissors game
A little stuck with chainer
Make a fire with kdeplot
Make a math drill print
Write processing time measurement a little easier using the with clause
[C language] How to create, avoid, and make a zombie process