Simple problem, the I added a 3rd party library which was done in C to my C++ project. For whatever reason it causes LINK2019 error, the header that came with the C file is included and defines everything. However each time I try to use any of the functions the compiler flips out.
main.cpp, making a const char pointer and passing to the initializer:
const char * testTexture = "test_png.png";
graphicsTextureAsset * textureAsset1 = new graphicsTextureAsset(1, testTexture);
some_class.cpp, initializer calls the function passes the pointer to it:
#include "graphicsAllLibs.h"
graphicsTextureAsset::graphicsTextureAsset(int textureAssetID, const char * fileNameAddr)
{
this->readFilePNG(fileNameAddr);
}
void graphicsTextureAsset::readFilePNG(const char * fileNameAddr)
{
int errorVal = 0;
this->textureBuffer = upng_new_from_file(fileNameAddr);
}
graphicsAllLibs.h contains all headers, including the one containing the functions and it never caused problems with other libraries.
The message is:
1>graphicsTextureAsset.obj : error LNK2019: unresolved external symbol upng_new_from_file referenced in function "public: void __cdecl graphicsTextureAsset::readFilePNG(char const *)" (?readFilePNG@graphicsTextureAsset@@QEAAXPEBD@Z)
1>graphicsTextureAsset.obj : error LNK2019: unresolved external symbol upng_free referenced in function "public: __cdecl graphicsTextureAsset::~graphicsTextureAsset(void)" (??1graphicsTextureAsset@@QEAA@XZ)
The C functions are described as:
upng_t* upng_new_from_bytes (const unsigned char* buffer, unsigned long size); // the header
upng_t* upng_new_from_file(const char *filename)
{
upng_t* upng;
unsigned char *buffer;
FILE *file;
long size;
upng = upng_new();
if (upng == NULL) {
return NULL;
}
file = fopen(filename, "rb");
if (file == NULL) {
SET_ERROR(upng, UPNG_ENOTFOUND);
return upng;
}
/* get filesize */
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
/* read contents of the file into the vector */
buffer = (unsigned char *)malloc((unsigned long)size);
if (buffer == NULL) {
fclose(file);
SET_ERROR(upng, UPNG_ENOMEM);
return upng;
}
fread(buffer, 1, (unsigned long)size, file);
fclose(file);
/* set the read buffer as our source buffer, with owning flag set */
upng->source.buffer = buffer;
upng->source.size = size;
upng->source.owning = 1;
return upng;
}
Any thoughts? Are there any alterations to the C header/file that need to be done in order work with my program? Any compiler changes?
EDIT: I am using Visual Studio 2015.I modified the header of the C function with:
#ifdef __cplusplus
extern "C" {
#endif
stuff here
#ifdef __cplusplus
}
#endif
!!! SOLVED: solution was to force the .c file to compile as a c++ file. Otherwise it crashes and burns.