1

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 !

jxh
  • 69,070
  • 8
  • 110
  • 193
Flof
  • 74
  • 7
  • What command(s) are you using to compile? – dbush Dec 19 '18 at 18:34
  • You need to compile both sources and link them together. Perhaps that would be achieved by naming both (the .c and .cxx) on the compiler command line. – John Bollinger Dec 19 '18 at 18:36
  • Question, however: what language do you expect the compiler to assume for file `param.cxx`? If C, then its name should end in `.c`, not `.cxx`. – John Bollinger Dec 19 '18 at 18:37
  • This part of the code is compiled with g++. – Flof Dec 19 '18 at 18:38
  • @JohnBollinger, unfortunately I can't modify the compiler call, which is unaccessible for me. However, I don't think that it's a compiler linking problem (I'm surely wrong) : I tried the solution described here (https://stackoverflow.com/questions/8562694/how-to-declare-function-pointer-in-header-and-c-file) an it worked, but once I adapt it to follow the rest of my code, nope, not anymore :') – Flof Dec 19 '18 at 18:44

1 Answers1

2

.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
jxh
  • 69,070
  • 8
  • 110
  • 193
  • Thank you it worked !! However I don't quite understand why in another groupe of header/*.c file already existing in the program I'm using, it worked with the same header declaration ? – Flof Dec 19 '18 at 18:56
  • Are the other C files calling `grid_create()`? – jxh Dec 19 '18 at 19:24
  • No, but a similarly defined function. Actually I copy/paste the header files for those other functions in a new header, and then simplifying this content to create my own function i.e. grid_create() – Flof Dec 19 '18 at 19:27
  • It is not clear if those C files face the same problem. If the symbols are resolving, then it means the linker found a defined symbol. – jxh Dec 19 '18 at 19:31
  • Related: https://stackoverflow.com/q/1041866/315052 and https://stackoverflow.com/q/3789340/315052 – jxh Dec 19 '18 at 21:12