0

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.

  • The object code of the C functions need to be linked along with the object code of the C++ functions when creating the executable. Merely `#Include`ing the header file of the C functions does not make that happen automatically. – R Sahu May 06 '16 at 05:03
  • Sounds like you forgot to link the library containing this c function. – Harold May 06 '16 at 05:04
  • @AdmiralFishHead : Add the compiler you use to the question. – sjsam May 06 '16 at 05:09
  • 2
    Did you put an `extern "C"` around the include of the C header in your C++ source? – Lukas Thomsen May 06 '16 at 05:12
  • 1
    Added. Hope it helps. I tried, it gave me some weird error, so I did it directly in the header using #ifdef __cplusplus extern "C" { #endif method – AdmiralFishHead May 06 '16 at 05:12
  • @AdmiralFishHead : static or shared build? – sjsam May 06 '16 at 05:22
  • Probably shaded (sorry, not a strong software engineer), I'm using system-wise dlls. I cleaned the solution and rebuilt, now the system gives me fatal error c1853 on all of the cpp files. – AdmiralFishHead May 06 '16 at 05:28
  • Do you think setting the .c file to "compile as c++" would be a good idea? – AdmiralFishHead May 06 '16 at 05:31
  • You are still mixing things that's why can't get out of trouble. At beginning you had problem because, without `extern "C"` in the C header, the compiler interpreted the library as a C++ one, mangling all symbols that could have never been found in the unmangled C library (error LINK2019). Then you set compilation as C project, but this conflicted again with prebuilted C++ headers of your C++ project (fatal error c1853). If your project is C++ let it so and simply add `extern "C" {` at beginninng of the C header file and the matching close brace `}` at its end. – Frankie_C May 06 '16 at 07:23

0 Answers0