Multithreaded Programming Guide

Compiling and Linking in the POSIX Threads Environment

If your application uses only Pthreads or uses both Solaris threads and Pthreads, use the following command to compile and link:

cc -mt [ flag ... ] file... [ library... ] -lpthread 

The -mt option links in the libthread library, while the -lpthread option links in the libpthread library. Both flags are needed when using Pthreads because libpthread provides an interface to libthread.

The -mt option can appear anywhere in the command line. The -lpthread option should come after any user libraries. The relative positions of -mt and -lpthread do not matter.

For example, the following lines are equivalent:

cc -mt -o myprog f1.o f2.o   -lmylib -lpthread
cc     -o myprog f1.o f2.o -mt -lmylib -lpthread
cc     -o myprog f1.o f2.o -lmylib -mt -lpthread
cc     -o myprog f1.o f2.o -lmylib -lpthread -mt

See the Sun Studio cc(1) man page and Sun Studio 12: C User’s Guide for more information about the cc command options.