I’m writing a compiler for a special purpose language in LLVM. I want to add bindings for a library that is already written in C++. My idea is to compile the library to LLVM bytecode (using clang -emit-llvm -S abc.c) and link it during compilation. This works well for code like
// lib.c
int f() {
return 123;
}
But parts of the library are written like
// A.cc
class A {
public:
int f() { return 123; }
};
Which results in empty bytecode files. I know that I can fix this by separating the implementation:
// A.cc
class A {
public:
int f();
};
int A::f() {
return 123;
}
But that would be a lot of tedious work. Is there any way to create useful bytecode from my library sources as they are? Or any other way to make the library available in my compiler?
You could see whether clang honours external linkage for explicit template instantiations. This might apply to non-templates, but otherwise you could ‘force it’ to work for templates.
Simple synopsis:
lib1.h
add a file
lib1_instantiate.cppThis should instantiate the named templates with external linkage.
If you’re stuck with a non-template class, and the trick above doesn’t work for that, you might wrap it like so:
instantiate.cpp:If you’re out of luck you’ll have to ‘use’ the inline members for them to get external linkage. A common trick is to use the address of these members (you won’t need to implement delegating stuff):
instantiate.cpp:However
HTH