且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在gcc中使用线程编译C程序?

更新时间:2022-01-24 22:11:38

在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.