An alias or command to compile and link all C files

I recently started compiling / linking my C files manually using the gcc command. However, this requires that all source files be printed at the end of the command. When there are many files to compile / link, it can be boring. That's why I had the idea of โ€‹โ€‹creating a bash alias for a command that would directly type all * .h and * .c files in a folder.

My line in .bashrc is this:

alias compile = 'ls * .c * .h | gcc -o main '

I found that it worked several times, but most of the compilation time will return this:

gcc: fatal error: no input files

compilation completed.

I thought the pipe would give the results of ls * .c * .h as gcc arguments, but it seems like it is not. What am I doing wrong? Is there a better way to achieve the same?

thanks for the help

+5
source share
3 answers

The pipe does not create command line arguments. Channel feeds standard input.

You need xargs convert standard input to command line arguments.

But you do not need (or want) xargs or ls or standard input here at all.

If you just want to compile each .c file into your own executable, just use:

 gcc -o main *.c 

(Normally, you do not need .h files on the gcc command line.)

As Kay points out in the comments, the pedantically correct and safe version of the aforementioned command (and I don't mean to derogate it):

 gcc -o main ./*.c 

See File names and path names in the shell: how to do this correctly for a detailed discussion of various problems here.

Having said that, you can use any of several tools to save you from having to do this and from having to rebuild everything when only a few things change.

Tools like make or its many clones, front-end (like autotools, cmake) or replacements (tup, scons, cons, and about a million other tools).

+5
source

There are a few misconceptions here:

  • the pipe redirects the standard output of the first command to the standard input of the second command; however, gcc does not accept files for compilation on stdin, but on the command line;
  • the substitution syntax is not something magical only for ls , it is a shell that performs their expansion on the command line;
  • header files should not compile - you are compiling .c files, which in turn may include headers.

Armed with this knowledge, you will realize that the right team is something like

 gcc -o main *.c 

In fact, we can do better: first of all, you will want to change *.c to ./*.c ; this prevents the use of files whose name begins with - , which are interpreted as command-line options.

Most importantly, you should really enable compiler warnings, they can be lifesavers. You need to add -Wall and -Wextra .

 gcc -Wall -Wextra -o main ./*.c 

Finally, it is worth saying that by default you compile with optimizations disabled. If you are debugging this OK, but you also want to add -g so that the executable can be used for debugging; otherwise, if the target is equal to speed, you should at least add -O2 .

+4
source

Have you tried using makefile ? It seems like this might be more effective for what you are trying to do.

If you really want to do this using BASH aliases, you should use xargs to get the standard input for command line arguments.

+3
source

Source: https://habr.com/ru/post/1235570/


All Articles