首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在Linux/gcc上创建与pthreads和libstdc ++静态链接的共享对象?

在Linux/gcc上创建与pthreads和libstdc++静态链接的共享对象,可以按照以下步骤进行:

  1. 编写源代码文件,例如example.cpp
代码语言:cpp
复制
#include<iostream>
#include <pthread.h>
#include <unistd.h>

using namespace std;

void* printHello(void* arg) {
    cout << "Hello from pthread!"<< endl;
    return NULL;
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, printHello, NULL);
    pthread_join(thread, NULL);
    return 0;
}
  1. 使用-c选项编译源代码文件为目标文件:
代码语言:bash
复制
g++ -c example.cpp -o example.o -pthread -static-libstdc++

其中,-c选项表示只编译源代码文件,不进行链接;-o选项表示指定输出文件名;-pthread选项表示链接pthreads库;-static-libstdc++选项表示静态链接libstdc++库。

  1. 使用-shared选项将目标文件链接为共享对象:
代码语言:bash
复制
g++ -shared example.o -o example.so -pthread -static-libstdc++

其中,-shared选项表示生成共享对象文件;-o选项表示指定输出文件名。

完成以上步骤后,example.so即为与pthreads和libstdc++静态链接的共享对象。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券