Visual Studio 2019Linux開發添加動態鏈接庫參數


上一篇文章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,編譯運行程序,得到結果如下:

運行成功!🍺


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM