I'm experimenting with some sample projects for the stm32f4. I would like to build on some of these with some c++ code.
If I add a cpp file, the ide seems to correctly recognise it as such. And if I set language to auto (extension based) my cpp file will build, with a class in it, great.
But can I connect between them? Everything compiles (no warnings), but it fails at linking if I call the cpp function from c (no definition for )
Is there a way to call cpp from c and c from cpp?
Thanks
Yes, it's possible. However, you must explicitly tell the C++ that a function is a C function. You do this by declaring it as follows:
extern "C"
{
void my_function(void);
}
To ensure that header files work properly under both C and C++, they are typically written as:
#ifdef __cplusplus
extern "C"
{
#endif
void my_function(void);
#ifdef __cplusplus
}
#endif
Related
I have a open source C project and if I try to execute examples it is working fine but if i try to call those functions from C++ by using extern (including all headers with function definition) it is throwing error undefined reference to function_name. Can someone help me out how to properly wrap the C code using C++ and calling functions directly from C++ by just including headers of C?
You need extern "C" language linkage to declare C functions in your C++ code.
This (among other things) prevents the name mangling otherwise used by the C++ compiler for functions.
Commonly extern "C" is added using conditional compilation in header files by checking the existence of the __cplusplus macro:
#ifdef __cplusplus
extern "C" {
#endif
// Function declarations
#ifdef __cplusplus
} // End of the extern "C" block
#endif
This question already has answers here:
Combining C++ and C - how does #ifdef __cplusplus work?
(4 answers)
Closed 8 years ago.
I'm having a problem with eclipse CDT.
Where i am having a C++ project that uses the C FatFs library. I'm trying to implement the fatfs files.
Question: In multiple files i'm adding
#ifdef __cplusplus
extern "C"
{
#endif
// code..
#ifdef __cplusplus
}
#endif
wrapper. But for some reason, in the one .h file _cplusplus is defined, and in the other .h file the __cplusplus is not defined.
Any suggestions?
Can send screenshot for clarification.
Whether __cplusplus is defined or not depends on how the file that includes the header is being compiled. If the file is being compiled as C source (.c) it will not be defined. if the file is being compiled as C++ source (.cpp, .cc, or any other extension associated as a C++ source file) then __cplusplus will be defined.
Double check the file extensions and if necessary the settings in your project to ensure that the files are being compiled correctly.
Look here: Combining C++ and C — how does #ifdef __cplusplus work?
extern "C" doesn't really change the way that the compiler reads the
code. If your code is in a .c file, it will be compiled as C, if it is
in a .cpp file, it will be compiled as C++ (unless you do something
strange to your configuration).
What extern "C" does is affect linkage. C++ functions, when compiled,
have their names mangled -- this is what makes overloading possible.
The function name gets modified based on the types and number of
parameters, so that two functions with the same name will have
different symbol names.
Code inside an extern "C" is still C++ code. There are limitations on
what you can do in an extern "C" block, but they're all about linkage.
Also, you probably want two #ifdef __cpluspluss:
#ifdef __cplusplus
extern "C" {
#endif
// ...
#ifdef __cplusplus
}
#endif
Otherwise, your C code will never see your definitions.
What I want to do:
I have autogenerated C Code generated with Matlab Simulink and want to enhance it with some more functionality written in C++. To be exact, the C code calls a C-style API that internally uses C++. The whole thing is in a VS 2008 C++ project.
The problem:
It compiles, as long as I tell VS to compile it as C and leave out my C++ code. As soon as I compile it as C++ problems arise.
First of all, I can't compile it as C++ because math.h produces an error C2668 due to an ambiguous call to an overloaded function (fabs()).
If I now additionally add some C++, e.g. include iostream, I get hundreds of compiler errors complaining about missing curly braces and misplaced colons somewhere in cstdlib.
My question:
How can I mix the two languages in a way that works? I read about preprocessor defines (http://www.parashift.com/c++-faq-lite/overview-mixing-langs.html) but I don't know how to apply them correctly to solve my problem.
Any help is greatly appreciated!
It seems you are including C++ headers in your C source code. Probably indirectly by including it in other header files (i.e. the C source include your C++ header, and the C++ header includes other C++ header files).
There are two ways of solving this:
Use the preprocessor to conditionally include the C++ headers only when compiled in C++. This can be done like
#ifdef __cplusplus
# include some_cpp_header
#endif
Don't include C++ headers (directly or indirectly) in your header files. Or better, make a separate header file whose only purpose is to be included in the C source, and which only contains the function prototypes (with extern "C" when compiled as C++) of the API. The body of the header file could look like this
#ifdef __cplusplus
extern "C" {
#endif
void function1(int);
int function2(const char*);
/* More function prototypes */
#ifdef __cplusplus
}
#endif
I recommend the second method.
Include the <iostream> library and then you'll have to compile your code with a c++-compiler.
Just a small question:
Can C++ use C header files in a program?
This might be a weird question, basically I need to use the source code from other program (made in C language) in a C++ one. Is there any difference between both header files in general? Maybe if I change some libraries...
I hope you can help me.
Yes, you can include C headers in C++ code. It's normal to add this:
#ifdef __cplusplus
extern "C"
{
#endif
// C header here
#ifdef __cplusplus
}
#endif
so that the C++ compiler knows that function declarations etc. should be treated as C and not C++.
If you are compiling the C code together, as part of your project, with your C++ code, you should just need to include the header files as per usual, and use the C++ compiler mode to compile the code - however, some C code won't compile "cleanly" with a C++ compiler (e.g. use of malloc will need casting).
If on, the other hand, you have a library or some other code that isn't part of your project, then you do need to make sure the headers are marked as extern "C", otherwise C++ naming convention for the compiled names of functions will apply, which won't match the naming convention used by the C compiler.
There are two options here, either you edit the header file itself, adding
#ifdef __cplusplus
extern "C" {
#endif
... original content of headerfile goes here.
#ifdef __cplusplus
}
#endif
Or, if you haven't got the possibility to edit those headers, you can use this form:
#ifdef __cplusplus
extern "C" {
#endif
#include <c_header.h>
#ifdef __cplusplus
}
#endif
Yes, but you need to tell the C++ compiler that the declarations from the header are C:
extern "C" {
#include "c-header.h"
}
Many C headers have these included already, wrapped in #if defined __cplusplus. That is arguably a bit weird (C++ syntax in a C header) but it's often done for convenience.
I want to write a C code, say "test.c", and call some C++ functions from it.
I have a header file header.h which has functions defined in it
and a C++ file which has definitions of the functions.
I'm unable to figure out compilation commands and how to use the extern command.
Can someone clarify this?
I want to write a C code, say "test.c" call some C++ functions from it.
Try something like this in your header:
#ifdef __cplusplus
extern "C"
{
#endif
void foo (void);
#ifdef __cplusplus
};
#endif
Then implement foo() in your .cpp file. Make sure that your .cpp file also includes the header.