当前位置:网站首页>GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions

GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions

2022-07-06 19:09:00 Small snails in big cities

Project scenario :

  • I have a problem recently , Let's simplify :
// int xxx();
 
int main()
{
    
    xxx();
}
  • Compile the , Natural compilation, but , The message is :
[email protected]:~/taoge/cpp/test$ g++ -c main.cpp 
main.cpp: In function ‘int main()’:
main.cpp:5:9: error: ‘xxx’ was not declared in this scope
     xxx();
         ^
[email protected]:~/taoge/cpp/test$ 
  • Open the above comment , obtain :
int xxx();
 
int main()
{
    
    xxx();
}
  • Compile the , Can you pass ? have a look :
[email protected]:~/taoge/cpp/test$ g++ -c main.cpp     
[email protected]:~/taoge/cpp/test$ 
  • Link up , Find out xxx() Undefined .
[email protected]:~/taoge/cpp/test$ g++ main.o
main.o: In function `main': main.cpp:(.text+0x5): undefined reference to `xxx()'
collect2: error: ld returned 1 exit status

So as to confirm the problems in the title .
My wonder at that time was , xxx Function has no definition , Can actually compile . It does . To distinguish between compilation and linking .
Take a closer look at the above English Tips , Compile not pass , It is shown that not declared, And the link doesn't pass , It is shown that undefined. therefore , At a glance .

原网站

版权声明
本文为[Small snails in big cities]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061116588904.html