How to compile the multithread code with gcc

后端 未结 4 1426
耶瑟儿~
耶瑟儿~ 2021-01-16 06:43

I have seen the given two makefiles as follows:

all: thread1 thread2 thread3

CFLAGS=-I/usr/include/nptl -D_REENTRANT
LDFLAGS=-L/usr/lib/nptl -lpthread

clea         


        
相关标签:
4条回答
  • 2021-01-16 06:59

    If your code don't have external dependencies beyond pthread:

    gcc thread1.c -o thread1 -D_REENTRANT -lpthread
    

    Quote:

    Defining _REENTRANT causes the compiler to use thread safe (i.e. re-entrant) versions of several functions in the C library.

    0 讨论(0)
  • 2021-01-16 07:07

    Those two makefiles will generate two different sets of command-line arguments. You could check it yourself just by running make:

    $ make -f makefile1
    cc -I/usr/include/nptl -D_REENTRANT  -L/usr/lib/nptl -lpthread  thread1.c   -o thread1
    $ make -f makefile2
    cc -D_REENTRANT  -lpthread  thread1.c   -o thread1
    

    Choose your favourite.

    0 讨论(0)
  • 2021-01-16 07:13

    Almost:

    gcc -o thread1 -I/usr/include/nptl -D_REENTRANT -L/usr/lib/nptl thread1.c -lpthread
    

    The *FLAGS variables contain the arguments that are passed to the compiler and linker invocartion, respectively. (In your case you're compiling and linking in one go.) Make sure to add libraries after your own object files.

    0 讨论(0)
  • 2021-01-16 07:23

    Your question is answered here

    gcc: Do I need -D_REENTRANT with pthreads?

    Essentially all you need is

    gcc thread1.c -o thread1 -pthread

    and gcc will handle all the defines for you.

    0 讨论(0)
提交回复
热议问题