gpt4 book ai didi

Clion 和 OpenMP

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:42 24 4
gpt4 key购买 nike

我正在学习并行计算,并开始了我的 OpenMP 和 C 之旅。

我一直在配置 Clion,但没有成功。

#include <stdio.h>
#include <omp.h>

int main() {
#pragma omp parallel
{
int n = omp_get_num_threads();
int tid = omp_get_thread_num();
printf("There are %d threads. Hello from thread %d\n", n, tid);
};

/*end of parallel section */
printf("Hello from the master thread\n");

但是我收到了这个错误:

在函数 main' 中:
C:/Users/John/CLionProjects/Parallelexamples/main.c:6: undefined reference
omp_get_num_threads'C:/Users/John/CLionProjects/Parallelexamples/main.c:7: 未定义对“omp_get_thread_num”的引用collect2.exe:错误:ld 返回 1 退出状态mingw32-make.exe[2]: * [Parallelexamples.exe] 错误 1CMakeFiles\Parallelexamples.dir\build.make:95:目标“Parallelexamples.exe”的配方失败mingw32-make.exe[1]: * [CMakeFiles/Parallelexamples.dir/all] 错误 2CMakeFiles\Makefile2:66: 目标 'CMakeFiles/Parallelexamples.dir/all' 的配方失败Makefile:82: 目标 'all' 的配方失败mingw32-make.exe: *** [all] 错误 2

我按照说明制作了我的 CMakeListtxt 文件,如下所示:

cmake_minimum_required(VERSION 3.8)
project(Parallelexamples)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu11 -fopenmp")


set(SOURCE_FILES main.c)
add_executable(Parallelexamples ${SOURCE_FILES})

我错过了什么吗?

最佳答案

首先,由于您使用的是 CMake,因此请利用 FindOpenMP 宏:https://cmake.org/cmake/help/latest/module/FindOpenMP.html

cmake_minimum_required(VERSION 3.8)
project(Parallelexamples)

其次,您似乎没有链接到 OpenMP 运行时库。您不仅必须传递 openmp 编译标志,还必须传递正确的链接器标志:

set_target_properties(Parallelexamples LINK_FLAGS "${OpenMP_CXX_FLAGS}")

作为旁注,如果你真的用 C 而不是 C++ 编程,你将不需要 CXX_FLAGS 你可以只使用 C_FLAGS

关于Clion 和 OpenMP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46632072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com