1

I have this code here in a header file and source files. Here are little snippets of the code. This is from the .cpp file.

int sample(Cdf* cdf)  
{
    //double RandomUniform();
    double r = RandomUniform(); //code that is causing the error
    for (int j = 0; j < cdf->n; j++)
    if (r < cdf->vals[j])
    return cdf->ids[j];
    // return 0;
}

This is from the .c file:

double RandomUniform(void)
{
    double uni;

    /* Make sure the initialisation routine has been called */
    if (!test) 
    RandomInitialise(1802,9373);

    uni = u[i97-1] - u[j97-1];
    if (uni <= 0.0)
    uni++;
    u[i97-1] = uni;
    i97--;

    // ...
}

And this is from my header file

void   RandomInitialise(int,int);
double RandomUniform();
double RandomGaussian(double,double);
int    RandomInt(int,int);
double RandomDouble(double,double);

I have used #include "headerfile.h" in the .cpp file and then I compiled the code. As you can see from the snippets, I am basically calling the function RandomUniform() that is in the .cpp file then defining it in the header file.

Problem is, whenever I build the program, I get an "undefined reference to function" error. Here is the error I am getting

       In function 'Z6sampleP3Cdf':
       undefined reference to 'RandomUniform()'

Anybody have any idea?

user2035796
  • 67
  • 2
  • 7
  • 14
  • What is `Cdf`? Where is it declared? – Mat Mar 16 '13 at 22:20
  • `RandomUniform()` != `RandomUniform(void)`, without any parameters it actually means `RandomUniform(...)`, a function that takes any number of parameters. – emil Mar 16 '13 at 22:23
  • the answer below is fine but whats cdf. – Daij-Djan Mar 16 '13 at 22:35
  • @emil no, I dont think so .. a() == a(void) – Daij-Djan Mar 16 '13 at 22:35
  • @user2035796 How are you linking the C project to the C++ project? Please describe this process. If you don't know what I'm talking about, I suggest reading [The build process of Code::Blocks: Using project dependencies](http://wiki.codeblocks.org/index.php?title=The_build_process_of_Code%3a%3aBlocks#Using_project_dependencies) – autistic Mar 16 '13 at 23:01
  • The files are in the same project not different projects – user2035796 Mar 16 '13 at 23:10
  • And you include the C source file containing the `RandomUniform` function in the project? In other words, you _link_ with the correct files? – Some programmer dude Mar 17 '13 at 00:01
  • yes I have done the #include thing – user2035796 Mar 17 '13 at 00:11
  • this feels like a quiz show to me :D – Daij-Djan Mar 17 '13 at 00:43
  • the error occurs just after int(Cdf* cdf), in the function RandomUniform() – user2035796 Mar 17 '13 at 01:00
  • And how about the _linking_? The error is a _linker_ error meaning that the linker can't find the definition of the `RandomUniform` function. For the project to be complete, you need to compile _all_ source files to object files, then link _all_ object files into the final executable. _And_ if parts are in a C file you need the solution provided in my answer as well. – Some programmer dude Mar 17 '13 at 09:08
  • OK is there information on linking in the codeblocks documentation? I'm going to have a look – user2035796 Mar 17 '13 at 09:28

3 Answers3

5

Remember that C++ mangles its function names. So a function named sample in C++ will not be named the same in C.

And the oposite of course, a function like void RandomInitialise(int,int) in C will not be simply named RandomInitialise in C++.

You have to use extern "C" for your function implemented in C, or the C++ compiler will create mangled names for your C functions.

So you have to change your header file containing these C-only functions as:

extern "C" void   RandomInitialise(int,int);
extern "C" double RandomUniform(void);
extern "C" double RandomGaussian(double,double);
extern "C" int    RandomInt(int,int);
extern "C" double RandomDouble(double,double);

Of course this means you can't use the same header file from a pure C project, as extern "C" is not valid in a pure C compiler. But you can use the preprocessor to help with that:

#ifdef __cplusplus
extern "C" {
#endif

void   RandomInitialise(int,int);
double RandomUniform(void);
double RandomGaussian(double,double);
int    RandomInt(int,int);
double RandomDouble(double,double);

#ifdef __cplusplus
}
#endif
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

If you didn't add your file to debug and release at file creation! Then that would be the problem!

The cpp files need to be linked and added! If not within debug and|or release! The linker will not find them!

How to add them to debug or release

If they are already created! In the file explorer left! right click! And choose properties! Within properties choose build! You'll find where to add debug and release!

enter image description here enter image description here

Note about extern "C"

Adding extern "C" is not necessary! And i confirm that! The linker link without problem!

Still check this answers

https://stackoverflow.com/a/15455385/7668448

https://stackoverflow.com/a/15141021/7668448

Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
-4

right click here then "add files " then choose the .h & .c files you want to editing their file pass then press ok.

click here to see where to press

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141