*.hxx 中声明的 void 函数,在 *.cxx 中定义但在 *.c 中未定义
Void function declared in *.hxx, defined in *.cxx but undefined in *.c
我正在尝试声明一个可通过不同的 *.c 文件访问的全局函数。
我在 param.hxx 中声明了它,在 param.cxx 中定义了它,我想在 compute_grid.c 中访问它。
不幸的是,在编译过程中出现以下错误:
compute_grid.c:(.text+0x5) : undefined reference to « grid_create »
我不太熟悉 C 中的此类函数声明。实际上我正在构建一个大程序的模块部分,我从代码的另一个文件中复制了这些声明 似乎工作?!
不幸的是,该程序是高度机密的,我无法访问所有资源,但我会尽力为您提供删除的部分...
param.hxx :
typedef struct grid_t
{
int *test;
} grid_t;
void
grid_create(void);
extern grid_t *grid;
param.cxx :
#include <malloc.h>
#include "param.hxx"
grid_t *grid;
void grid_create(void)
{
grid = (grid_t*)malloc(sizeof(grid_t));
grid->test = (int*)malloc(sizeof(int));
*grid->test = 123;
}
compute_grid.c :
#include "param.hxx"
void
compute_grid()
{
grid_create();
return;
}
感谢您的帮助!
.cxx
是用于 C++ 文件的扩展名之一。您的编译器可能正在使用文件的扩展名将源代码编译为 C++。编译 C 文件时,它会生成对 C 符号 grid_create
的未解析引用,但编译后的 C++ 文件将 grid_create
定义为 C++ 符号。因此,链接器将保留对 grid_create
的 C 符号引用未解析,因为只有一个 C++ 符号与 void grid_create(void)
.
相关联
您可以尝试保护头文件,以便 C++ 编译器生成 C 符号而不是 C++ 符号。
#ifdef __cplusplus
extern "C" {
#endif
void
grid_create(void);
#ifdef __cplusplus
}
#endif
我正在尝试声明一个可通过不同的 *.c 文件访问的全局函数。
我在 param.hxx 中声明了它,在 param.cxx 中定义了它,我想在 compute_grid.c 中访问它。
不幸的是,在编译过程中出现以下错误:
compute_grid.c:(.text+0x5) : undefined reference to « grid_create »
我不太熟悉 C 中的此类函数声明。实际上我正在构建一个大程序的模块部分,我从代码的另一个文件中复制了这些声明 似乎工作?!
不幸的是,该程序是高度机密的,我无法访问所有资源,但我会尽力为您提供删除的部分...
param.hxx :
typedef struct grid_t
{
int *test;
} grid_t;
void
grid_create(void);
extern grid_t *grid;
param.cxx :
#include <malloc.h>
#include "param.hxx"
grid_t *grid;
void grid_create(void)
{
grid = (grid_t*)malloc(sizeof(grid_t));
grid->test = (int*)malloc(sizeof(int));
*grid->test = 123;
}
compute_grid.c :
#include "param.hxx"
void
compute_grid()
{
grid_create();
return;
}
感谢您的帮助!
.cxx
是用于 C++ 文件的扩展名之一。您的编译器可能正在使用文件的扩展名将源代码编译为 C++。编译 C 文件时,它会生成对 C 符号 grid_create
的未解析引用,但编译后的 C++ 文件将 grid_create
定义为 C++ 符号。因此,链接器将保留对 grid_create
的 C 符号引用未解析,因为只有一个 C++ 符号与 void grid_create(void)
.
您可以尝试保护头文件,以便 C++ 编译器生成 C 符号而不是 C++ 符号。
#ifdef __cplusplus
extern "C" {
#endif
void
grid_create(void);
#ifdef __cplusplus
}
#endif