The situation and solution of Link2019 in C++

1. Link2019 appears

  • Error message: LNK2019 unresolved external symbol "void __cdecl Log(char const *)" (?Log@@YAXPEBD@Z), which is referenced in function main
  • The reason is encountered, because during the linking process, the method body of the declared function cannot be found
  • Code Situation:
    File A:
    #include <iostream>
    
    // 这里定义了一个Logr()函数,但是不存在Log()
    void Logr(const char* message) {
        std::cout << message << std::endl;
    }
    file B:
     
    #include <iostream>
     
    // 声明Log函数,但是没有方法体,链接会在编译后的obj文件里面,进行寻找
    // 如果没有找到,就会出现Link2019
    void Log(const char* message); 
    int main() {
        Log("Hello world!");
        std::cin.get();
    }
    
    
  • Solution:
    1. In file B, stop calling the Log() function. The link will not look for functions that have not been called.
    2. Give the Log() function a method body, and change the Logr() function to Log()
  • Supplement: return value function function name (parameter, parameter,...) definition must be the same, otherwise the link cannot be found
    void Log(const char* message); 
    void Log(const char* message,int num); 
    
    // 两个函数不一样,因为参数不同

Guess you like

Origin blog.csdn.net/dantui_/article/details/130114440