gpt4 book ai didi

c - 将库中定义的函数名称作为 C 中的函数指针传递

转载 作者:行者123 更新时间:2023-11-30 19:01:28 26 4
gpt4 key购买 nike

我想开发一个定义不同算法的库和一个函数处理程序,它可以获取定义该算法的函数之一的名称并调用它。这是源文件(imp_GE_s.c):

#include "imp_GE_s.h"

// This is a function handler developed to get name of a functions as
// function pointer and call the desired function
double func_handler(void (*func)(float*, float*, float*), float* A, float* L, float* U)
{
// call the function and do sth
}


// non-blocked KJI form of GE with column pivoting
void LU_kij_nonBlocked(float* A, float* L, float* U)
{
//some statements
}


// non_blocked JKI form of GE with column pivoting
void LU_kji_nonBlocked(float* A, float* L, float* U)
{
//some statements
}

这是头文件( imp_GE_s.h ):

#ifndef IMP_GE_S
#define IMP_GE_S

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

void LU_kij_nonBlocked(float* A, float* L, float* U);
void LU_kji_nonBlocked(float* A, float* L, float* U);
double func_handler(void (*func)(float*, float*, float*), float* A, float* L, float* U);

#endif

这是main.c文件:

#include "imp_GE_s.h"

int main()
{
float A[4][4] = {{1,2,3,4},{2,3,4,5},{3,4,6,8},{2,5,7,9}};
float L[4][4] = {0};
float U[4][4] = {0};

printf ("time of operation = %d \n", func_handler((*LU_kij_nonBlocked),A,L,U));
}

但是每次我用这个命令编译主文件gcc main.c -o main我收到以下错误

in function 'main':
example_GE_s.c:(.text+0x186): undefined reference to 'LU_kij_nonBlocked'
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: example_GE_s.c:(.text+0x18b): undefined reference to 'func_handler'
collect2: error: ld returned 1 exit status

最佳答案

您应该注意许多警告,例如控制到达非 void 函数的末尾。无论如何,您的问题源于未链接源 C 文件。您需要像下面这样编译,

gcc main.c imp_GE_s.c -o exec

作为旁注,如 Jonathan Leffler其他 C 大师说,将警告视为错误

关于c - 将库中定义的函数名称作为 C 中的函数指针传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57755263/

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