Vivado HLS and the “no function body” error: Using a C++ function in plain C code

This post was written by eli on December 31, 2016
Posted Under: FPGA,Vivado

It’s quite well-known, that in order to call a function in C, which has been compiled in a C++ source file, there’s need for an extern “C” statement.

So if this appears on a C++ source file:

void my_puts(const char *str) {
 ...
}

and there’s an attempt to call my_puts() in a plain C file, this will fail with a normal compiler as well as HLS.

In HLS, specifically, the function call in the C file will yield an error like

ERROR: [SYNCHK 200-71] myproject/example/src/main.c:20: function 'my_puts' has no function body.

The thing is, that just adding an extern “C” in the .cpp file will not be enough. The “no function body” error will not go away. What’s required is setting the namespace to hls as well. Something like this:

namespace hls {

extern "C" {

void my_puts(const char *str) {
 ...
}

}
}

The inspiration for this solution came from the source files of the HLS suite itself. I don’t know if this is really a good idea, only that it works.

Add a Comment

required, use real name
required, will not be published
optional, your blog address