如何在gcc中使用线程编译C程序? [英] How to compile C program with threads in gcc?

查看:336
本文介绍了如何在gcc中使用线程编译C程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要与gcc一起使用的哪个选项才能消除错误?

What option do I need to use with gcc in order to get rid of errors ?

推荐答案

在Linux上,您应该首先使用

On Linux, you should first compile with

gcc -g -Wall -pthread yourcode.c -lpthread -o yourprogram

-g询问调试信息,-Wall询问所有警告,-pthread询问对POSIX线程的支持.

-g asks for debug information, -Wall asks for all warnings, -pthread asks for POSIX threads support.

当编译时没有警告,并且您已使用gdb对其进行调试时,您可能希望编译器通过将-g替换为-O2

when it compiles without warnings and you have debugged it with gdb you might want the compiler to optimize by replacing (or adding after) -g with-O2

很快,您将要使您的程序由多个编译单元(链接在一起)组成.然后,您需要学习如何使用GNU make 这样的构建器,并且Makefile(不要忘记标签在其中很重要).

Very soon, you'll want to make your program made of several compilation units (linked together). You then need to learn how to use a builder like GNU make and you'll have a Makefile (don't forget that tabs are significant inside them).

当然,您应该使用版本控制系统(我建议使用 git ,也许通过 github )在您的源代码上.

Of course, you should use a version control system (I suggest to use git, perhaps thru gitorious or github) on your source code.

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

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