This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?

(34个答案)


在4个月前关闭。




endianness.cpp
#include "endianness.h"

#include <cstdint>

 using namespace io;

 void switch_endianness(uint16_t* n)
 {
     *n = (*n >> 8) | (*n << 8);
 }

 void switch_endianness(uint32_t* n)
 {
     ...
 }

 void switch_endianness(uint64_t* n)
 {
     ...
 }
字节序
#ifndef ENDIANNESS_H
#define ENDIANNESS_H

#include <cstdint>


namespace io
{
    void switch_endianness(uint16_t* n);

    void switch_endianness(uint32_t* n);

    void switch_endianness(uint64_t* n);
}

#endif
尝试在app.cpp中测试我的switch_endianness函数时,出现未解决的符号错误:
LNK2019 unresolved external symbol "void __cdecl io::switch_endianness(unsigned short *)" (?switch_endianness@io@@YAXPEAG@Z) referenced in function main
app.cpp
#ifndef TEST_BUILD

#include <iostream>
#include "io/endianness.h"

int main(int argn, char** argv)
{
    std::uint16_t y = 0x0000;
    io::switch_endianness(&y);
    std::cout << y;
}

#endif
到目前为止,我的理解和阅读方式是链接/引用问题。我认为我的代码很好,应该按预期进行编译和运行,我确实将“包含在项目中”添加到了endianness文件中,也许有些微不足道的内容我丢失或在引用时做错了吗?我真的似乎无法解决这个问题。

最佳答案

在我的endianness.cpp文件中,我将函数声明更改为包括它所在的文件夹目录,现在它可以工作了。(因为它在头文件中抱怨这一点,无法找到函数定义)

 void io::switch_endianness(uint16_t* n)
 {
     ...
 }

09-04 18:03