A while back I wrote an article on using inline templates. It's a bit of a niche article as I would generally advise people to write in C/C++, and tune the compiler flags and source code until the compiler generates the code that they want to see.
However, one thing that I didn't mention in the article, it's implied but not stated, is that inline templates are defined as C functions. When used from C++ they need to be declared as extern "C", otherwise you get linker errors. Here's an example template:
.inline nothing nop .end
And here's some code that calls it:
void nothing();
int main()
{
nothing();
}
The code works when compiled as C, but not as C++:
$ cc i.c i.il $ ./a.out $ CC i.c i.il Undefined first referenced symbol in file void nothing() i.o ld: fatal: Symbol referencing errors. No output written to a.out
To fix this, and make the code compilable with both C and C++ we use the __cplusplus feature test macro and conditionally include extern "C". Here's the modified source:
#ifdef __cplusplus
extern "C"
{
#endif
void nothing();
#ifdef __cplusplus
}
#endif
int main()
{
nothing();
}
No comments:
Post a Comment