如何在gcc中使用线程编译C程序? [英] How to compile C program with threads in gcc?
问题描述
我需要与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屋!