C++ 使用 mingw 在 windows 上使用 openmp。找不到 -lpthread

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23554716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:29:17  来源:igfitidea点击:

Using openmp on windows with mingw. Cannot find -lpthread

c++windowsgccmingwopenmp

提问by Erotemic

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.

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

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

我决定尝试一个较小的测试用例并编译 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.

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

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

我递归地搜索了我的 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-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

我不确定我是否缺少标志或包裹,或者我做错了什么。为了更好地衡量这里是 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?

知道出了什么问题吗?

回答by Erotemic

I was finally able to get things working.

我终于能够让事情运作起来。

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

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

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

这允许我在 gcc 中使用 -fopenmp 标志。

But when using CMake I had to include the lines:

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

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

像往常一样,但我还必须确保我的 target_link_libraries 命令中有 OpenMP_CXX_FLAGS

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})

回答by Patrick

In your CMakeLists.txtfile, you should use the following:

在您的CMakeLists.txt文件中,您应该使用以下内容:

find_package(OpenMP REQUIRED)

set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")

If you get an error when you are configuring cmake, then that means you don't have the necessary libraries, or at least cmake can't find them. If you have the libraries and cmake can't find them, then make sure the find path is set:

如果您在配置 cmake 时遇到错误,则意味着您没有必要的库,或者至少 cmake 找不到它们。如果您有这些库而 cmake 找不到它们,请确保设置了查找路径:

set (CMAKE_FIND_ROOT_PATH C:/MinGW)

You might want to try creating a "toolchain" file as described here.

您可能想尝试按照此处所述创建“工具链”文件。

I would also suggest you try mingw-w64 for cross-compiling for Windows. I use it successfully for both 32-bit and 64-bit applications. I use mingw[32/64]-cmake, and then everything just works.

我还建议您尝试使用 mingw-w64 进行 Windows 交叉编译。我成功地将它用于 32 位和 64 位应用程序。我使用 mingw[32/64]-cmake,然后一切正常。

I'm using Fedora 19 and 20, and some packages relevant to your question are (for 32-bit):

我正在使用 Fedora 19 和 20,与您的问题相关的一些软件包是(对于 32 位):

mingw32-filesystem
mingw32-libgomp

回答by Theja

Very simple steps that worked for me:

对我有用的非常简单的步骤:

  • Got to the MinGW installation manager
  • Find MinGW Standard Libraries section on the left side
  • Install the mingw32-pthreads-w32packages.
  • 到 MinGW 安装管理器
  • 在左侧找到 MinGW 标准库部分
  • 安装mingw32-pthreads-w32软件包。