use threads and when compiling do not use -l pthread

1

Hi, I'm doing a program in linux with threads and when I compile I should use gcc .... -l pthread to work my question is how can I do so that I do not have to be putting the pthread always simply compile as gcc -or test test.c

I hope you understand, regards

    
asked by EmiliOrtega 13.06.2017 в 08:30
source

1 answer

1

Use a Makefile eat this:

CC=gcc
CFLAGS=-g
LDFLAGS=-g -pthread
DEPS = func.h
OBJ = main.o func.o 

%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS

miprog: $(OBJ)
    gcc -o $@ $^ $(LDFLAGS)

The previous makefile assumes that you have three files:
main.c
func.c
func.h

If you have more or fewer files, adjust it appropriately.
The .h have to put them in DEPS .
For each .c you have to put a .o in OBJ since they are first compiled and then linked. In CFLAGS put the options you use to compile. For example -g if you do debugging.
In LDFLAGS you set the linking options. In this case -lpthread.
And in CC you put the compiler that you use.

Put this file called Makefile in the same directory as your source code. And to compile it, just execute:

make
    
answered by 13.06.2017 / 08:52
source