How to compile the multithread code with gcc
Asked Answered
L

4

1

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

clean:
    rm -f thread1 thread2 thread3

######################

all: thread1 thread2 thread3

CFLAGS=-D_REENTRANT
LDFLAGS=-lpthread

clean:
    rm -f thread1 thread2 thread3

Without using makefile, what is the correct command line to compile the thread1.c with gcc?

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

Luna answered 5/7, 2011 at 18:35 Comment(4)
What are you trying to ask? Those two makefiles will cause different commands to get run.Howells
I don't want to use these makefile to compile all the source codes and would like to just compile the code one by one through gcc commandline.Luna
possible duplicate of gcc: Do I need -D_REENTRANT with pthreads?Consideration
But which set of flags do you need to pass? Do you need the -I and -L flags or not? That's not something anyone here can answer for you.Howells
S
2

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.

Situate answered 5/7, 2011 at 18:37 Comment(2)
Do you think both CFLAGS=-D_REENTRANT and LDFLAGS=-lpthread are not necessary?Luna
I think the code uses nptl. From the book "In multithreaded programs, you tell the compiler that you need this feature by defining the _REENTRANT macro before any #include lines in your program." But the attached source code thread1.c doesn't include the _REENTRANT, so I wonder what I should do.Luna
D
4

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.

Determinable answered 5/7, 2011 at 19:7 Comment(0)
S
2

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.

Situate answered 5/7, 2011 at 18:37 Comment(2)
Do you think both CFLAGS=-D_REENTRANT and LDFLAGS=-lpthread are not necessary?Luna
I think the code uses nptl. From the book "In multithreaded programs, you tell the compiler that you need this feature by defining the _REENTRANT macro before any #include lines in your program." But the attached source code thread1.c doesn't include the _REENTRANT, so I wonder what I should do.Luna
N
1

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.

Nicks answered 5/7, 2011 at 18:37 Comment(0)
H
1

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.

Howells answered 5/7, 2011 at 18:41 Comment(2)
The original makefile compiles all source code under the directory. So I want to avoid running it by using gcc command-line.Luna
@Luna - but make already shows you the command line. Just copy/paste it from the output.Howells

© 2022 - 2024 — McMap. All rights reserved.