Flof
Flof

Reputation: 74

Void function declared in *.hxx, defined in *.cxx but undefined in *.c

I'm trying to declare a global function accessible through different *.c files.

I have declared it in param.hxx, defined it in param.cxx and I would like to access it in compute_grid.c.

Unfortunately, during the compilation I have the following error :

compute_grid.c:(.text+0x5) : undefined reference to « grid_create »

I'm not very familiar with such functions declarations in C. Actually I'm building a module part of a big program, I've copied those declaration from another file of the code witch seems to work ?!

Unfortunately, the program is hugely confidential, I don't have access to all the sources but I will do my best to give you expurged parts...

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;
}

Thank you for your help !

Upvotes: 1

Views: 313

Answers (1)

jxh
jxh

Reputation: 70412

.cxx is one of the extensions used for C++ files. Your compiler may be using the extension of the file to compile the source as C++. When the C file is compiled, it generates an unresolved reference to the C symbol grid_create, but the compiled C++ file defines grid_create as a C++ symbol. Thus, the linker will leave the C symbol reference to grid_create unresolved, since there is only a C++ symbol associated with void grid_create(void).

You can try to guard the header file so that the C++ compiler will generate a C symbol rather than a C++ one.

#ifdef __cplusplus
extern "C" {
#endif

void 
grid_create(void);


#ifdef __cplusplus
}
#endif

Upvotes: 2

Related Questions