日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

【Linux】编译C语言文件(-o -lpthread)

發布時間:2023/11/30 linux 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Linux】编译C语言文件(-o -lpthread) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在gcc中使用-o編譯

對于一個一般的程序,直接使用gcc <C語言文件名> -o <編譯后生成的文件名>即可,例如以下程序:

// cpu.c

#include <stdio.h> #include <unistd.h> #include <stdlib.h>int main(int argc,int *argv[]){if(argc != 2){fprintf(stderr,"need parameter\n");exit(1);}char *str = argv[1];for(int i = 0;i < 4;i++){printf("%s\n",str);sleep(1);}return 0; }

編譯命令:gcc cpu.c -o cpu

(這個警告不重要)之后就會生成可執行文件cpu,我們可以使用./cpu運行它。

額外參數 -lpthread

對于含有<pthread.h>的程序,例如下面的:

// threads.c

#include <stdio.h> #include <stdlib.h> #include <pthread.h>volatile int counter = 0; int loops;void *worker(void *arg) {int i;for (i = 0; i < loops; i++) {counter++;}return NULL; }int main(int argc, char *argv[]) {if (argc != 2) {fprintf(stderr, "usage: threads <value>\n");exit(1);}loops = atoi(argv[1]);pthread_t p1, p2;printf("Initial value : %d\n", counter);pthread_create(&p1, NULL, worker, NULL);pthread_create(&p2, NULL, worker, NULL);pthread_join(p1, NULL);pthread_join(p2, NULL);printf("Final value : %d\n", counter);return 0; }

在編譯的時候需要加上額外的參數-lpthread,因為該頭文件在Linux默認Import Library中沒有,需要使用庫libpthread.a進行編譯鏈接。

命令gcc threads.c -o threads -lpthread


然后會生成可執行文件threads,使用./threads運行即可。

總結

以上是生活随笔為你收集整理的【Linux】编译C语言文件(-o -lpthread)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。