1

My Problem is that I want to call some template function in C.

Therefore, I have declared an extern "C" function as a wrapper function, which calls this template function.

I know, that I need to compile the wrapper function, with the c++ compiler and then I get an object file, which I can link against a normal c-program.

The problem is, that I have written a header only C++ library, which I want to use in C, but in C I can't make the library header only anymore, once I need to link the C-program against the wrapper function.

So does there exist any way of generating a header only c-library, which calls the c++ template function? This method should work in GCC and it is absolutely no problem, if it is nonstandard, requires several pragmas, cli-parameters or something else.

I think, a possible solution would be to call gcc in C-mode on a file, where on a particular line, a switch from C to C++ inside the header file is possible. This C++ switch does the actual template call and than a switch back to the C-compiler is performed. Does some method exist?

e.g. like this:

#include <iostream>

#define SWITCH_TO_CPP_COMPILER() /*TODO: is this possible?*/
#define SWITCH_BACK_TO_C_COMPILER() /*TODO: is this possible?*/

#ifdef __cplusplus
extern "C" {
#endif
    inline bool wrapper();
#ifdef __cplusplus
}
#endif


SWITCH_TO_CPP_COMPILER();

template<bool tmp>
constexpr bool template_func() noexcept {
    return tmp;
}

inline bool wrapper() {
    return template_func<true>();
}

SWITCH_BACK_TO_C_COMPILER()

#include <stdio.h>

int main() {
    printf("%d\n", wrapper());
}

I have already looked at several questions, but they do not answer it:

C Wrapper for C++: How to deal with C++ templates?

how to write c wrapper around c++ code to expose class methods

Create a C wrapper around a C++ library that can be linked by a C linker

C wrapper around C++ library without unnecessary header files

byteunit
  • 991
  • 4
  • 15

2 Answers2

2

The whole point of the C wrapper is to have the C++ compiler generate an object module for the instantiated template and the wrapper, and letting the C compiler be blissfully unaware of anything but the wrapper, known/invoked thorough an extern function declaration.

This thing cannot be header-only: a header is brutally included in the file that uses it, and that file is going to be processed by a C compiler, which then would have to be able to parse/instantiate the C++ template besides the wrapper function, and this is obviously not possible. Parsing C++ is already complex as it is, requiring to switch to a different language in mid-air is completely bonkers.

If you want to use your library into a "C proper" program you have to have an extra TU compiled by a C++ compiler. Otherwise, since however you think about this you need some C++ compiler being able to compile your template, just compile everything in C++ and, if you prefer, just use the C subset for the rest of the files.

You already have a dependency from a C++ compiler and most probably from the C++ standard library, so this isn't going to change much besides compilation speed in the grand scheme of things.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
2

This in mind what Matteo wrote in his answer:

Convinced that what OP wants is impossible, I tried to find a substantive argument.

I compared the "Phases of translation" for C and C++.

The first phases concerning pre-processing look quite similar but starting with phase 7 (compilation) it becomes quite different.

I hardly doubt that any compiler can switch from C to C++ and back in the mid of a translation unit (if it is standard conform).

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56