使用 clang-10+ 从 C 中没有参数的函数创建 pthread 的正确 ANSI 兼容方法是什么?

问题描述 投票:0回答:1

我在 C 中有一个没有参数的函数。Clang-16 建议我避免在没有原型的情况下声明函数,但是

pthread_create
不再适用于没有参数的原型。这是场景:

main.c
文件尝试从
thread_function
函数创建 pthread:

// main.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "./thread.h"

int main(void) {
  pthread_t thread_id;
  pthread_create(&thread_id, NULL, thread_function, NULL);
  pthread_join(thread_id, NULL);
  return EXIT_SUCCESS;
}

thread_function
函数在
thread.h
头文件中声明:

// thread.h
#ifndef _THREAD_H_
#define _THREAD_H_

void *thread_function();

#endif  // _THREAD_H_

并且在

thread.c
文件中定义:

// thread.c
#include <stdio.h>

void *thread_function() {
  printf("Hi\n");
  return NULL;
}

我用

clang-16
编译了程序:

$ clang-16 main.c thread.c -o thread -Wpedantic
In file included from main.c:4:
././thread.h:4:22: warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes]
void *thread_function();
                     ^
                      void
1 warning generated.
thread.c:3:22: warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes]
void *thread_function() {
                     ^
                      void
1 warning generated.

为了修复警告,我应用了两个建议的修复:

// thread.h
#ifndef _THREAD_H_
#define _THREAD_H_

void *thread_function(void);

#endif  // _THREAD_H_

还有

thread.c

// thread.c
#include <stdio.h>

void *thread_function(void) {
  printf("Hi\n");
  return NULL;
}

现在我无法再编译程序了:

$ clang-16 main.c thread.c -o thread -Wpedantic
main.c:8:36: error: incompatible function pointer types passing 'void *(void)' to parameter of type 'void * _Nullable (* _Nonnull)(void * _Nullable)' [-Wincompatible-function-pointer-types]
  pthread_create(&thread_id, NULL, thread_function, NULL);
                                   ^~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/pthread.h:334:31: note: passing argument to parameter here
                void * _Nullable (* _Nonnull)(void * _Nullable),
                                            ^
1 error generated.

我将函数的签名更改为:

// thread.h
#ifndef _THREAD_H_
#define _THREAD_H_

void *thread_function(void *);

#endif  // _THREAD_H_

还有:

// thread.c
#include <stdio.h>

void *thread_function(void *) {
  printf("Hi\n");
  return NULL;
}

它可以编译,但不再兼容 ANSI:

$ clang-16 main.c thread.c -o thread -Wpedantic
thread.c:3:29: warning: omitting the parameter name in a function definition is a C2x extension [-Wc2x-extensions]
void *thread_function(void *) {
                            ^
1 warning generated.

如果我将签名更改为

void *thread_function(void *args)
,它就不再是“没有参数的函数”。如何从不带参数的函数创建 pthread 并且仍然兼容 ANSI?

c clang pthreads
1个回答
0
投票

如果要强制 ANSI 与 clang 兼容,则需要传递

-std=c89
-ansi

您可以通过“将其强制转换为

void
”来显式丢弃参数。由于您绝对必须命名参数才能满足
-Wpedantic
,因此您必须使用它。

void *thread_function(void *arg) {
    (void) arg;
    printf("Hello");
    return NULL;
}

这会与

-ansi -Wall -Wpedantic -Wextra -Werror
一起编译。编译器资源管理器链接.

© www.soinside.com 2019 - 2024. All rights reserved.