如何编译多线程code用gcc [英] How to compile the multithread code with gcc

查看:265
本文介绍了如何编译多线程code用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

Quote:

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

这篇关于如何编译多线程code用gcc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆