Using C code from C++ using autotools - c++

I am writing a (my first) C++ class on top of some code written in C, but I can only get the C++ to compile by declaring the C functions in a extern block. My project uses autotools; is there any way to automate this process so I don't have to maintain two header files?

Use a extern block inside a #ifdef in C codes header files
Start of header files
#ifdef __cplusplus
extern "C" {
#endif
...and at end of the header files
#ifdef __cplusplus
}
#endif
This way it will work for being included in both C and C++ sources

Yes create wrapper header files which include your C header files like so...
//Wrapper.h
#ifdef __cplusplus
extern "C"
{
#include "Actual.h"
}
#else
#include "Actual.h"
#endif
//Use.cpp
#include "Wrapper.h"
int main()
{
return 0;
}
//Use.c
#include "Wrapper.h"
/*or #include "Actual.h" */
int main()
{
return 0;
}

Use the C Preprocessor. Do something like this:
#ifdef __cplusplus
extern "C" {
#endif
// code goes here
#ifdef __cplusplus
}
#endif

We have a macro in a header file:
#ifdef __cplusplus
#define _crtn "C"
#else
#define _crtn
#endif
Then in header files, we can use _crtn:
#include "croutine.h"
extern _crtn void MyFunction( ... );
The only gotcha is to make sure you include the header file containing the prototype of MyFunction inside the file containing the implementation of MyFunction so that it is compiled with "C" linkage.
This is the same as #epatel's answer, but only requires the ugly #ifdef's in one header file.

Related

./maincpp.h(4): error: expected identifier or '('

#ifndef MAINCPP_H
#define MAINCPP_H
extern "C"
{ void displaylistfun(void);
void strfun(void);
}
#endif
Don't know why i am getting this error, I am trying to call Cpp function in .c file
extern "C" { ... } is a C++-specific thing, when you include the header file in a C source file you should be getting errors.
You need to use conditional compilation to have a header file that can be included in both C and C++ source files:
#ifndef MAINCPP_H
#define MAINCPP_H
#ifdef __cplusplus
// Only in C++
extern "C" {
#endif
void displaylistfun(void);
void strfun(void);
#ifdef __cplusplus
}
#endif
#endif // MAINCPP_H
Remember to include this header file on both the C++ and the C source files (so the C++ compiler knows that the functions are extern "C").

Undefined reference when use c-function from c++ file

I just begin new project in eclipse. Try to use C-library from C++ file.
In headers, where are prototypes of functions, here is:
#ifdef __cplusplus
extern "C" {
#endif
// prototypes...
void Init_Configuration(void);
#ifdef __cplusplus
}
#endif
It's initialize.h. In main.cpp i include this file and try use function:
#include "initialize.h"
int main()
{
Init_Configuration();
// Life cycle
while (1)
{
}
}
After compilation i get this error:
initialize.c:54: undefined reference to `SPI_Cmd'
In source C file "initialize.c", where i also include "initialize.h" i use function from C-library for stm32f2xx:
#include "initialize.h"
#include "stm32f2xx_rcc.h"
#include "stm32f2xx_spi.h"
#include "stm32f2xx_gpio.h"
// some code
SPI_Cmd(SPI1, DISABLE);
// some code
In this library also is this code:
#ifdef __cplusplus
extern "C" {
#endif
...
I don't know where a problem. Please, help!

C++ extern not found when in header

I am very new to C++ and I'm trying to do my best to have a good project structure from the beginning.
I am using the C library libjpeg and was including it in my .cpp with the use of
extern "C" {
#include <jpeglib.h>
}
It worked fine until I removed it to put it in a header file which now gives me the following errors:
inc/jpeg_utils.h: 6: inc/jpeg_utils.h: extern: not found
inc/jpeg_utils.h: 8: inc/jpeg_utils.h: Syntax error: "}" unexpected
My headerjpeg_utils.h :
#ifndef JPEG_UTILS_INCLUDE
#define JPEG_UTILS_INCLUDE
#include <stdio.h>
extern "C" {
#include <jpeglib.h>
}
int read_jpeg_file(char *filename, int decompression);
void write_jpeg_file(char *filename, unsigned char *image_buffer, int image_width, int image_height, int quality);
#endif
And at the top of jpeg_utils.cpp :
#include "../inc/jpeg_utils.h"
Did I misunderstand the use of a header ?
If you include jpeg_utils.h in a C file, that extern "C" directive will not compile (obviously, C is not C++).
Add a pre-processor directive to extern "C" only when in fact you compile as C++.
#ifdef __cplusplus
extern "C" {
#endif
#include <jpeglib.h>
#ifdef __cplusplus
}
#endif

How Do I layout Function Prototypes mixing C with C++

I have a header that I want to include from .c and .cpp files.
so I know about name mangling and extern "C" so...
#ifdef __cplusplus
extern "C"
{
int isPrime(int64_t p);
}
#endif
but when I include this in the .c file it doesn't see the function because of the #ifdef __cplusplus
so then I end up making 2 copies:
#ifdef __cplusplus
extern "C"
{
int isPrime(int64_t p);
}
#else
int isPrime(int64_t p);
#endif
is there a better way to do this... I thought about making another header called prototypes.h and including that in those 2 places... but is there something simple I am missing?
Yes, there is a better way. People are usually doing this :
#ifdef __cplusplus
extern "C"
{
#endif
int isPrime(int64_t p);
#ifdef __cplusplus
}
#endif
If you want to do something different in c and c++ (like for example this) then you can use the 2nd syntax in your question.
Another way of doing this is as follows
// intro
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif
// content
EXTERN_C int isPrime(int64_t p);
// many other functions declared with EXTERN_C
Normally the "intro" part is placed in some common header file for everyone to use.
The advantage of this format is that each declaration is immediately seen as extern "C". A reader doesn't have to search around for possible extern "C" {
#ifdef __cplusplus
extern "C"
{
#endif
int isPrime(int64_t p);
#ifdef __cplusplus
}
#endif
That way, you can extend the int isPrime(int64_t p); part arbitrarily without repeating it in your #ifdef and #else cases.

Call C++ class member function from C (Visual Studio)

I need to call a C++ member function from a C program.
I created .cpp/.h wrapper files in the C code, wrapping the C++ member functions.
i.e.- wrapper.cpp
#include "wrapper.h"
extern "C" {
void wrap_member1()
{
Class::member1();
}
void wrap_member2()
{
Class::member2();
}
}
and wrapper.h:
#include <windows.h>
#include <stdio.h>
#include "../C++ class with members I need to call.h"
extern "C" void wrap_member1();
extern "C" void wrap_member2();
My problem is when I complie:
error C2061: syntax error : identifier 'Class'
It points to the .h declaration of the C++ class as an error. Same result as if I did not have the wrapper files....?
P.S. I also removed the "extern "C" " from the prototypes and received an error on the wrapper function:
error C2732: linkage specification contradicts earlier specification for 'wrap_member1'
Any advice?
There are two issues:
One, you are including a C++ header file in a C header file. This means the C compiler gets C++ code. This is what causes the error you are experiencing. As Reed Copsey suggests, put the #include in the C++ source file instead of the C header file.
Two, you are using extern "C" in the C header file. Wrap your statement in an #ifdef as such:
#ifdef __cplusplus
extern "C" {
#endif
/* Functions to export to C namespace */
#ifdef __cplusplus
}
#endif
This will allow the file to be usable for both C and C++.
In your wrapper you must conditionaly compile the extern "C" part, because is a C++ only construct:
wrapper.h:
#ifdef __cplusplus
extern "C" {
#endif
extern void wrap_member1();
#ifdef __cplusplus
}
#endif
In the wrapper.cpp:
extern "C" void wrap_member1()
{
Class::Member1();
}
In your C module you include only wrapper.h and link to wrapper.obj.
BTW Objective-C is capable of consuming C++, just change the name of your file from *.m to *.mm in XCode.
You need to include your class in wrapper.cpp:
#include "wrapper.h"
#include "ClassHeaderFile.h" // The header that defines "Class"
extern "C" {
void wrap_member1()
{
Class::member1();
}
void wrap_member2()
{
Class::member2();
}
}