Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inclusion of C headers in C++ revisited

I just read a question on SO discussing scenarios in which a piece of code is valid in both C and C++ but would produce different behavior in each language.

This begs the question: Could this ever be a problem when including C headers in C++ code?

I know from this question that you should include C headers like this:

extern "C" {
#include <your_os_or_library_header_in_c.h>
}

But all I found so far is that the extern "C" only guarantees that name mangling is turned off.

I couldn't find any information on whether it evaluates all statements as C, so that e.g. sizeof('a') or 10 //* comment */ 2 (which you could find in an inline function) are parsed as C and not C++. (Note that relying on such behavior as someone who writes a C header is obviously a bad idea, but I'm asking it from a purely academic standpoint of "What if?".)

Does the C++ standard say that enclosing a block of code in extern "C" means that all statements in it must be parsed as C?

like image 302
Alec Bender Avatar asked Nov 12 '13 12:11

Alec Bender


People also ask

Can you include C headers in C++?

You can #include them using their original names. #include <stdio. h> works just fine in C++. The C standard headers are required to work in standard C++, although you may be putting more than you like into the global namespace.

Should AC file include its own header?

If your file does not include its own header, you can accidentally get in a situation when forward declaration of a function does not match the definition of the function - perhaps because you added or removed a parameter, and forgot to update the header.

What are header files in C?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

Why are header files important in C?

Header files serve two purposes. System header files declare the interfaces to parts of the operating system. You include them in your program to supply the definitions and declarations you need to invoke system calls and libraries.


Video Answer


2 Answers

extern "C" is a C++ construct which affects language linkage in a C++ program. It does not switch the language to C in any way. All source text inside an extern "C" declaration is still parsed as C++ and as C++ only.

Note that extern "C" affects more than just name mangling - it is (theoretically) possible that functions with C and C++ language linkage could have different calling conventions, for example.

like image 177
Angew is no longer proud of SO Avatar answered Oct 23 '22 12:10

Angew is no longer proud of SO


extern "C" only changes linking, i.e. mangling and possibly the calling convention. Try compiling and running this program with both a C and a C++ compiler:

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif
    inline void putsizeofchar()
    {
        printf("%zd\n", sizeof(char));
    }
#ifdef __cplusplus
}
#endif

int main()
{
    putsizeofchar();
    return 0;
}

Since header inclusion is textual substitution in both languages, the lack of a header doesn't make any difference.

like image 3
Fred Foo Avatar answered Oct 23 '22 14:10

Fred Foo