Including C Headers Inside a C++ Program

Including C headers inside a C++ program

For a list of C standard C headers (stdio, stdlib, assert, ...), prepend a c and remove the .h.
For example stdio.h becomes cstdio.

For other headers, use

extern "C"
{
#include "other_header.h"
}

How do I use C Headers in a C++ Program?

error LNK2001: unresolved external symbol "void (__cdecl* b_evnt) 
(unsigned short,short *,union I_EVNT_T *,union O_EVNT_T *)"
(?b_evnt@@3P6AXGPAFPATI_EVNT_T@@PATO_EVNT_T@@@ZA)

That means, the C++ mangled variable b_evnt can't be found. That's true, because it should've been C mangled (just an _ prefix). To fix that, tell it to the compiler in the header when compiling for C++:

#ifdef __cplusplus
extern "C" BINDING_PROC_BEVNT b_evnt;
#else
extern BINDING_PROC_BEVNT b_evnt;
#endif

If that's all, you're done. If there are more symbols you need, you might want to use Greg's solution instead - but be aware that that is also not a fixall.

How can I include a C header that uses a C++ keyword as an identifier in C++?

Use a macro to rename the fields:

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wkeyword-macro"
#endif
#define explicit explicit_
#ifdef __clang__
#pragma clang diagnostic pop
#endif

#include <xcb/xkb.h>

#undef explicit

Using a keyword as a macro name is ill-formed in standard C++, but GCC, Clang (with pragma), and MSVC do accept it. (The standard says you "shall not" do it, cppreference clarifies that it means "ill-formed".)

Including C headers in a C++ namespace - is it a standard behavior?

You may even do strange things like

//test.c
int
#include "main.h"
{
return 1;
}

//main.h
main(void)

The preprocessor macros are expanded before any syntax check is done. The above example will expand to

int
main(void)
{
return 1;
}

which is legal code. While you really should avoid such examples, there are cases, where including into another element is quite useful. In your question it depends on how the names are mangled during compilation. If all the definitions in your header file are declared with extern "C", the names will be searched unmangled in the object file, this is, however, not the case if the object file containing the implementation does not use the same namespace as it's definition in the consuming code and does not declare it extern "C".

What's the benefit for a C source file include its own header file

The main benefit is having the compiler verify consistency of your header and its implementation. You do it because it is convenient, not because it is required. It may definitely be possible to get the project to compile and run correctly without such inclusion, but it complicates maintenance of your project in the long run.

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. When this happens, the code relying on the function with mismatch would still compile, but the call would result in undefined behavior. It is much better to have the compiler catch this error, which happens automatically when your source file includes its own header.

Header Files in C

A header file in C would declare the function add for those modules that need it, but not define the function. The function is still to be defined in its own module (e.g., in your case, add.c).

So in general, to make a function foo available to several modules, you would normally:

  • Choose a header file (maybe it's own if there are other associated
    defines, etc) to declare foo. For example, perhaps foo.h would
    have void foo(...);

  • In some module, perhaps foo.c, you would define the complete
    function foo.

  • In any module that wants to call foo, you would #include "foo.h"
    (or whatever header you used) and call the function.

  • When you compile/link the code, you would make sure all modules,
    including foo.o or whatever module has foo defined in it, were
    present.

A declaration, given in the header file, provides (of course) the function name, the function return type as well as listing all the parameters and their types. This is all the compiler needs to know to figure out how to call the function from the calling module. At link time, addresses are all resolved so that the modules then know exactly where the function is in its own particular module.

How to use a C header.h in multiple C++ files without getting multiple definition error?

You have two options:

  • You move the implementation of the function in a separate .c/.cpp file, leaving in the header file only the declaration. Then you compile this source file, and link it with the rest of the program. Example:

func.h

#ifndef func_h
#define func_h
#include<string>

void *funct(char *cName);
#endif

func.cpp

#include "func.h"

void *funct(char *cName)
{
std::string name = cName;
cName+= ".extension";
}

You compile and link as usual, e.g.

g++ -c -o func.o func.cpp
g++ -c -o main.o main.cpp
g++ -o prog main.o func.o
  • You specify the keyword inline in the definition of the function. This allows appearance of the function in multiple compile units. See this question.


Related Topics



Leave a reply



Submit