Undefined reference

main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "types.h"
#include "gaussian.h"
#include "args.h"
#include <stdbool.h>


int main ( int argc, char *argv[] )
{
    double units;
    uint32_t gaussianSize;
    float gaussianSigma;
    char* imgname;//the name of the image file, taken by the arguments

    //read in the program's arguments
    if(readArguments(argc,argv,&imgname,&gaussianSize,&gaussianSigma)==false)
        return -1;


    //perform CPU blurring
    if(pna_blur_cpu(imgname,gaussianSize,gaussianSigma)==false)//time it
        return -2;

    //perform GPU blurring and then read the timer
    if(pna_blur_gpu(imgname,gaussianSize,gaussianSigma)==false)
        return -3;

    return 0;
}


I have errors of undefined reference for functions
readArguments declared in args.h
pna_blur_cpu declared in gaussian.h
pna_blur_gpu declared in gaussian.h
can you tell me why?
Last edited on
Could you show args.h to us?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef READ_ARGS_H
#define READ_ARGS_H

#include "types.h"
#include "refu/rf_setup.h"

extern char helpStr[];

#define DEFAULT_IMG_NAME        "image3.BMP"
#define DEFAULT_GAUSSIAN_SIZE   3
#define DEFAULT_GAUSSIAN_SIGMA  0.8

bool readArguments(int argc, char *argv[],char** imgName,uint32_t* gaussianSize,float* gaussianSigma);

#endif 
The definition of bool, where is it? types.h?
Is this C or C++? Can you give us the exact compiler errors?
'undefined reference' usually means there is no body for the mentioned functions.

In your case... it sounds like you have prototypes for readArguments, pna_blur_cpu, and pna_blur_gpu... but you never gave those functions a body.
I use C.
types.h
#include <stdbool.h>

Here types.c, there is definition of readArguments:
http://paste.ofcode.org/QE5GYVWU2EarMTwmHYnbDx

Gaussian.h:
1
2
3
4
5
6
7
8
#ifndef GAUSSIAN_H
#define GAUSSIAN_H
#include "types.h"
#include "bitmap.h"
#include "refu/rf_setup.h"
char pna_blur_cpu(char* imgname,uint32_t size,float sigma);
char pna_blur_gpu(char* imgname,uint32_t size,float sigma);
#endif 

And here is gaussian.c
http://paste.ofcode.org/3vQCC5h68mQuVFMHgpMzuJ

Errors:
1
2
3
main.cpp|24|undefined reference to `readArguments(int, char**, char**, unsigned int*, float*)'|
main.cpp|31|undefined reference to `pna_blur_cpu(char*, unsigned int, float)'|
main.cpp|40|undefined reference to `pna_blur_gpu(char*, unsigned int, float)'| 


Edit:
there is type uint32_t and I change it to unsigned int ... but it is the same still.
Last edited on
Are those source files part of your project? IE, are you compiling/linking them?
Yeas, they are part of project. They are included. No linking.
I'm skeptical.

Try mucking up gaussian.c. Like add a garbage line to it just before the includes:

1
2
3
// something like this:

rkljhdkslfj


Then try rebuilding. If you don't get a compiler error, you know the file is not being linked.
Can you try it? Not to build, just check it. http://sourceforge.net/projects/autots/files/Gaussian_with_OpenCL%201.zip/download Code blocks project Gaussian_with_OpenCL.cbp, it uses OpenCL.
Last edited on
> I use C.

Then, how are you getting errors from main.cpp?
main.cpp|31|undefined reference to ...

Either rename the file as main.c

Or wrap the C header in
1
2
3
4
5
6
7
8
9
#ifdef __cplusplus 
    extern "C" {
#endif 

#include "gaussian.h"

#ifdef __cplusplus 
    }
#endif  


Tip: C linkage is not type-safe.

With C you wouldn't have got this error:
undefined reference to `pna_blur_cpu(char*, unsigned int, float)'

Instead, you would have got: undefined reference to `_pna_blur_cpu'
Last edited on
Wow, I did not noticed that. I had no idea it could be the reason. That's the file I
originally created and copied code into it, instead of copy the original file as main.c . This was the error.

I yet removed two lines of C++ and it looked compiled, but program crashes after run. But never mind. I just wanted to remove this error. I already have updated version of the program which works. Thanks for help.
Last edited on
> Try mucking up gaussian.c.
> Then try rebuilding. If you don't get a compiler error, you know the file is not being linked.
Or you could read the build command


> Instead, you would have got: undefined reference to `_pna_blur_cpu'
¿why would it add an underscore?
> ¿why would it add an underscore?

Don't ask me; ask the people who wrote the linker which emitted the symbol verbatim.

Note: It does not 'add an underscore'; the underscore is there in the symbol given to the linker. Some linkers 'remove the leading underscore' when they format the diagnostic; some others do not.
Topic archived. No new replies allowed.