Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7687923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:48:31+00:00 2026-05-31T19:48:31+00:00

My project has a C++ library that I want to allow the user to

  • 0

My project has a C++ library that I want to allow the user to use via some programming language to be JIT’d to call functions in said library. For the sake of simplicity, assume the library has classes like:

class item {
public:
  item();
  item( int );
  ~item();
  // ...
};

class item_iterator {
public:
  virtual ~item_iterator();
  virtual bool next( item *result ) = 0;
};

class singleton_iterator : public item_iterator {
public:
  singleton_iterator( item const &i );
  // ...
};

I’m aware that LLVM doesn’t know anything about C++ and that one way to call C++ functions is to wrap them in C thunks:

extern "C" {

  void thunk_item_M_new( item *addr ) {
    new( addr ) item;
  }

  void thunk_singleton_iterator_M_new( singleton_iterator *addr, item *i ) {
    new( addr ) singleton_iterator( *i );
  }

  bool thunk_iterator_M_next( item_iterator *that, item *result ) {
    return that->next( result );
  }

} // extern "C"

The first problem is how to allocate an item from LLVM. I know how to create StructTypes and add fields to them, but I don’t want to have to parallel the C++ class layout — that’s tedious and error-prone.

The idea I got was simply to add a char[sizeof(T)] as the only field to a StructType for a C++ class type:

StructType *const llvm_item_type = StructType::create( llvm_ctx, "item" );
vector<Type*> llvm_struct_types;
llvm_struct_types.push_back( ArrayType::get( IntegerType::get( llvm_ctx, 8 ), sizeof( item ) ) );
llvm_item_type->setBody( llvm_struct_types, false );
PointerType *const llvm_item_ptr_type = PointerType::getUnqual( llvm_item_type );

I would think that, because it’s a StructType, the alignment would be correct and the sizeof(item) would get the size right. Will that work? Is there a better way?

The second problem is that, unlike the C++ class hierarchy, there’s no inheritance relationship between StructTypes. If I create a Function that takes an llvm_iterator_type but try to build a Function object using an llvm_singleton_iterator_type, the LLVM verifyModule() function complains at me:

Call parameter type does not match function signature!

So then I thought I’d simply use void* everywhere:

Type *const llvm_void_type = Type::getVoidTy( llvm_ctx );
PointerType *const llvm_void_ptr_type = PointerType::getUnqual( llvm_void_type );

but verifyModule() still complains at me because, apparently, there’s no automatic casting to void* types in LLVM. How can I solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:48:33+00:00Added an answer on May 31, 2026 at 7:48 pm

    It turns out that using char[sizeof(T)] is a reasonable way to gets StructTypes to be the correct size — at least one other person from the LLVM mailing list does this.

    As for the “Call parameter type does not match function signature!” errors, a solution to that is simply to have all thunks use void* and use static_casts inside. When passing arguments to the thunks, use the CreateBitCast() IRBuilder function (since casts-tovoid are not automatic in LLVM).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a maven project that has a set of library dependancies that are
I'm using a third party static library in my C++ project that has a
My project (an interpreted language) has a standard library composed by multiple files, each
Recently I found a C library that I want to use in my C++
I want to use the ASSIMP library http://assimp.sourceforge.net in an iOS project. Unfortunately, I'm
I have a library project that contains some samples in a subfolder. The library
In my current project I am using a 3rd party library which has no
Our project has one folder that is not part of the solution. How can
I have a class library that I have written in C#.net. I want to
I'm rewriting a JavaScript project, and I want to be able to use object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.