首页 > 技术文章 > Visual Studio 2019Linux开发添加动态链接库参数

sgawscd 2020-05-19 15:54 原文

上一篇文章Visual Studio 2019 基于Linux平台的C++开发中介绍了如何配置通过VS进行Linux C++开发的环境。

这一篇主要介绍如何使用libpthread.so这类的动态链接共享库。

如果是在Linux平台,GCC或者g++,想要编译含有例如pthread的代码,需要如下的命令

g++ -o exefile -std=c++0x main.c -lpthread 
#-std=c++0x 开启对c++11标准的支持
#-lpthread 加载动态链接库libpthread.so

但是在visual studio下,该如何加入-lpthread这个参数呢?

把一系列选项看了半天终于发现,在“项目” -> “属性” -> “配置属性” -> “链接器” -> “命令行”中的其他选项中输入-lpthread即可。

为了测试我们操作的正确与否

下面这个例子使用了pthread相关函数

#include<iostream>
#include<unistd.h>
#include<cstdlib>
#include<pthread.h>
#include<vector>
#include<string>
using namespace std;

void* f1(void *)
{
	pthread_detach(pthread_self());//分离自身线程
	string s;
	vector<string> vs({"1.1Java","1.2Android","1.3hadoop","1.4cloud","1.5Design","1.6Patterns"});
	for (auto a : vs)
		cout << a << "\t";
	cout << endl;
	return NULL;
}

void* f2(void *)
{
	vector<string> vs({"2.1C++","2.2Linux","2.3thread","2.4fork","2.5join","2.6time","2.7Lisp"});
	for (auto i = vs.cbegin(); i != vs.cend(); ++i)
		cout << *i <<"\t";
	cout << endl;
	return NULL;
}

int main(int argc, char* argv[])
{
	pthread_t pt1,pt2;
	pthread_create(&pt1, NULL, f1, NULL);
	pthread_create(&pt2, NULL, f2, NULL);
	pthread_join(pt2, NULL);//等待该线程
	return 0;
}

点击绿色箭头或直接按F5,编译运行程序,得到结果如下:

运行成功!

推荐阅读