user2035796
user2035796

Reputation: 67

Undefined reference to function in Codeblocks

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?

Upvotes: 1

Views: 5623

Answers (3)

Mohamed Allal
Mohamed Allal

Reputation: 20870

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

Upvotes: 3

Abdelrhman Elokbe
Abdelrhman Elokbe

Reputation: 1

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

Upvotes: -4

Some programmer dude
Some programmer dude

Reputation: 409176

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

Upvotes: 5

Related Questions