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

q0987
  • 34,938
  • 69
  • 242
  • 387
  • What are you trying to ask? Those two makefiles will cause different commands to get run. – Carl Norum Jul 05 '11 at 18:37
  • 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. – q0987 Jul 05 '11 at 18:41
  • 2
    possible duplicate of [gcc: Do I need -D_REENTRANT with pthreads?](http://stackoverflow.com/questions/875789/gcc-do-i-need-d-reentrant-with-pthreads) – Nemo Jul 05 '11 at 18:41
  • 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. – Carl Norum Jul 05 '11 at 18:44

4 Answers4

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.

Community
  • 1
  • 1
JohnKlehm
  • 2,368
  • 15
  • 9
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.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Do you think both CFLAGS=-D_REENTRANT and LDFLAGS=-lpthread are not necessary? – q0987 Jul 05 '11 at 18:38
  • 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. – q0987 Jul 05 '11 at 18:43
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.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
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.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • The original makefile compiles all source code under the directory. So I want to avoid running it by using gcc command-line. – q0987 Jul 05 '11 at 18:45
  • @q0987 - but `make` already shows you the command line. Just copy/paste it from the output. – Carl Norum Jul 05 '11 at 18:49