简体   繁体   English

为什么有些 glibc 函数包含在头文件中,有些包含在共享库中?

[英]Why are some glibc functions included in header files and some are in shared libraries?

Simple example:简单的例子:

#include <iostream>

int main(){
 float a = std::min(1.0, 2.0);
 printf("hello\n");
 return 0; 
}

Compiling the above with -g and using gdb we can see that std::min is part of the final binary, provided by <iostream> and not in a shared lib:使用-g和 gdb 编译以上内容,我们可以看到std::min是最终二进制文件的一部分,由<iostream>提供,而不是在共享库中:

/usr/include/c++/6/bits/stl_algobase.h /usr/include/c++/6/bits/stl_algobase.h

template<typename _Tp>
_GLIBCXX14_CONSTEXPR
inline const _Tp&
min(const _Tp& __a, const _Tp& __b)
{
// concept requirements
  __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
//return __b < __a ? __b : __a;
  if (__b < __a)
    return __b;
  return __a;
}

However printf is linked dynamically and is part of the libc runtime, and does not exist within my final binary.但是printf是动态链接的并且是 libc 运行时的一部分,并且不存在于我的最终二进制文件中。

Why isn't std::min part of the runtime libc also?为什么std::min不是运行时 libc 的一部分?

Why isn't std::min part of the runtime libc also?为什么 std::min 不是运行时 libc 的一部分?

Firstly, libc is an implementation of the C standard library, and it doesn't have the namespace std because it is the standard library for the C language which is a language doesn't have namespaces.首先,libc 是 C 标准库的实现,它没有命名空间std因为它是 C 语言的标准库,而 C 语言是一种没有命名空间的语言。

Secondly, the C standard doesn't specify that the standard library has such function as min , so there is no reason for a C standard library implementation to provide such function.其次,C 标准没有规定标准库有min这样的功能,所以 C 标准库实现没有理由提供这样的功能。


In case you are wondering why the std::min template from the C++ standard library is compiled into your binary, it is because the instance of that template is an inline function.如果您想知道为什么 C++ 标准库中的std::min模板被编译到您的二进制文件中,那是因为该模板的实例是一个内联函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM