Finished pthreads article

Joe Nelson joe at begriffs.com
Sun Apr 26 17:11:30 UTC 2020


Joe Nelson wrote:

> Nicholas Drozd wrote:
> > - I was unable to build life.c on Linux.
> >   -- undefined reference to `pthread_barrier_wait'
> >   -- undefined reference to `pthread_barrier_init'
> >   -- undefined reference to `pthread_barrier_destroy'
> >   Similar compilation errors on Mac.
> 
> Interesting, I wonder if this is an instance of gcc being a special
> snowflake. Or maybe my Makefile isn't as portable as it should be.
> 
> Doing a quick search, I see people say that gcc uses the -pthread
> option rather than the linker directive -lpthread.

Update: I was previously going down some pretty complicated paths trying
to see why -lpthread wasn't working with gcc. Turns out that I just
needed to put -lpthread after the source file inputs to gcc, not before
them. It's the LDFLAGS/LDLIBS distinction that Chris Wellons and I were
talking about in another thread.

This makefile should work for the examples in my article:

	.POSIX:
	CFLAGS = -std=c99 -pedantic -D_POSIX_C_SOURCE=200809L -Wall -Wextra
	LDLIBS = -lpthread

	.c:
		$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)

I also updated the article. Thanks again for pointing out the build failure.

Oh and the gcc-specific `-pthread` option for CFLAGS isn't needed
anymore. Its purpose was to add -lpthread and the glibc symbol
-D_REENTRANT, but the latter is deprecated
https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html#index-_005fREENTRANT


More information about the Friends mailing list