在Linux操作系统中,编译多线程程序可以使用gcc命令。下面是一个简单的示例:
“`shell
gcc -o myprogram myprogram.c -lpthread
“`
上述命令中,`myprogram`是生成的可执行文件的名称,`myprogram.c`是源代码文件的名称。
`-lpthread`选项用于链接线程库,确保可执行文件可以正确运行多线程程序。
另外,需要注意的是,编写多线程程序时,需要包含`
下面是一个简单的示例代码:
“`c void* thread_func(void* arg) { int main() { 使用上述命令和代码,可以编译并运行多线程程序。
#include
#include
printf(“Hello from thread!\n”);
return NULL;
}
pthread_t thread;
pthread_create(&thread, NULL, thread_func, NULL);
pthread_join(thread, NULL);
return 0;
}
“`