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:
- 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.
- 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?
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
This causes the consuming project to use the symbol in the import library, that you have added to the consuming module.