通过函数指针从其他的C文件中使用函数。

huangapple go评论68阅读模式
英文:

Use function from other C file by a function pointer

问题

首先,我不确定标题是否能很好地描述问题,请随意更改或建议一个更合适的标题。

我有以下问题:
我需要将一个函数指针传递给某个结构体。当我在同一个文件中定义该函数时,这完全正常。请参见这里:

static void a_func(int param1, /*...*/) {
    //...
}

static const struct bt_mesh_handlers my_handlers = {
	.set = a_func,
	// ...
};

当我现在将这个函数定义在另一个文件中时,无法使其正常工作:
头文件(这里不显示包含保护):

//函数声明:
void a_func(int param1, /*...*/);

C 文件:

//函数定义(之前错误地定义为 static)
void a_func(int param1, /*...*/) {
    //...
}

在主文件中:

#include "myheader.h"

static const struct bt_mesh_handlers my_handlers = {
	.set = a_func,
	// ...
};

当我将函数外包到另一个文件时,在构建过程中出现以下错误:

undefined reference to `a_func';

我已经进行了一些研究,我相对确定在定义 a_func 的文件中,会自动生成一个同名的函数指针。因此,我可以使用该指针将我的函数传递给结构体。

但我不知道如何将外包文件中的函数指针传递到主文件中。
一个好的解决方案应该是什么样的?

英文:

First of all, I'm not sure if the title describes well the problem, feel free to change or suggest a more fitting heading.

I have the following problem:
I need to pass a function pointer to some struct. When I define the function in the same file, this works perfectly fine. See here:

static void a_func(int param1, /*...*/) {
    //...
}

static const struct bt_mesh_handlers my_handlers = {
	.set = a_func,
	// ...
};

When I now define this function in another file, I can't get it to work:
the header file (include guards are not shown here):

//function declaration:
void a_func(int param1, /*...*/);

the c file:

//function definition (this was defined wrongly as static before)
void a_func(int param1, /*...*/) {
    //...
}

in the main-file:

#include "myheader.h"

static const struct bt_mesh_handlers my_handlers = {
	.set = a_func,
	// ...
};

When I outsource my function to another file, I get the following error during build:

undefined reference to `a_func'

I already did some research and I'm relatively sure that in the file where a_func is defined, there automatically is generated a function pointer with the same name. Consequently, I can use that pointer to hand over my function to the struct.

But I don't know how to get that function pointer from the outsourced file into my main file.
What would a good solution look like?

答案1

得分: 1

A static function shall be defined in each translation unit where it is used because it has internal linkage. So in the translation unit with main in your example the function is not defined,

Either define it in the header (you can define it for example as an inline function) or remove the storage class specifier static to make the function with external linkage.

英文:

A static function shall be defined in each translation unit where it is used because it has internal linkage. So in the translation unit with main in your example the function is not defined,

Either define it in the header (you can define it for example as an inline function) or remove the storage class specifier static to make the function with external linkage.

答案2

得分: 1

a_func的声明中删除static,包括其定义。

在这个上下文中,static为标识符a_func提供了内部链接,意味着该名称的使用只会与同一翻译单元中的名称定义链接在一起(正在编译的源文件,包括直接或间接包含的所有文件)。

要链接到另一个翻译单元中定义的名称,您需要外部链接。这是函数的默认设置,因此删除static将为您提供外部链接。

我已经进行了一些研究,我相对确定在定义a_func的文件中,会自动生成一个同名的函数指针。

函数的名称指的是函数本身。它代表函数。在C语言中,函数标识符会自动转换为指向函数的指针,除非它是sizeof或一元&的操作数。在定义它的文件中,没有特殊或单独的生成与函数同名的函数指针的过程。编译器、链接器和/或程序加载器共同工作以知道函数的位置并生成适当的指令来使用函数或提供其地址。

英文:

Remove static from the declarations of a_func, including its definition.

In this context, static gives the identifier a_func internal linkage, meaning uses of the name will only be linked to a definition of the name in the same translation unit (the source file being compiled, including all files it includes directly or indirectly).

To link to a name defined in another translation unit, you want external linkage. That is the default for functions, so removing static will give you external linkage.

> I already did some research and I'm relatively sure that in the file where a_func is defined, there automatically is generated a function pointer with the same name.

The name of a function refers to the function. It designates the function. In C, a function designator is automatically converted to a pointer to the function, except when it is the operand of sizeof or unary &. There is no special or separate “generation” of a function pointer with the same name as the function in the file where it is defined. The compiler, linker, and/or program loader work together to know where the function is and to generate appropriate instructions to use the function or to provide its address.

答案3

得分: 0

总结一下:问题在于我的文件没有正确链接。
可以从不同的文件中调用函数并使用其指针(这在开始时对我不起作用)。
Eric 很好地通过一个标识符解释了这是如何工作的。

英文:

To sum it up: The issue was that my files had not been linked properly.
It is possible to call a function from a different file AND to use its pointer
(that did not work for me in the beginning).
Eric explained well how this works via a designator.

huangapple
  • 本文由 发表于 2023年5月21日 22:49:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定