用mingw在windows上使用openmp。找不到-lpthread [英] Using openmp on windows with mingw. Cannot find -lpthread

查看:1537
本文介绍了用mingw在windows上使用openmp。找不到-lpthread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用OpenMP的CMake项目,可以在linux上运行。
当我在Windows机器上编译它时,它看起来像CMake无法找到mingw的gcc的openmp标志。



我决定尝试一个更小的测试用例,然后编译main_openmp.c

  #include  
#include< stdio.h>
int main(int argc,char * argv []){
int id;
#pragma omp parallel private(id)
{
id = omp_get_thread_num();
printf(%d:Hello World!\\\
,id);
}
返回0;
}

然后当我尝试编译时

  gcc -o OpenMPTest2 main_testomp.c -fopenmp 

我得到

 >>> gcc -o OpenMPTest2 main_testomp.c -fopenmp 
c:/ mingw / bin /../ lib / gcc / mingw32 / 4.8.1 /../../../../ mingw32 / bin / ld。 exe:无法找到-lpthread
collect2.exe:错误:ld返回1退出状态

我试图在网上找到解决方案,并尝试使用-lgomp,-lpthreadgc2和-lpthreadvc2的变体,但没有任何改进。



我搜索了我的C:\MinGw目录,以递归方式查找包含lpthread的文件名,并得到了这个结果:

  C:\MinGW\bin\pthreadgc2.dll 
C:\MinGW\bin\pthreadgce2.dll
C:\MinGW\var \cache\mingw-get\packages\pthreads-w32-2.9.1-1-mingw32-dl​​l.tar.lzma
C:\MinGW\var\lib\mingw-get \data\mingw32-pthreads-w32.xml

我不确定我是否错过了一面旗帜或一个包裹,或者我做错了什么。这里的好办法是输出gcc -v

 使用内置规格。 
COLLECT_GCC = gcc
COLLECT_LTO_WRAPPER = c:/ mingw / bin /../ libexec / gcc / mingw32 / 4.8.1 / lto-wrapper.exe
目标:mingw32
已配置与:../gcc-4.8.1/configure --prefix = / mingw --host = mingw32 --build = mingw32 --without-pic
--enable-shared --enable-static --with -gnu-ld --enable -lto --enable-libssp --disable-multilib --ena
ble-languages = c,c ++,fortran,objc,obj-c ++,ada --disable -sjlj-exceptions --with-dwarf2 --disable-win32
-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-gmp = / usr / src / pkg / gm
p-5.1.2-1-mingw32-src / bld --with-mpc = / usr / src / pkg / mpc-1.0.1-1-mingw32-src / bld --with-mpfr = --withwith -sy
stem-zlib --with-gnu-as --enable-decimal-float = yes --enable-libgomp --enable-threads --with-libiconv
-prefix = / mingw32 - -with-libintl-prefix = / mingw --disable-bootstrap LDFLAGS = -s CFLAGS = -D_USE_32BIT_TIM
E_T
线程模型:win32
gcc版本4.8.1(GCC)

任何想法有什么不对?

解决方案

我终于能够完成工作。

首先,使用mingw-get我安装了mingw32-pthreads-w32



这让我可以使用 - 与gcc的fopenmp标志。



但是当使用CMake时,我必须包含以下行:

 消息(STATUSChecking OpenMP)
find_package(OpenMP)
IF(OPENMP_FOUND)
消息(Found OpenMP!^ _ ^)
#为OpenMP添加标志$ b $ C set(CMAKE_C_FLAGS$ {CMAKE_C_FLAGS} $ {OpenMP_C_FLAGS})
set(CMAKE_CXX_FLAGS$ {CMAKE_CXX_FLAGS} $ {OpenMP_CXX_FLAGS})
set(CMAKE_SHARED_LINKER_FLAGS$ {CMAKE_SHARED_LINKER_FLAGS} $ {OpenMP_SHARED_LINKER_FLAGS} )
set(CMAKE_EXE_LINKER_FLAGS$ {CMAKE_EXE_LINKER_FLAGS} $ {OpenMP_EXE_LINKER_FLAGS})
ELSE()
消息(错过的OpenMP!x_x)
ENDIF()

正常,但我也必须确保在我的target_link_libraries命令中包含OpenMP_CXX_FLAGS


$ b $

  set(SOURCE_FILES 
src / foo.cpp
src / bar.cpp
src / baz.cpp)
add_library(< mylib> SHARED $ {SOURCE_FILES})
target_link_libraries(< mylib> $ {OpenMP_CXX_FLAG S})


I have a CMake project which is using OpenMP and works on linux. When I went to compile it on my windows machine it looked like CMake was having trouble finding the openmp flags for mingw's gcc.

I decided to try a smaller test case and just compile main_openmp.c

#include <omp.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
    int id;
    #pragma omp parallel private(id)
        {
        id = omp_get_thread_num();
        printf("%d: Hello World!\n", id);
        }
    return 0;
}

Then when I try to compile

gcc -o OpenMPTest2 main_testomp.c -fopenmp

I get

>>> gcc -o OpenMPTest2 main_testomp.c -fopenmp
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lpthread
collect2.exe: error: ld returned 1 exit status

I attempted to find the solution online and tried variants of -lgomp, -lpthreadgc2, and -lpthreadvc2, with no improvement.

I searched my C:\MinGw directory recursively for any filenames containing lpthread and got this:

C:\MinGW\bin\pthreadgc2.dll
C:\MinGW\bin\pthreadgce2.dll
C:\MinGW\var\cache\mingw-get\packages\pthreads-w32-2.9.1-1-mingw32-dll.tar.lzma
C:\MinGW\var\lib\mingw-get\data\mingw32-pthreads-w32.xml

I'm not sure if I'm missing a flag, or a package, or what I'm doing wrong. For good measure here is the output of gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/4.8.1/lto-wrapper.exe
Target: mingw32
Configured with: ../gcc-4.8.1/configure --prefix=/mingw --host=mingw32 --build=mingw32 --without-pic
 --enable-shared --enable-static --with-gnu-ld --enable-lto --enable-libssp --disable-multilib --ena
ble-languages=c,c++,fortran,objc,obj-c++,ada --disable-sjlj-exceptions --with-dwarf2 --disable-win32
-registry --enable-libstdcxx-debug --enable-version-specific-runtime-libs --with-gmp=/usr/src/pkg/gm
p-5.1.2-1-mingw32-src/bld --with-mpc=/usr/src/pkg/mpc-1.0.1-1-mingw32-src/bld --with-mpfr= --with-sy
stem-zlib --with-gnu-as --enable-decimal-float=yes --enable-libgomp --enable-threads --with-libiconv
-prefix=/mingw32 --with-libintl-prefix=/mingw --disable-bootstrap LDFLAGS=-s CFLAGS=-D_USE_32BIT_TIM
E_T
Thread model: win32
gcc version 4.8.1 (GCC)

Any idea what's wrong?

解决方案

I was finally able to get things working.

First, using mingw-get I installed mingw32-pthreads-w32

This allowed me to use the -fopenmp flag with gcc.

But when using CMake I had to include the lines:

message(STATUS "Checking OpenMP")
find_package(OpenMP)
IF(OPENMP_FOUND)
    message("Found OpenMP! ^_^")
    # add flags for OpenMP
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OpenMP_SHARED_LINKER_FLAGS}")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
ELSE()
    message("Missed OpenMP! x_x")
ENDIF()

as normal, but I also had to make sure I had the OpenMP_CXX_FLAGS in my target_link_libraries command

set(SOURCE_FILES 
    src/foo.cpp 
    src/bar.cpp
    src/baz.cpp)
add_library(<mylib> SHARED ${SOURCE_FILES})
target_link_libraries(<mylib> ${OpenMP_CXX_FLAGS})

这篇关于用mingw在windows上使用openmp。找不到-lpthread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆