C / C ++使用的内联函数未定义的引用 [英] C/C++ unused inline function undefined reference

查看:2256
本文介绍了C / C ++使用的内联函数未定义的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下code(这不是PTHREAD具体;其他实例中,如那些涉及实时库,表现出类似的行为):

Consider the following code (this is not pthread specific; other examples, such as those involving the realtime library, exhibit similar behavior):

#define _GNU_SOURCE
#include <pthread.h>

inline void foo() {
    static cpu_set_t cpuset;
    pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
}

int main(int argc, char *argv[]) { }

这是C和C ++中的有效的程序。所以,我的这个内容保存到 testc.c testcpp.cpp ,并尝试建立。

This is a valid program in C and in C++. So I save the contents of this to testc.c and testcpp.cpp and try to build.

当我建立 C ++ 我没有得到任何错误。当我建立 C 我得到一个未定义的引用错误。现在,在 -O1 出现此错误,并在 -O3 。反正是有指示的gcc做正确的事(请参阅未使用,跳过 pthread_setaffinity_np )?

When I build in C++ I get no error. When I build in C I get an undefined reference error. Now, this error occurs in -O1 and in -O3. Is there anyway to instruct gcc to do the right thing (see that foo is unused and skip the requirement for a definition of pthread_setaffinity_np)?

编辑:我认为这是上下文明显的,但错误信息是:

I thought it was obvious from context, but the error message is:

/tmp/ccgARGVJ.o: In function `foo':
testc.c:(.text+0x17): undefined reference to `pthread_setaffinity_np'

请注意,由于不在主路径被引用, G ++ 正确忽略功能完全,但 GCC 没有。​​

Note that since foo isn't being referenced in the main path, g++ correctly ignores the function entirely but gcc does not.

编辑2:让我来试试这一次。功能,以及随后调用 pthread_setaffinity_np ,未使用。主要功能是空的。你看看吧!不知怎的, G ++ 想通了,没有必要列入,随后生成过程并没有绊倒时,我们特意省略 -lpthread (并检查与纳米导出的符号确认,无论是富< /还需要code>,也没有提及 pthread_setaffinity_np )。从 GCC生成的输出未对这一事实回升。

EDIT 2: Let me try this one more time. The function foo, and the subsequent call to pthread_setaffinity_np, is unused. The main function is empty. Just look at it! Somehow, g++ figured out that foo did not need to be included, and subsequently the build process did not trip up when we intentionally omitted -lpthread (and checking the exported symbols with nm confirms that neither foo nor reference to pthread_setaffinity_np were needed). The resultant output from gcc didn't pick up on that fact.

我问这个问题,因为在C ++以及C前端似乎给了相同的输入不同的结果。这似乎并不成为一个 LD 问题初步因为我希望这两个路径给予同样的链接错误,这就是为什么我强调的是,这似乎是一个编译器问题。如果这两个C ++和C了问题,那么是的,我会同意,它是一个连接问题。

I am asking this question because the C++ and the C frontends seem to give different results on the same input. This doesn't seem to be an ld issue prima facie because I would expect both paths to give the same linking error, which is why I emphasized that it seems to be a compiler issue. If both C++ and C gave problems, then yes I would agree that its a linking issue.

推荐答案

好吧,显然你的程序中包含一个错误:您声明并调用函数 pthread_setaffinity_np ,但你永远不定义它。显然,你忘了提供包含定义的库。这是C和C ++错误。

Well, apparently your program contains an error: you declare and call function pthread_setaffinity_np, but you never define it. Apparently you forgot to supply the library that contains the definition. This is an error in both C and C++.

在换句话说,这是的的C和C ++中的有效的程序。它违反了C的一个定义规则++(和任何类似的规则被称为C)。

In other words, this is not a valid program in C and in C++. It violates the One Definition Rule of C++ (and whatever the similar rule is called in C).

其余的取决于编译器是否会赶上这个错误,它发出诊断消息。虽然在形式上编译器应该抓住它,在现实中出现链接错误并不总是由编译过程捕获(在任期延长的含义,即包括链接以及)。

The rest depends on whether the compiler will catch this error and issue a diagnostic message for it. While formally the compiler is supposed to catch it, in reality linking errors are not always caught by the compilation process (in extended sense of the term, i.e. including linking as well).

无论他们被发现与否可能取决于许多因素。在这种特殊情况下,重要的因素是明显的C和C ++语言的内联函数的性质之间的差异。 (是的,他们是C和C之间真正的不同++)。我猜想,在C ++模式下,编译器决定,这inline函数并不需要实际的身体,而在C模式决定无论如何要产生身体。

Whether they are caught or not might depend on many factors. In this particular case the factor that matters is apparently the difference between the properties of inline functions of C and C++ languages. (And yes, they are really different between C and C++). I would guess that in C++ mode the compiler decided that this inline function does not need the actual body, while in C mode it decided to generate the body anyway.

所以,再次,如果这个节目,不知何故编译成功在某些情况下,这是唯一的,因为你真的很幸运。你似乎认为,这不叫功能应该是完全忽略。无论是C和C ++做出这样的保证。假设 pthread_setaffinity_np 的定义确实丢失,你的程序是C和C ++无效。出于这个原因,拒绝进行编译,编译器实际上是一个具有正确的行为。

So, again, if this program, somehow successfully compiles in some circumstances, it is only because you got lucky. You seem to believe that a function that is not called is supposed to be "ignored entirely". Neither C nor C++ make such guarantees. Assuming that the definition of pthread_setaffinity_np is indeed missing, your program is invalid in both C and C++. For this reason, the compiler that refused to compile it is actually the one with the correct behavior.

考虑到上述帐户,您可能要问自己是否真的在乎,为什么你有C和C ++模式的不同错误报告。如果你这样做,这将需要一些研究成具体实施的内部机制,不会有很多工作要做,与语言本身。

Taking the above into account, you might want to ask yourself whether you really care about why you got different error reports in C and C++ modes. If you do, it will require some research into the internal mechanics of that specific implementation and won't have much to do with the languages themselves.

这篇关于C / C ++使用的内联函数未定义的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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