GCC is not multi-threaded. The -j<n>
switch is specific to make
build system, not the compiler. It tells make
how many tasks it can run in parallel.
If you run make -j4
you can observe in your task manager/top/process list that it tries to run 4 instances of GCC compiling 4 independent *.c
files at the same time.
To make use of -j
command you must have a Makefile
in your project that can benefit from it. It should have multiple independent targets, so that they can be launched in parallel.
If you are lost in the terminology, I advice you to look at make
tutorial, such as this one:
http://mrbook.org/tutorials/make/
The usual strategy here is to have a separate target for every c
or cpp
file in our project. That way make can easily spawn multiple compiler processes for each compilation unit. Once all *.o files are generated, they are linked.
Let's see at this example snippet:
SRCS := main.c func.c other.c another_file.c ...
OBJS := $(SRCS:.c=.o)
objects: $(OBJS)
%.o: %.c
gcc -o $(@) -c $(<)
We pass a list of c
files, change them to corresponding o
file using suffix substitution and treat the list of *.o
files as targets. Now the make
can compile each c
file in parallel.
In contrast, if we do something like this:
SRCS := main.c func.c other.c another_file.c ...
all:
gcc $(SRCS) -o a.out
...we won't benefit from -j
switch at all, because there is only one target.