如何编译多线程code用gcc [英] How to compile the multithread code with gcc
问题描述
我见过的给出了两个makefile文件如下:
所有:线程1线程2 thread3CFLAGS = -I / usr / include目录/ NPTL -D_REENTRANT
LDFLAGS = -L / usr / lib目录/ NPTL -lpthread清洁:
RM -f线程1线程2 thread3######################所有:线程1线程2 thread3CFLAGS = -D_REENTRANT
LDFLAGS = -lpthread清洁:
RM -f线程1线程2 thread3
如果不使用生成文件,有什么正确的命令行编译使用gcc的thread1.c是什么?
GCC -o线程1
CFLAGS = -I / usr / include目录/ NPTL
-D_REENTRANT LDFLAGS = -L / usr / lib目录/ NPTL -lpthread thread1.c
块引用>解决方案如果您code没有外部依赖的pthread之外:
的gcc -o thread1.c -D_REENTRANT线程1 -lpthread
<一个href=\"http://stackoverflow.com/questions/2601753/reentrant-flag-in-pthreads/2602822#2602822\">Quote:
定义_REENTRANT导致编译器使用线程安全的(即重入)的多种功能版本的C库。
块引用>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
解决方案If your code don't have external dependencies beyond pthread:
gcc thread1.c -o thread1 -D_REENTRANT -lpthread
Defining _REENTRANT causes the compiler to use thread safe (i.e. re-entrant) versions of several functions in the C library.
这篇关于如何编译多线程code用gcc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!