加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:21:56 所属栏目:百科 来源:网络整理
导读:我有几个“未定义的引用”(在链接期间)和“未解析的符号”(在dlopen之后的运行时)我工作的问题.它是一个相当大的makefile系统. 是否有一般规则和指南来链接库和使用编译器标志/选项来规避这些类型的错误? 解决方法 如果你使用MSVC: 你不能通过设置一个标志
我有几个“未定义的引用”(在链接期间)和“未解析的符号”(在dlopen之后的运行时)我工作的问题.它是一个相当大的makefile系统.

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

解决方法

如果你使用MSVC:

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

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读