Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a C++ template library from a C program?

Tags:

c++

c

templates

My knowledge of C++ is limited. I understand that C++ libraries can be called from C provided a C interface is available. Is it possible to call a C++ template library defined in a .hpp file from a C library? For example, this wonderful expression evaluation library.

It may be time to learn C++! OK. Here is a first try:

#ifndef EXPRTK_C_H_
#define EXPRTK_C_H_
#include "exprtk.hpp"

//Header
#ifdef __cplusplus
extern "C" {
#endif

typedef exprtk::symbol_table<double>      symbol_table_t;
typedef exprtk::expression<double>          expression_t;
typedef exprtk::parser<double>                  parser_t;

  void add_variable_(symbol_table_t st, char* name, double *value);
  void register_symbol_table_(expression_t ex, symbol_table_t st);
  void compile_(parser_t parser, char *expression_string, expression_t ex);
  double evaluate_(expression_t ex);

#ifdef __cplusplus
}
#endif

#endif /* EXPRTK_C_H_ */


#include "exprtk.hpp"
#include "exprtk_c.hpp"

Here is the .cpp file. trig_function() is the real example, used in main(). What follows is my attempt at wrapping the functions. Error message is with parser.

exprtk.hpp:16962:7: error: ‘exprtk::parser<T>::parser(const exprtk::parser<T>&) [with T = double]’ is private
exprtk_simple_example_01.cpp:85:83: error: within this context
exprtk_simple_example_01.cpp:60:6: error:   initializing argument 1 of ‘void compile_(parser_t, char*, expression_t)’

template<typename T>
void trig_function() {
    std::string expression_string =
            "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";
    T x;
    exprtk::symbol_table<T> symbol_table;
    symbol_table.add_variable("x", x);
    symbol_table.add_constants();

    exprtk::expression<T> expression;
    expression.register_symbol_table(symbol_table);

    exprtk::parser<T> parser;
    parser.compile(expression_string, expression);

    for (x = T(-5.0); x <= T(+5.0); x += 0.001) {
        T y = expression.value();
        printf("%19.15f\t%19.15f\n", x, y);
    }
}



//Cpp file
void add_variable_(symbol_table_t st, char *varname, double *value) {
    const std::string varname_ = varname;
    bool r = st.add_variable(varname_, *value);
    printf("%d\n", r);
    r = st.add_constants();
}

void register_symbol_table_(expression_t ex, symbol_table_t st) {
    ex.register_symbol_table(st);
}
void compile_(parser_t parser,char *expression_string, expression_t ex) {
    const std::string expression_string_ = expression_string;
    parser.compile(expression_string_, ex);
}

double evaluate_(expression_t ex) {
    double val = ex.value();
    return (val);
}

int main() {
    trig_function<double>();

    double x;
    //exprtk::symbol_table<double> symbol_table;
    symbol_table_t symbol_table;
    add_variable_(symbol_table, "x", &x);
    symbol_table.add_constants();

    //exprtk::expression<double> expression;
    expression_t expression;
    register_symbol_table_(expression, symbol_table);

    //exprtk::parser<double> parser;
    parser_t parser;
    compile_(parser, "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)", expression);

    double q = -5.0, y = 0.0;
    for (x = q; x <= +5.0; x += 0.001) {
        y = evaluate_(expression);
        printf("%19.15f\t%19.15f\n", x, y);
    }
    return (0);
}
like image 354
Sue Avatar asked Jan 02 '14 00:01

Sue


People also ask

Can I use a CPP library in C?

Oracle Developer Studio C and C++ use the same C runtime libraries, as noted in the section about compatible compilers. Using Oracle Developer Studio compilers, you can therefore use Standard I/O functions freely in both C and C++ code in the same program.

Can STL be used in C?

C can't have an "exact equivalent" of STL because C doesn't have templates or classes.


1 Answers

Definitely learn C++.

You could hide the templates in a cpp file and expose an interface to use them:

//Header
#ifdef __cplusplus
extern "C" {
#endif
  void push(int elt);
  int pop();
#ifdef __cplusplus
}
#endif

//Cpp file
static std::deque<int> queue;

void push(int elt) {
  queue.push_back(elt);
}

int pop() {
  auto tmp = queue.front();
  queue.pop_front();
  return tmp;
}

You can't instantiate a template via C, but you can use a header file that takes advantage of templates behind the scenes and link everything together. That said, I strongly recommend leaving C in the past, and learning C++; it grants more power, speed, and safety.

like image 161
Mark Avatar answered Sep 18 '22 15:09

Mark