I have a library that I want to use which only provides C++ header files and a static library. Go is unable to parse the namespaces that it is wrapped in.
I have looked at this: How to use C++ in Go? and it makes sense, but there are no namespaces involved there.
This is the C++ code in question that when imported causes issues (only the beginning shown):
#pragma once
#include <stdint.h>
namespace ctre {
namespace phoenix {
And here is the result of compiling:
./include/ctre/phoenix/ErrorCode.h:4:1: error: unknown type name 'namespace'
namespace ctre {
^~~~~~~~~
./include/ctre/phoenix/ErrorCode.h:4:16: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
namespace ctre {
Is there any way that I can provide a C wrapper which avoids this problem?
I figured it out by creating a C wrapper header file. Then I created a CPP file which bridges said C interface with the library CPP headers and library.
The C header understands my library object as a void pointer, and my CPP implementation has to cast it in order to access all of its functions.
The extern "C" part is very important and prevent Go from freaking out - It prevents the CPP compiler from mangling function names.
Of course, also link the binary with the proper LDFLAGS.
phoenix.h
typedef void CTalon;
#ifdef __cplusplus
extern "C" {
#endif
CTalon* CTRE_CreateTalon(int port);
void CTRE_Set(CTalon* talon, double output);
void CTRE_Follow(CTalon* slave, CTalon* master);
#ifdef __cplusplus
}
#endif
phoenix.cpp
#include "phoenix.h" // My C wrapper header
#include "ctre/phoenix/motorcontrol/can/TalonSRX.h" // Actual CPP header from library
#define TALON(ctalon) ((ctre::TalonSRX*) ctalon) // Helper macro to make converting to library object easier. Optional
namespace ctre { // Specific to my library which has a lot of long namespaces. Unrelated to problem
using ctre::phoenix::motorcontrol::ControlMode;
using ctre::phoenix::motorcontrol::can::TalonSRX;
}
extern "C" {
CTalon* CTRE_CreateTalon(int port) {
return (CTalon*) new ctre::TalonSRX(port);
}
void CTRE_Set(CTalon* talon, double output) {
TALON(talon)->Set(ctre::ControlMode::PercentOutput, output);
}
void CTRE_Follow(CTalon* slave, CTalon* master) {
TALON(slave)->Follow(*(TALON(master)));
}
}
Related
I'm trying to wrap a C library in C++, to make it a modern, high level and idiomatic C++ library. What I want to do, is to make the C objects completely opaque and/or directly unavailable from the C++ code and wrap/replace them with higher-level alternatives.
The problem I'm facing with is simple: I want to include the C header only to the C++ source, so that the C++ header when included won't include the C header's declarations as well, that is, it won't pollute the global namespace.
But it looks like the correct separation of the header and source files does not allow me to do that. Here is a very much dummified version of my problem, the comments will tell you the rest:
my_header.h:
typedef enum
{
my_Consts_ALPHA = /* some special value */,
my_Consts_BETA = /* other special value */,
} my_Consts;
typedef struct
{
// members...
} my_Type;
void
my_Type_method(my_Type *const,
my_Enum);
my_header.hpp:
namespace my
{
enum class Consts; // <-- This header is missing the constant values of
// this enum, because its values are defined by
// the C header :(
class Type : public my_Type // <-- The super struct is coming from the
// C header, but I don't want to include
// that header here :(
{
public:
void
method(Consts constant);
};
}
my_source.cpp:
extern "C"
{
#include "my_header.h"
}
#include "my_header.hpp"
namespace my
{
enum class Consts
{
ALPHA = my_Consts_ALPHA,
BETA = my_Consts_BETA,
};
void
Type::method(Consts constant)
{
my_Type_method(static_cast<my_Type *const>(this),
static_cast<my_Consts>(constant));
}
}
So my questions are: am I missing something very obvious here? Is this even possible to achieve? Is there a trick that I'm not aware of?
In the comments of the question #AnalPhabet suggested sarcastically, that one should use #include of a C header inside a namespace. #n.m. confirmed, that it is actually a working solution, and now I tested it on my own setup, and fortunately it is working pretty fine.
(Although I have no idea, if this is implementation specific or not, but I tested on both g++ and clang++ and it is working.)
It does not solve the opaqueness problem, but at least it makes a bit harder to access to the raw C data directly as it is living in a separate namespace now, therefore the user can't accidentaly access, but willingly.
So, the my_header.hpp should look like this:
namespace my
{
extern "C"
{
#include "my_header.h"
}
enum class Consts
{
ALPHA = my_Consts_ALPHA,
BETA = my_Consts_BETA,
};
class Type : public my_Type
{
public:
void
method(Consts constant);
};
}
So wherever my_header.hpp is #include'd, the user can only access to the C values as follows:
my::my_Consts_ALPHA // The wrapped value is => my::Consts::ALPHA
my::my_Type // The wrapped value is => my::Type
my::my_Type_method(t,..) // The wrapped value is => t.method(..)
If the whole idea of writing high-level and idiomatic C++ wrapper is to bring safety, automatic memory management and convenient C++ types like std::sting, I would include C header into cpp file only.
Provide clean idiomatic C++ interface, and use C library only in the implementation.
Do not afraid to write a couple of utility functions that convert C data to C++ and back. If a C++ class should hold C-specific data, and it is not possible to replace it with C++ analog, use some type erasure technique to keep clean interface.
I wouldn't worry about performance due to such wrapping until I see it on top in a profiler log. In most cases it is not a bottleneck.
Again, splitting interface and implementation is usually a win.
UPDATE
Initially, I was thinking more about project specific C++ interface rather than universal C++ wrapper around C library.
Solution with extern "C" wrapped into a namespace looks correct to me (see §7.5 of C++11 standard). But, I've never seen this technique in the wild.
You can go further and add nested detail namespace to not pollute my namespace with C types. This trick is popular in header only libraries:
namespace my
{
namespace detail
{
extern "C"
{
#include "my_header.h"
}
}
enum class Consts
{
ALPHA = detail::my_Consts_ALPHA,
BETA = detail::my_Consts_BETA,
};
class Type : public detail::my_Type
{
public:
void
method(Consts constant);
};
}
Take into account that you can't make C functions completely opaque or wrap them to a single namespace when you link with static library. They have external linkage and know nothing about namespaces.
namespace A {
extern "C" void my_Type_method(my_Type *const, my_Enum);
}
namespace B {
extern "C" void my_Type_method(my_Type *const, my_Enum);
}
extern "C" void my_Type_method(my_Type *const, my_Enum);
Basically, all these declarations refer to the same C function. As C doesn't support namespaces and overloading, linker usually uses function names as unique identifiers (even argument types are ignored).
Anyway, this approach will help to avoid accidental access to C interface.
I'm not sure if it's language legal, but I think extern "C" is just there to unmangle functions, so as long as you keep them in the .cpp file you can get away with this.
This is a little profane, but it seems to work with gcc 4.3.5. It demonstrates that you can use C functions while also hiding them in a namespace.
I didn't bother with inheriting struct_t, but it should probably work. I have no idea if you can pull off the enum class.
foo.h
#ifndef foo_H
#define foo_H
typedef enum {
ALPHA,
BETA
} enum_t;
typedef struct
{
int i;
} struct_t;
void printit(struct_t print_me);
#endif // foo_H
foo.c
#include <stdio.h>
#include "foo.h"
void printit (struct_t print_me)
{
printf ("Hello World %d!\n", print_me.i);
}
bar.hpp
#ifndef bar_HPP
#define bar_HPP
namespace _foo {
// Don't need extern "C" since we're not using functions
#include "foo.h"
}
struct based_on_struct_t // : public _foo:struct_t // Do you really have to derive? It might be possible, but it's ugly
{
_foo::struct_t i;
double j;
based_on_struct_t (int _i, double _j) : j(_j) { i.i = _i; }
void print(void); // Gonna call printit, MUST be in .cpp
};
#endif // bar_HPP
bar.cpp
namespace _foo{
extern "C" {
#include "foo.h"
}
}
#include "bar.hpp"
#include <stdio.h>
void based_on_struct_t::print (void) {
// Call the old version...
printit(i);
// And do new crap
printf ("Goodbye World %d %f\n", i.i, j);
}
driver.cpp
#include "bar.hpp"
int main (void) {
based_on_struct_t B(10, .1);
B.print();
return 0;
}
Demo...
$ gcc foo.c -c -O3
$ g++ foo.o bar.cpp driver.cpp
$ ./a.out
Hello World 10!
Goodbye World 10 0.100000
$
I've found three tutorials that cover writing a parser in C++ using Bison: here, here, and here. The first two don't cover how to use a class as the return type of a rule. The third does cover it, but it doesn't appear to clearly explain the stipulations.
Here is a simple parser.yy file which uses a class in this manner.
%{
#include <stdio.h>
extern FILE * yyin;
int yyerror(char *s)
{
fflush(stdout);
printf("error\n");
}
int yywrap(void) {
return 1;
}
int yyparse() { return 1; }
int main() {
yyparse();
return 1;
}
class myclass { int x; };
%}
%union {
int token;
myclass * mc;
}
%token <token> REGISTER
%type <mc> start_sym
%start start_sym
%%
start_sym :
REGISTER '+' { $$ = new myclass(); }
;
Bison runs with no problems using this input. I defined a simple input for flex to use with this. However, when I try to compile it, I get the error:
$ g++ parser.tab.cc lex.yy.cc
In file included from lex.ll:11:0:
parser.yy:31:5: error: ‘myclass’ does not name a type
myclass * mc;
^
Where is the appropriate place to declare myclass?
The error is coming from your lex-generated file, and is the result of your not putting a definition for you class in a place where the lex-generated file can see it. Putting the definition in a separate header file and including that header file in both the scanner (flex) and parser (bison) files is the simplest solution; moreover, it makes sense because there are probably other compilation units -- the consumer of the parse -- which also require these headers.
If you are using a relatively modern version of bison, for the case of type definitions which really only need to be visible to the scanner and parser, and no other component, it is possible to get bison to insert definitions into the header file which it generates by putting a %code requires block into your prologue:
%code requires {
class myclass {
// ...
};
}
(You can also use a %code provides block, but normally you would want these definitions to be available when the semantic type union is defined, which is the location of %code requires.)
For more information, see the bison manual.
With respect to the extern "C" issue, I would have thought that to be unnecessary if you include the flex-generated header file in your bison input. You also might want to specify %option noyywrap in your flex input, unless you are actually using yywrap. The yywrap included in -lfl has "C" linkage, but as far as I know the extern "C" yywrap declaration does occur in the flex-generated header file.
Having said all that, I need to confess that I don't use the C++ APIs for either bison or flex. I prefer to just use the C interfaces; the resulting files can be compiled cleanly with a C++ compiler (at least with recent versions of the bison and flex tools), and you can use pointers to C++ objects in your semantic union without problems.
I've found that the only place that I can declare the class without getting a compilation error is in a separate header file ("myclass.h"). This header file must be included in both parser.yy and scanner.ll. In scanner.ll, it must be included before parser.tab.hh.
Additionally, the only way I've gotten it to compile without the compiler complaining about yyparse(), yylex(), or yywrap() is to include prototypes for these functions in the file myclass.h with external C linkage, like so:
#ifndef __MYCLASS_H_
#define __MYCLASS_H_
class myclass { int x; };
extern "C" {
int yyparse(void);
int yylex(void);
int yywrap(void);
}
#endif
If anyone is aware of a better solution, please do let me know.
I would like to write a c++ wrapper for a C API. for this it is most convenient to just include the C-API header in my own header, but this also includes the header into the file of the outer system, that should not be exposed to the C-API.
capi.h
enum MyFlags {
MY_FLAG_A,
MY_FLAG_B,
};
void FOO_bar(int flags);
cppapi.hh
#include "capi.h"
enum class MyFlags {
A = MY_FLAG_A,
B = MY_FLAG_B
};
namespace foo {
void bar(MyFlags flags) {
FOO_bar((int)flags);
}
}
it is just to translate c naming convention into c++ language features. So when using the C++ variant of the language, I would like that the c_api is not available to my auto completion and therefore not accidently used.
Define a namespace detail in your projects namespace, and include the C header there.
If the C header does not define en extern "C"- block when included from C++, do one of these:
Wrap with an external extern "C" block and verify the C header is, if included thus, valid and equivalent C++. (Special attention to enum sizes and inline functions).
Make it valid and equivalent C++ using #ifdef __cplusplus.
Thus, all those C functions are hidden in the internal implementation-detail namespace, and won't bother anyone.
namespace foo {
namespace internal {
extern "C" {
#include "capi.h"
}
}
// Function alias
using bar = internal::FOO_bar;
// Type alias
using config_t = internal::FOO_config;
}
Selectively get those symbols you need into the FOO-namespace using using.
BTW: Your forwarder-function should really be inline!
You should be able to arrange things using
#ifdef __cplusplus
/* C++ code here, including #include*/
#else
/*C code*/
#endif
I would like to know if this is the correct way to port a C library to C++; for this example I wrote a 2 line C header with a function and a typedef managing a pointer.
lib.h
#ifndef LIB_H
#define LIB_H
#include <math.h>
double foo(double a) { return (log(a)); }
typedef double (*PtoFoo)(double);
#endif // LIB_H
lib.hpp
#ifndef LIB_HPP
#define LIB_HPP
namespace lib {
extern "C" {
#include "lib.h"
}
}
#endif // LIB_HPP
and a little test in C++11
#include <iostream>
#include "lib.hpp"
#include <functional>
int main() {
// call to the function
std::cout << lib::foo(42354.343) << "\n";
// trying the pointer to function type
lib::PtoFoo ptr = lib::foo;
std::function<double(double)> f(ptr);
std::cout << f(342.4) << "\n";
return (0);
}
right now my attention is focused on pointers, function pointers and functions, but in general I would like to know if this is the correct way to port standard C code to C++ and using the new c++ interface with namespaces without possible backfires .
Yes, that is the correct way to do it.
Alternatively, you can use the __cplusplus preprocessor token to define the C++ interface in the same header file:
#ifdef __cplusplus
namespace lib {
extern "C" {
#endif
typedef ...
#ifdef __cplusplus
}
}
#endif
The advantage of this approach is that only a single header file is required.
As I commented, C++ is designed to be compatible with C, so just wrapping your C library headers with the appropriate extern "C" { and } /* end extern "C"*/; is enough.
However, you may want to design a C++ friendly interface to your library. This requires some thoughts, and some code (most of it in C++ specific header files). For instance, you may want to provide interfaces using C++ object, overloading, operator, and template facilities.
I can't help more, but look for examples at: libonion and its C++ bindings, GTKmm which is a large C++ binding to GTK (and companion libraries like Glib...), the C++ class interface to gmplib, or even the newest C++11 standard thread library (i.e. std::thread etc, etc...) which could be viewed as a clever C++ wrapping of pthreads ....
I believe that each library could have its own C++ wrapping... How to design that is up to you... (depends a lot on the wrapped library, your available time, and your fluency with C++...).
I'm not an expert C++ programmer, and i have been recently doing a trick thing in C++ which is causing me the below issue.
Objective of my task: Specific non system thread (cooperative threading actually) safe module is duplicated to create a system thread safe version to support different needs in the system. But instead of creating sys_XXX functions to keep the compatibility, we have decided to create a namespace to protect the system thread version in a C++ header file. I can actually include this in the CPP file and work happily but i just realised my funcInit call is not called before it reaches the CPP file control. Unfortunately this init for the cooperative threading version is in a C file. Now i need to init my system thread safe version from the same place, but im have been blocked by the compilation error which are hard to solve from my knowledge.
Compilation Error log:-
In file included from sysinit.c:87:
sys_unicode.h:39: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'SystemThreadUtils' <== corresponds to line [namespace SystemThreadUtils {]
sysinit.c:88:
sysinit.c:512: error: expected identifier or '(' before string constant <== corresponds to line [extern "C" bool SystemThreadUtils::funcInit(void);]
sysinit.c:513: error: expected identifier or '(' before string constant <== corresponds to line [extern "C" bool SystemThreadUtils::funcTerm(void);]
sysinit.c: In function 'SysInit':
sysinit.c:817: error: 'SystemThreadUtils' undeclared (first use in this function) <= corresponds to line [SystemThreadUtils::funcInit();]
sysinit.c:817: error: (Each undeclared identifier is reported only once
sysinit.c:817: error: for each function it appears in.)
sysinit.c:817: error: expected ')' before ':' token
sysinit.c: In function 'SysTerm':
sysinit.c:2737: error: expected expression before ':' token <== corresponds to line [SystemThreadUtils::funcTerm();]
sysinit.c:2737: warning: label 'SystemThreadUtils' defined but not used
Source and header snippets FYI :-
C header file (unicode.h):
// all functions called from the C source file
funcInit();
funcA();
funcB();
funcTerm();
C header file (unicode.c):
// all functions called from the C source file
funcInit() {
}
funcA() {
}
funcB() {
}
funcTerm() {
}
C++ header file (sys_unicode.h):
#include "unicode.h"
namespace SystemThreadUtils {
// below functions called from the C source file
extern "C" funcInit();
extern "C" funcTerm();
// below functions called from the CPP source file
funcA();
funcB();
}
C++ source definition (sys_unicode.cpp):
#include "sys_unicode.h"
namespace SystemThreadUtils {
// below functions are called from C source
funcInit() {
}
funcTerm() {
}
// below methods are called from CPP source
funcA() {
}
funcB() {
}
}
CPP source # 1 (utils.cpp):
#include "sys_unicode.h"
using namespace SystemThreadUtils;
utils::utils_init()
{
funcA();
funcB();
}
C source #2 (sysinit.c):
#include "sys_unicode.h"
extern "C" bool SystemThreadUtils::funcInit(void);
extern "C" bool SystemThreadUtils::funcTerm(void);
SysInit ()
{
funcInit(); // non system thread safe version
SystemThreadUtils::funcInit(); // system thread safe version
}
SysTerm ()
{
funcTerm(); // non system thread safe version
SystemThreadUtils::funcTerm(); // system thread safe version
}
You cannot use namespaces in C, extern "C" also is not allowed in C. So what could be a solution: Your C++ module defines a set of functions in namespace SystemThreadUtils that need to get called in C-Code. Since you cannot do that directly, you will have to write a C-conformant wrapper around them:
//C/C++ - header "sys_unicode_for_c.h"
#ifdef __cplusplus
extern "C" {
#endif
void STU_funcInit();
void STU_funcTerm();
#ifdef __cplusplus
} //end extern "C"
#endif
//C++-source: sys_unicode_for_c.hpp
#include "sys_unicode.h"
extern "C" {
void STU_funcInit() {
SystemThreadUtils::funcInit();
}
void STU_funcTerm() {
SystemThreadUtils::funcTerm();
}
}
Meaning: you need a header that is valid C code and declares a delegating function for each C++ function you need to call from C. The source is in C++ and just does the call.
See Calling C++ functions from C file as well.