简体   繁体   中英

Standard C++ way of calling C standard library functions

I have a few questions about calling C standard library functions in C++:

  1. If I want to call getline() which is declared in <stdio.h> is it always sufficient to just include <cstdio> ? This works with the compiler I'm using and I see that it includes <stdio.h> in <cstdio> but I want to know if the standard guarantees this.
  2. Are all C standard library functions guaranteed to be available in C++? With the getline() example from above I noticed that on cppreference under <cstdio> it doesn't list getline() .
  3. For C standard library functions and types that are made available in the std:: namespace like FILE or malloc() are they any problems with accessing them through the global namespace or is it just more idiomatic to access them as std::FILE or std::malloc() ?

You should always include what the documentation tells you to. (C++ standard library implementations will often be written such that functions are reachable via other includes but of course relying on that means your code is not portable.) Note that there is no standard getline function in C. But there is one in C++:

std::getline()

is defined in header <string> . The C++ standard doesn't guarantee that, in general, C functions are available at global scope or are even part of the C++ standard library. The two languages began their divergence many years ago and so the idea that C++ is in a sense a superset of C - libraries included - is a myth.

Reference: https://en.cppreference.com/w/cpp/string/basic_string/getline

In relation to #3 :

The .h libraries must place all its names in the global namespace and may also place them in the std:: namespace.

The c begining version must place all its names in the std:: namespace and may also place them in the global namespace.

It should be easy to link any C library with C++ in general, not just the standard headers.

Note that you may have to trawl through man to work out what version of what unix the method was introduced or if it is a specific extension, and decide for yourself if that historical startpoint is acceptable to you. But this is true if you wrote a C program instead of C++.

The C++ alias headers include most, but not all of the functionality from the C headers, but occasionally you may find the only way to get a function is to include the C header directly. On the other hand you need to ask yourself why they chose not to include that method, usually because it is deprecated, dangerous, or non-standard.

So the way it works, is that C functions, including C library functions are introduced with the extern "C" keyword.

When you #include C header files they will generally contain some code such as:

/* C++ needs to know that types and declarations are C, not C++.  */
#ifdef  __cplusplus
# define __BEGIN_DECLS  extern "C" {
# define __END_DECLS    }
#else
# define __BEGIN_DECLS
# define __END_DECLS
#endif

__BEGIN_DECLS

... which introduces a section of C interface functions.

If you needed to include a really old C library that did not do this, then you could easily add extern "C" around the #include:

extern "C" {
#include "ancientinterface.h"
};

You can also write your own methods that are "C" interface compatible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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