Unresolved externals and extern "C"?

1.1k views Asked by At

As new as I am to C++, I don´t fully understand this linking and stuff. And I think this is about extern "C" linking.

extern "C"
{
int loadbmp(char *filename, unsigned char **buf,
            int *w, int *h, int pf, int bottomup);
const char *bmpgeterr(void);
}

unsigned char *srcBuf=NULL, **jpegBuf=NULL;
unsigned long jpegsize=0;
int width, height;
char *filename={"Screenshot158139.bmp"};
tjhandle handle=NULL;

void main(){
    if(loadbmp(filename, &srcBuf, &width, &height,TJPF_RGB, 0)==-1){
        //printf("Could not load bitmap: %s\n", bmpgeterr());
        exit(1);
    }
    if((handle=tjInitCompress())==NULL) {
        printf("Could not initialize compressor: %s\n", tjGetErrorStr());
        free(srcBuf);
        exit(1);
    }
    if((tjCompress2(handle, srcBuf, width, 0, height, TJPF_RGB,
                    jpegBuf, &jpegsize, TJSAMP_444,10, 0))==-1) {
        printf("Could not compress: %s\n", tjGetErrorStr());
        free(&srcBuf);
        tjDestroy(handle);
        exit(1);
    }    
}

The problem I get from this is that I need to resolve the extern "C" code I think:

error LNK2001: unresolved external symbol loadbmp

Sadly, I don´t know how to do that, and as this error is extremely common in the C++ world, finding an answer for this is not that easy as they can differ.

Hopefully it´s pretty easy to solve this, as I guess I must define it or something as it´s external code.

2

There are 2 answers

5
Dietmar Kühl On BEST ANSWER

It seems you have declared loadbmp() but you haven't defined it. Where is the function defined? If it is supposed to come from a library, do not declare this function yourself but rather include the relevant header. The documentation of the function should tell you which is the relevant header and it should mention which extra libraries you may need to include.

If loadbmp() isn't function you want to take from a library, you need to define (implement) it.

0
SSpoke On

What you are missing is linking to your .lib file. Often a quick and easy way is to add this line to the top of the file

#pragma comment(lib,"put_your_lib_filename_here.lib")