I have a static library of functions written in C. Let's say the header file is called myHeader.h and looks like:
#ifndef MYHEADER_H
#define MYHEADER_H
void function1();
void function2();
#endif
function1 and function2 aren't anything too special. Let's say they exist in a file called impl1.c which looks like:
#include "myHeader.h"
void function1() {
// code
}
void function2() {
// more code
}
All of the code mentioned so far is compiled into some static library called libMyLib.a. I'd rather not modify any of the code used to build this library. I also have a C++ header (cppHeader.h) that looks like:
#ifndef CPPHEADER_H
#define CPPHEADER_H
class CppClass {
private:
double attr1;
public:
void function3();
};
#endif
Then cppHeader.cpp looks like:
#include "cppHeader.h"
#include "myHeader.h"
// constructor
CppClass::CppClass(){}
void CppClass::function3() {
function1();
}
When I try to compile this, I get an error about an undefined reference to function1(). I believe that I've linked everything properly when compiling. I'm pretty rusty in my C++. I'm sure that I'm just doing something stupid. I hope that my simple example code illustrates the problem well enough.
Thanks in advance for any help!
The other solution (to the one suggested originally by Yann) is to surround your "C" header with:
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
Which saves you from having to remember to do:
extern "C" {
#include "foo.h"
}
every place you use foo.h
Make sure to use:
extern "C" {
#include "myHeader.h"
}
Or else the C++ compiler will generate symbol names which are name-mangled.
Related
I have a static C library that I want to port to C++, in the C library I got some global variables that store some common data used by the functions for example:
// global variable in the C library
int global_number_of_cpu_cores;
init_global_vars()
{
global_number_of_cpu_cores = get_info();
}
void lib_function()
{
// use global_number_of_cpu_cores
}
when using the library, it must be first initialized by the init function but in C++, the object's constructors are executed before the main function, so I cannot code:
class class_lib
{
class_lib()
{
// use global_number_of_cpu_cores but this is uninitialized!
}
}
Also, you can initialize global variables with functions:
int program_var = lib_function(); // lib_function uses global_number_of_cpu_cores but this is unintialized!
what is a decent/elegant way to solve this when designing a C++ library?
how do well-designed C++ libraries like Boost, Qt, etc solve this? any idea?
What you can do - simply create a C wrapper interface for C++ I.e. something like following:
In header file, e.g. foo.h
#ifndef __FOO_H_INCLUDED__
#define __FOO_H_INCLUDED__
#ifdef __cplusplus
extern "C" {
#endif
void print_logical_cpus()
#ifdef __cplusplus
}
#endif / * extern "C" */
#endif / * __FOO_H_INCLUDED__ */
in the implementation file e.g. foo.cpp
#include "foo.h"
#include <cstdio>
#include <thread>
struct GlobalSettings {
GlobalSettings():
logical_cpus(std::thread::hardware_concurrency())
{}
std::size_t logical_cpus;
};
static GlobalSettings __gsettings;
extern "C" {
void print_logical_cpus()
{
std::printf("Logical cpus %z", __gsettings.logical_cpus);
}
}
If you still need to port on raw C, there are compiler specific tricks to run functions before and after main.
GCC uses attribute((constructor)) and attribute((destructor)))
MS VC++ uses __declspec(allocate(".CRT$XLC"))
I am trying to compile an application involving both C and C++ files. With one particular header I face issues. The file in question (a C++ header file), would look something like this:
#ifndef TASK_H
#define TASK_H
#include "MyCCPObject.h"
int foo1(int);
int foo2(int);
int fooObject(MyCCPObject myCppObject); // Function involves a Class "MyCCPObject" type
#ifdef __cplusplus
extern "C" {
#endif
int foo3(void); // Function called in a C file
#ifdef __cplusplus
}
#endif
#endif //TASK_H
I have a function fooObject() which has a MyCCPObject class type as a parameter. Also, one of the functions, foo3() would be called from a C file.
When the C compiler, compiles this header, I get the following error: "error: #20:identifier "class" is undefined". To avoid this, I had to:
Place the fooObject() declaration within compiler guards:
#ifdef __cplusplus
int fooObject(MyCCPObject myCppObject);
#endif
Place compiler guards also in the class declaration in the header file MyCCPObject.h:
#ifdef __cplusplus
class MyCCPObject
{
public:
MyCCPObject(uint32_t val);
private:
uint32_t value;
};
#endif
Note: The MyCCPObject would not be called in any C file.
So, what would be a better approach, when I have a C++ header file, which involves:
Function would involves a class object
A extern call to a C file
Use separate headers for your C and C++ code.
Move the foo3 declaration (including the __cplusplus guards) inside a separate header. Let's call it Foo3.h
You now have the following files:
Task.h - contains the declarations for foo1 and foo2, fooObject and includes MyCCPObject.h
Foo3.h - contains the declarations for foo3
Task.cpp - includes Task.h and Foo3.h and provides definitions for foo1, foo2 and foo3
App.c - includes Foo3.h and uses foo3
From your build system (make, cmake etc.), when building the C++ library, add the files Task.h, Foo3.h, Task.cpp (and the other files related to MyCCPObject)
When building the C application, add only Foo3.h and App.c. That way, the other headers (which contain C++ code) will not be compiled and hence not give out any errors.
The makers of C++ wrote a FAQ which is also giving some guidance on how to mix C and C++. They are also looking at the possibility to use C++ objects from C code.
Option 1: If you just want the C compiler to be able to parse your task.h header file, then you could hide the C++ parts my using #ifdef __cplusplus:
#ifndef TASK_H
#define TASK_H
#ifdef __cplusplus
#include "MyCCPObject.h"
int foo1(int);
int foo2(int);
int fooObject(MyCCPObject myCppObject); // Function involves a Class "MyCCPObject" type
extern "C" {
#endif
int foo3(void); // Function called in a C file
#ifdef __cplusplus
}
#endif
#endif //TASK_H
Option 2: If you want to make the fooObject function accessible from C, then you can change MyCppObject.h to provide the full class information to C++ and only a minimal typedef for C. The typedef makes sure that C understands just the class name MyCCPObject without writing class or struct before it.
#ifdef __cplusplus
class MyCCPObject
{
public:
MyCCPObject(uint32_t val);
private:
uint32_t value;
};
#else
typedef struct MyCCPObject MyCCPObject;
#endif
and task.h to
#ifndef TASK_H
#define TASK_H
#include "MyCCPObject.h"
int foo1(int);
int foo2(int);
#ifdef __cplusplus
extern "C" {
#endif
int fooObject(MyCCPObject *myCppObject); // Function involves a Class "MyCCPObject" type
int foo3(void); // Function called in a C file
#ifdef __cplusplus
}
#endif
#endif //TASK_H
Please note that I needed to change the signature of fooObject to take a pointer to the object as the C code does not see the complete class and does not know the size of the object.
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!
How should I call a C++ function (no classes involved) from a Swift file?
I tried this:
In someCFunction.c:
void someCFunction() {
printf("Inside the C function\n");
}
void aWrapper() {
someCplusplusFunction();
}
In someCpluplusfunction.cpp:
void someCplusplusFunction() {
printf("Inside the C++ function");
}
In main.swift:
someCFunction();
aWrapper();
In Bridging-Header.h:
#import "someCFunction.h"
#import "someCplusplusFunction.h"
I found this answer very informative, but still I cannot make it work.
Could you point me in the right direction?
Thanks!
What does the header look like?
If you want to explicitly set the linking type for C-compatible functions in C++, you need to tell the C++ compiler so:
// cHeader.h
extern "C" {
void someCplusplusFunction();
void someCFunction();
void aWrapper();
}
Note that this isn't valid C code, so you'd need to wrap the extern "C" declarations in preprocessor macros.
On OS X and iOS, you can use __BEGIN_DECLS and __END_DECLS around code you want linked as C code when compiling C++ sources, and you don't need to worry about using other preprocessor trickery for it to be valid C code.
As such, it would look like:
// cHeader.h
__BEGIN_DECLS
void someCplusplusFunction();
void someCFunction();
void aWrapper();
__END_DECLS
EDIT: As ephemer mentioned, you can use the following preprocessor macros:
// cHeader.h
#ifdef __cplusplus
extern "C" {
#endif
void someCplusplusFunction();
void someCFunction();
void aWrapper();
#ifdef __cplusplus
}
#endif
In my Eclipse C/C++ project there is this undefined reference error which doesn't go away, doesn't matter what I do. I already changed the link order, checked if all files are compiled and included, basically everything, which is recommended in the in the Internet, when facing this problem.
Here's my program:
[...] - means that there is more code, which doesn't relate.
menulib.h
[...]
void start_GUI( void );
[...]
start_GUI.c - Note this is a .c-File
#include "menulib.h"
void start_GUI( void )
{
[...]
}
coreInterface.h
#ifndef COREINTERFACE_H_
#define COREINTERFACE_H_
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
/** only include the Core when compiled for C++ */
#include "Core.h"
#endif /* #ifdef CORE */
#ifdef __cplusplus
extern "C" {
#endif
void init_GUI( Core* core);
#ifdef __cplusplus
}
#endif
#endif /* COREINTERFACE_H_ */
coreInterface.cpp - Note this is a .cpp-File
#include "coreInterface.h"
#include "menulib.h"
void init_GUI( Core* core)
{
gui_core = core;
start_GUI(); // <--- **error appears here** - calls a .c file
}
Here's the error in the makefile (the auto generated one from Eclipse):
g++ -L/home/PC/the_Project/menu_GUI -o "the_Project" [...] ./menu_GUI/coreInterface.o [...] ./menu_GUI/start_GUI.o [...] -lcurses
./menu_GUI/coreInterface.o: In function `init_GUI':
/home/PC/the_Project/Default/../menu_GUI/coreInterface.cpp:23: undefined reference to `start_GUI()'
I believe this is coming from merging the .c and .cpp files. Still I couldn't solve it. Has anyone an idea?
Kind Regards
The problem is that you share the menulib.h header between C and C++ without specifying to C++ compiler that the function start_GUI is defined in C. Use extern "C" in #ifdef not only for init_GUI, but also for start_GUI.
Working example:
#include "coreInterface.h"
extern "C" {
#include "menulib.h"
}
void init_GUI( Core* core)
{
gui_core = core;
start_GUI();
}