c – 是否有解决未定义参考/未解决符号问题的一般指导原则?

前端之家收集整理的这篇文章主要介绍了c – 是否有解决未定义参考/未解决符号问题的一般指导原则?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有几个“未定义的引用”(在链接期间)和“未解析的符号”(在dlopen之后的运行时)我工作的问题.它是一个相当大的makefile系统.

是否有一般规则和指南来链接库和使用编译器标志/选项来规避这些类型的错误

解决方法

如果你使用MSVC:

你不能通过设置一个标志来逃避这种类型的错误:它意味着一些单位(.cpp)不具有声明的标识符的定义.它肯定是由某个地方缺少包含或缺少对象定义(通常是静态对象)引起的.

在开发过程中,您可以遵循这些准则(从those articles开始),以确保所有cpp包含他们需要的所有标题,但不再包括

  • Every cpp file includes its own header file first. This is the most@H_301_15@ important guideline; everything else@H_301_15@ follows from here. The only exception@H_301_15@ to this rule are precompiled header@H_301_15@ includes in Visual Studio; those@H_301_15@ always have to be the first include in@H_301_15@ the file. More about precompiled@H_301_15@ headers in part two of this article.
  • A header file must include all the header files necessary to parse it.@H_301_15@ This goes hand in hand with the first@H_301_15@ guideline. I know some people try to@H_301_15@ never include header files within@H_301_15@ header files claiming efficiency or@H_301_15@ something along those lines. However,@H_301_15@ if a file must be included before a@H_301_15@ header file can be parsed,it has to@H_301_15@ be included somewhere. The advantage@H_301_15@ of including it directly in the header@H_301_15@ file is that we can always decide to@H_301_15@ pull in a header file we’re interested@H_301_15@ in and we’re guaranteed that it’ll@H_301_15@ work as is. We don’t have to play the@H_301_15@ “guess what other headers you need”@H_301_15@ game.
  • A header file should have the bare minimum number of header files@H_301_15@ necessary to parse it. The prevIoUs@H_301_15@ rule said you should have all the@H_301_15@ includes you need in a header file.@H_301_15@ This rule says you shouldn’t have any@H_301_15@ more than you have to. Clearly,start@H_301_15@ by removing (or not adding in the@H_301_15@ first place) useless include@H_301_15@ statements. Then,use as many forward@H_301_15@ declarations as you can instead of@H_301_15@ includes. If all you have are@H_301_15@ references or pointers to a class,you@H_301_15@ don’t need to include that class’@H_301_15@ header file; a forward reference will@H_301_15@ do nicely and much more efficiently.

但是正如评论者的建议,似乎你正在使用g …

猜你在找的C&C++相关文章