1

I am trying to call void process (uint8_t* I1,uint8_t* I2,float* D1,float* D2,const int32_t* dims); from a header file, using the following

    int n=10;
    size_t size=n*n*sizeof(uint8_t);
    uint8_t* I1 = (uint8_t*)malloc(size);
    uint8_t* I2 = (uint8_t*)malloc(size);
    size=n*n*sizeof(float);
    float* D1=(float*)malloc(size);
    float* D2=(float*)malloc(size);
    const int32_t dims[3] = {n,n,n};
    process(I1,I2,D1,D2,dims);

but when I compile, using g++ on linux, I get the message undefined reference to process(unsigned char*, unsigned char*, float*, float*, int const*) Why does this happen?

Zeyad Khattab
  • 91
  • 1
  • 1
  • 4
  • I am writing C++, I guess the reason I didn't think I should more code is that I don't understand in the first place, why the compiler didn't find the function process regardless of what it does. There is something I think is worth mentioning in the top of the header file. ` #ifndef _MSC_VER ` ` #include ` ` #else` ` typedef __int8 int8_t; ` `#endif ` – Zeyad Khattab Aug 17 '20 at 11:19
  • `uint8_t` is likely just a type alias for `unsigned char`. Similarly, `int32_t` for `int`. The problem is that you don't link the proper object file/library. – Daniel Langr Aug 17 '20 at 11:21
  • *undefined reference* is a linker issue, you have to **link** with appropriate library. see [what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). – Jarod42 Aug 17 '20 at 11:23
  • @Jarod42 yes, you are correct, thank you – Zeyad Khattab Aug 17 '20 at 11:27
  • There is almost never any point in using `malloc` in C++. – molbdnilo Aug 17 '20 at 11:27
  • what's a better alternative ? (I am new to c++) @molbdnilo I am familiar with std::vector , are there any other alternatives ? – Zeyad Khattab Aug 17 '20 at 11:29
  • `std::vector` is the best alternative (`std::vector I1(n * n);`, then pass `I1.data()`, and so on). If you desperately need manual memory management, `new uint8_t[n*n]` (and remember to `delete []`). – molbdnilo Aug 17 '20 at 11:32

2 Answers2

2

, I get the message undefined reference to process(unsigned char*, unsigned char*, float*, float*, int const*) Why does this happen?

It happens because the linker was not able to find the definition for the function process(...). If this function comes from a library, this means that you are not linking to the library which contains the definition for this function.

Also, uint8_t is just an alias for unsigned char in gcc.

Moreover, you shouldn't manually manage the memory when you have a very simple and safe solution in the form of std::vector. Code without any mallocs:

    size_t size = n * n;
    std::vector <uint8_t> I1 (size);
    std::vector <uint8_t> I2 (size);

    std::vector <float> D1(size);
    std::vector <float> D2(size);

    const int32_t dims[3] = {n,n,n};

    process(&I1[0], &I2[0], &D1[0], &D2[0], dims);
Waqar
  • 8,558
  • 4
  • 35
  • 43
  • 2
    `size = n * n * sizeof(float);` - these calculations no longer needs to be in bytes - it should be in elements (i.e. just `n * n`), to size the `vector`s properly. – Tony Delroy Aug 17 '20 at 11:47
0

Confusion between uint8_t and unsigned char ... Why does this happen?

Simply because uint8_t is a type alias of unsigned char on your system. These two are the same type. This is normal.

undefined reference to process ... Why does this happen?

It appears that you've failed to define the function that you call.


P.S. Avoid malloc in C++. Also avoid owning bare pointers. Your example program leaks memory.

what's a better alternative ? (I am new to c++)

Use std::vector.

eerorika
  • 232,697
  • 12
  • 197
  • 326