vs2019 使用Pthreads--并行计算

参考:

https://blog.csdn.net/lhw_666/article/details/108430647
https://blog.csdn.net/weixin_40614261/article/details/89552222

下载
地址:ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip
有以下文件:
Pre-built.2
Pthreads.2
QueueUserAPCEx

配置要点记录:

  1. 在“调试”->“xxxxx属性”中设置参数;
  2. “配置管理器”,选x64;
  3. 链接器 -> 输入 -> 附加依赖项,添加: pthreadVC2.lib;
  4. VC++目录,包含目录添加: xxxx\Pre-built.2\include;
  5. VC++目录,库目录添加: xxxx\Pre-built.2\lib\x64;
  6. 注意分号,路径为解压地址;
  7. 理解:include意思“.h”文件,lib意味“lib文件”;
  8. 复制 pthreadVC2.dllpthreadGC2.dll(来自Pre-built.2的dll文件),粘贴到项目exe生成文件目录下(即"xxxx/x64/debug"),否则“dll”类报错。
  9. 编辑pthread.h文件开头部分,在35行添加: #define HAVE_STRUCT_TIMESPEC ,否则报错。

测试程序,helloword

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS     8

void* PrintHello(void* threadid)
{
	long taskid;
	//sleep(1);
	taskid = *(long*)threadid;
	printf("Hello from thread %ld\n", taskid);
	pthread_exit(NULL);
}

int main(int argc, char* argv[])
{
	pthread_t threads[NUM_THREADS];
	int rc;
	long t;

	for (t = 0;t < NUM_THREADS;t++) {
		printf("Creating thread %ld\n", t);
		rc = pthread_create(&threads[t], NULL, PrintHello, (void*)&t);
		if (rc) {
			printf("ERROR; return code from pthread_create() is %d\n", rc);
			exit(-1);
		}
	}

	pthread_exit(NULL);
}

编译运行项目,出结果。

已标记关键词 清除标记
-- Looking for pthread.h -- Looking for pthread.h - not found -- Found Threads: TRUE -- Found OpenSSL: C:/xmr-stak-dep/opensslbbeay32.lib (found version "1.0.2 l") You have called ADD_LIBRARY for library xmr-stak-c without any source files. This typically indicates a problem with your CMakeLists.txt file CMake Error at CMakeLists.txt:173 (add_executable): add_executable called with incorrect number of arguments CMake Error at CMakeLists.txt:178 (target_link_libraries): Cannot specify link libraries for target "xmr-stak-cpu" which is not built by this project. -- Configuring incomplete, errors occurred! See also "C:/Users/Administrator/sourceild/CMakeFiles/CMakeOutput.log". See also "C:/Users/Administrator/sourceild/CMakeFiles/CMakeError.log" .![图片说明](https://img-ask.csdn.net/upload/201709/07/1504754534_20002.png) CMAKEError.log内容如下: Determining if the include file pthread.h exists failed with the following output: Change Dir: C:/xmr-stak-cpu/CMakeFiles/CMakeTmp Run Build Command:"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional/MSBuild/15.0/Bin/MSBuild.exe" "cmTC_c3038.vcxproj" "/p:Configuration=Debug" "/p:VisualStudioVersion=15.0" 用于 .NET Framework 的 Microsoft (R) 生成引擎版本 15.3.409.57025 版权所有(C) Microsoft Corporation。保留所有权利。 生成启动时间为 2017/9/7 11:08:54。 节点 1 上的项目“C:\xmr-stak-cpu\CMakeFiles\CMakeTmp\cmTC_c3038.vcxproj”(默认目标)。 PrepareForBuild: 正在创建目录“cmTC_c3038.dir\Debug\”。 正在创建目录“C:\xmr-stak-cpu\CMakeFiles\CMakeTmp\Debug\”。 正在创建目录“cmTC_c3038.dir\Debug\cmTC_c3038.tlog\”。 InitializeBuildStatus: 正在创建“cmTC_c3038.dir\Debug\cmTC_c3038.tlog\unsuccessfulbuild”,因为已指定“AlwaysCreate”。 ClCompile: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.11.25503\bin\HostX86\x86\CL.exe /c /Zi /W3 /WX- /diagnostics:classic /Od /Ob0 /Oy- /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"cmTC_c3038.dir\Debug\\" /Fd"cmTC_c3038.dir\Debug\vc141.pdb" /Gd /TC /analyze- /errorReport:queue "C:\xmr-stak-cpu\CMakeFiles\CMakeTmp\CheckIncludeFile.c" 用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.11.25507.1 版 版权所有(C) Microsoft Corporation。保留所有权利。 cl /c /Zi /W3 /WX- /diagnostics:classic /Od /Ob0 /Oy- /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"cmTC_c3038.dir\Debug\\" /Fd"cmTC_c3038.dir\Debug\vc141.pdb" /Gd /TC /analyze- /errorReport:queue "C:\xmr-stak-cpu\CMakeFiles\CMakeTmp\CheckIncludeFile.c" CheckIncludeFile.c C:\xmr-stak-cpu\CMakeFiles\CMakeTmp\CheckIncludeFile.c(1): fatal error C1083: 无法打开包括文件: “pthread.h”: No such file or directory [C:\xmr-stak-cpu\CMakeFiles\CMakeTmp\cmTC_c3038.vcxproj] 已完成生成项目“C:\xmr-stak-cpu\CMakeFiles\CMakeTmp\cmTC_c3038.vcxproj”(默认目标)的操作 - 失败。 生成失败。 “C:\xmr-stak-cpu\CMakeFiles\CMakeTmp\cmTC_c3038.vcxproj”(默认目标) (1) -> (ClCompile 目标) -> C:\xmr-stak-cpu\CMakeFiles\CMakeTmp\CheckIncludeFile.c(1): fatal error C1083: 无法打开包括文件: “pthread.h”: No such file or directory [C:\xmr-stak-cpu\CMakeFiles\CMakeTmp\cmTC_c3038.vcxproj] 0 个警告 1 个错误 已用时间 00:00:00.31 ? ? ? ? ? 哪位有经验的牛哥指点一下呗??? 环境:Win7_64 VS2017 CMAke3.9.1
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页