【问题标题】:Confusion between uint8_t and unsigned charuint8_t 和 unsigned char 之间的混淆
【发布时间】:2020-08-17 11:13:20
【问题描述】:

我正在尝试使用以下方法从头文件中调用void process (uint8_t* I1,uint8_t* I2,float* D1,float* D2,const int32_t* dims);

    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);

但是当我在 linux 上使用 g++ 编译时,我收到消息 undefined reference to process(unsigned char*, unsigned char*, float*, float*, int const*) 为什么会这样?

【问题讨论】:

  • 我在写C++,我想我之所以不认为我应该更多代码的原因是我一开始就不明白,为什么编译器无论如何都找不到函数过程它能做什么。在头文件的顶部有一些我认为值得一提的东西。 ` #ifndef _MSC_VER ` ` #include ` ` #else` ` typedef __int8 int8_t; `#endif
  • uint8_t 可能只是unsigned char 的类型别名。同样,int32_t 对应于 int。问题是您没有链接正确的目标文件/库。
  • undefined reference 是链接器问题,您必须 link 与适当的库。见what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix
  • @Jarod42 是的,你是对的,谢谢
  • 在 C++ 中使用 malloc 几乎没有任何意义。

标签: c++ compilation g++ uint8t


【解决方案1】:

,我收到消息 undefined reference to process(unsigned char*, unsigned char*, float*, float*, int const*) 为什么会这样?

这是因为链接器无法找到函数process(...)定义。如果这个函数来自一个库,这意味着你没有链接到包含这个函数定义的库。

另外,uint8_t 只是 gcc 中unsigned char 的别名。

此外,当您有std::vector 形式的非常简单且安全的解决方案时,您不应该手动管理内存。没有任何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);

【讨论】:

  • size = n * n * sizeof(float); - 这些计算不再需要以字节为单位 - 它应该在元素中(即只是 n * n),以正确调整 vectors 的大小。
【解决方案2】:

uint8_t 和 unsigned char 之间的混淆......为什么会发生这种情况?

仅仅是因为uint8_t 是您系统上unsigned char 的类型别名。这两个是同一类型。这是正常的。

对进程的未定义引用...为什么会发生这种情况?

您似乎未能定义您调用的函数。


附:在 C++ 中避免使用 malloc。还要避免拥有裸指针。您的示例程序泄漏了内存。

什么是更好的选择? (我是 C++ 新手)

使用std::vector

【讨论】:

    猜你喜欢
    • 2015-07-16
    • 2011-06-02
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 2011-10-23
    • 2013-04-14
    • 1970-01-01
    相关资源
    最近更新 更多