是否有解决未定义的引用/未解决的符号问题的一般指南? [英] Are there general guidlines for solving undefined reference/unresolved symbol issues?

查看:212
本文介绍了是否有解决未定义的引用/未解决的符号问题的一般指南?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个未定义的引用(链接期间)和未解析的符号(在运行时dlopen)问题,我工作。这是一个很大的makefile系统。

I'm having several "undefined reference" (during linkage) and "unresolved symbol" (during runtime after dlopen) issues where I work. It is quite a large makefile system.

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

Are there general rules and guidelines for linking libraries and using compiler flags/options to evade these types of errors?

推荐答案

如果您使用MSVC:



您不能回避这种类型通过设置标志错误:意味着某些单位(.cpp)不具有已声明标识符的定义。

IF YOU WERE USING MSVC :

You cannot evade this type of error by setting a flag : it means some units (.cpp) dont' have definitions of declared identifiers. It's certainly caused by missing includes or missing object definitions (often static objects) somewhere.

在开发时,您可以遵循这些指南(从那些文章),以确保您的所有cpp包含他们需要的所有标头,但不再有:

While developing you can follow those guidelines ( from those articles ) to be sure all your cpp includes all the headers they need but no more :



  • 每个cpp文件首先包含自己的头文件。这是最重要的b $ b指南;一切
    从这里。唯一的例外
    这个规则是预编译头
    包括在Visual Studio;那些
    总是必须是
    文件中的第一个包含。有关本文第二部分中预编译的
    标头的更多信息。

  • 头文件必须包含解析它所需的所有头文件。
    这与第一个
    指南是密切相关的。我知道有些人尝试
    从来没有包括头文件
    头文件声明效率或
    这些行的东西。然而,
    如果一个文件必须包括在一个
    头文件可以解析之前,它必须包括
    包含在某个地方。
    的优点包括它直接在头文件
    文件中,我们总是可以决定
    拉入我们感兴趣的头文件
    ,我们保证它'll
    按原样工作。我们不必播放
    猜猜你需要什么其他标题
    游戏。

  • 头文件应该有最少的头文件数b $ b需要解析它。前面的
    规则说你应该拥有所有的
    包括你需要在头文件中。
    这条规则说,你不应该有任何
    比你多。显然,通过删除(或不添加在
    第一位)无用的
    启动
    语句。然后,使用尽可能多的forward
    声明,而不是
    包括。如果你有
    引用或指向类的指针,你
    不需要包括该类'
    头文件;


  • 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 ++ ...

But as commenter have suggested, it seem you're using g++...

这篇关于是否有解决未定义的引用/未解决的符号问题的一般指南?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆