OpenMP Hello World 错误:“架构 x86_64 的未定义符号:...”

发布于 2025-01-17 08:11:48 字数 1276 浏览 0 评论 0原文

我有以下用于使用 OpenMP 的基本 Hello World 程序:

#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/time.h>
#include<omp.h>

int main(int argc, char **argv) {
    printf("This line is executed serially.\n\n");

    #pragma omp parallel
        int nthreads = omp_get_num_threads();
        int myid = omp_get_thread_num();
        printf("Hello, world! I am thread %d of %d treads\n", myid, nthreads);
  
  return EXIT_SUCCESS;
}

我使用 Microsoft Visual Studio Code 作为 IDE,并且代码是用 c 编写的(因此文件具有 .c,而不是 .cpp 扩展名)。当我在没有调试的情况下运行代码时,VSC 在终端中使用以下命令来运行代码,并出现以下错误:

gcc tescript1.c -o tescript1 && "/[insert_working_directory_here]/"tescript1

Undefined symbols for architecture x86_64:
  "_omp_get_num_threads", referenced from:
      _main in tescript1-79e487.o
  "_omp_get_thread_num", referenced from:
      _main in tescript1-79e487.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

其他线程包括 C++ Hello World 体系结构 x86_64 的未定义符号: 似乎表明我使用了错误的编译器。然而,在这个例子中,他们的程序是 c++,而不是 c,并且 VSC 的设置对于如何更改编译器并不明确。我最终陷入困境,无法尝试我读过的建议或 100% 确定问题所在。任何帮助表示赞赏。

I have the following basic Hello World program for using OpenMP:

#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/time.h>
#include<omp.h>

int main(int argc, char **argv) {
    printf("This line is executed serially.\n\n");

    #pragma omp parallel
        int nthreads = omp_get_num_threads();
        int myid = omp_get_thread_num();
        printf("Hello, world! I am thread %d of %d treads\n", myid, nthreads);
  
  return EXIT_SUCCESS;
}

I am using Microsoft Visual Studio Code as an IDE, and the code is written in c (and file therefore has a .c, not .cpp extension). When I run the code without debugging, VSC uses the following command in terminal to run the code, and the following error appears:

gcc tescript1.c -o tescript1 && "/[insert_working_directory_here]/"tescript1

Undefined symbols for architecture x86_64:
  "_omp_get_num_threads", referenced from:
      _main in tescript1-79e487.o
  "_omp_get_thread_num", referenced from:
      _main in tescript1-79e487.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Other threads including C++ Hello World Undefined symbols for architecture x86_64: seem to suggest I'm using the wrong compiler. However, in this example, their program is c++, not c, and VSC's settings are anything but clear on how to change compilers. I've ended up stuck without being able to try the suggestions I've read or 100% for sure identify the problem. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终弃我 2025-01-24 08:11:48

您可以尝试在 Visual Studio 项目属性中将 /openmp 值设置为 OpenMP 支持,如下所示。

项目属性

如您所见,我能够在 OpenMP 支持下用 C 语言运行基本的 helloworld 程序。

基本 helloworld在 c 中使用 openMP

是 POSIX 标头,不是 C/C++ 标准库的一部分。它在 Windows 中不起作用。仅使用;如果您正在为 Windows 构建。

另外,希望你没有错过,
输入图片此处描述

You could try to set the /openmp value to OpenMP support in Visual studio project properties as shown below.

project properties

As you can see I was able to run a basic helloworld program in C with openMP support.

basic helloworld in c with openMP

<sys/time.h> is a POSIX header, not part of the C/C++ standard library. It won't work in windows. Use just <time.h> if you are building for windows.

Also, I hope you have not missed,
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文