2
votes

error LNK2019: unresolved external symbol "char * __cdecl BytesToString(unsigned char const *,unsigned int)" (?BytesToString@@YAPADPBEI@Z) referenced in function _wmain C:\Users\anandada\Documents\Visual Studio 2010\Projects\ByteToString\ByteToString\ByteToString.obj ByteToString

above is the error I am getting. code is shown below. ByteToString is a console Win32 application and Utility is Win32 DLL.

Utility.c

#include "stdafx.h"
#include "Utility.h"
#include "stdlib.h"

char* BytesToString( const unsigned char* PpcbBytes, const unsigned int PuiNoOfBytes)
{
    unsigned char bRetVal = 0;
    unsigned int ctr = 0;
    char* PpszString = NULL;
    int len=0;

    do
    {
        PpszString=(char*)calloc(PuiNoOfBytes*3+1,sizeof(char));
        if(NULL==PpszString)
            break;
        len=5;
    } while(0);
    return PpszString;
}

Utility.h

#ifndef _UTILITY_H
#define _UTILITY_H
__declspec(dllexport) char* BytesToString( const unsigned char* PpcbBytes, const unsigned int PuiNoOfBytes);
#endif

ByteToString.cpp

// ByteToString.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "string.h"
#include "stdlib.h"
#include "..\Utility\Utility.h"

int _tmain(int argc, _TCHAR* argv[])
{
    unsigned char pbArray[5]={0x41,0x42,0x43,0x44,0x45};
    char* pbExpArray=NULL;
    unsigned int Flag=1;
    int len=0;

    pbExpArray=BytesToString(pbArray,5);
    free(pbExpArray);
    Flag=strcmp("41 42 43 44 45 ",pbExpArray);
    len=strlen(pbExpArray);

    return 0;
}

I have set project properties like this:

both ByteToString and Utility project calling conventions are: __cdecl

In ByteToString, Linker->General->Additional Directories: $(OutDir)

(I tried this too Linker->General->Additional Directories:$(SolutionDir)$(Configuration)\ )

In ByteToString, Linker->Input->Additional Dependenicies: Utility.lib

I aslo tried this,

#ifndef _UTILITY_H
#define _UTILITY_H
extern "C"
{
__declspec(dllexport) char* BytesToString( const unsigned char* PpcbBytes, const unsigned int PuiNoOfBytes);
}
#endif

This gives error:

error C2059: syntax error : 'string'

Update:

  1. While creating project, I had added Utility.cpp. I renamed it to Utility.c, set the project properties and compiled. I got the error shown above.
  2. Then I renamed it back to Utility.cpp and compiled. No error.

Why is this? I want Utility file in .c. What is the correct method to add a .c file into project?

4
Have you tried using __declspec(dllimport) when including the file in the second project? - Luchian Grigore
I used __declspec(dllimport) 'char* BytesToString( const unsigned char* PpcbBytes, const unsigned int PuiNoOfBytes);' before main. It did not work. - SHRI

4 Answers

2
votes

Declare the external with extern "C" instead.

1
votes

You need the header file to differentiate between use in the exporting DLL and the consuming other module (DLL or EXE). The project wizard had generated a preprocessor definition for this purpose. If your exporting DLL is called Utility.dll than this definitions is UTILITY_EXPORTS. Check you projects properties for this.

In your header file you define something like this

#ifdef UTILITY_EXPORTS
#define UTILITY_API __declspec(dllexport)
#else
#define UTILITY_API __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C" {
#endif 

UTILITY_API char* BytesToString( const unsigned char* PpcbBytes, const unsigned int PuiNoOfBytes);

#ifdef __cplusplus
}
#endif

This causes the consuming project to use the symbol in the import library, that you have added to the consuming module.

0
votes

I think you haven't added the other project's .lib file to your project. That's why the linker says it can't find the function. The fact that you have another project lying around on your harddisk doesn't mean the linker knows where to find your function. It only looks into code that's compiled in the current project. If you have a DLL, you either load a function with LoadLibrary and then GetProcAddress, or you add the accompanying .lib file to your project.

0
votes

Your need to add __declspec(dllexport) while implementing the function:

__declspec(dllexport)
char* BytesToString( const unsigned char* PpcbBytes, const unsigned int PuiNoOfBytes) 
{ }

And make sure you have having correct LIB in your target project!