Pass generated C++ struct templates to a C header

See also “How can I pass an object of a C++ class to/from a C function?” (C++ FAQ), however I am unsure how to apply it to my case.

I’m trying to expose C-compatible types (structs) from my C++ source file.

I made them theoretically compatible by “casting” the struct to different ways. How do I make those “casted” types accessible to by C header c_header.h?

cpp_header.hpp

#ifdef __cplusplus

template <typename T>
struct item {
    uint32_t foo;
    T data;
};

int bar(struct item<T> *item);

extern "C" {

typedef struct item<uint8_t> u8_item_t;
typedef struct item<uint16_t> u16_item_t;

}

#endif // __cplusplus

c_header.h

#include "cpp_header.hpp"

int bar_u8(u8_item_t *item);
int bar_u16(u16_item_t *item);

cpp_source.cpp

#include "cpp_header.hpp"
#include "c_header.h"

template <typename T>
int bar(struct item<T> *item) {
    // ...
    return 0;
}

extern "C" {

int bar_u8(u8_item_t *item) { return bar<uint8_t>(item); }
int bar_u16(u16_item_t *item) { return bar<uint16_t>(item); }

}

When compiling, the first errors I see are

c_header.h : error: unknown type name 'u8_item_t'
   | int bar_u8(u8_item_t *item);
                ^~~~~~~~~
c_header.h : error: unknown type name 'u16_item_t'
   | int bar_u16(u16_item_t *item);
                 ^~~~~~~~~

9

The problem is that your typedefs for the C types are inside the C++ guard. Unfortunately, if you move them outside, you will get an error any time your are compiling C, because C does not have template types.

Probably your best choice is just to define C incomplete types. You will also need a separate C function for each variant of your template type you use because C can’t tell the difference -they are all pointers and it also doesn’t have parameter overloading.

#ifdef __cplusplus

template <typename T>
struct item {
    uint32_t foo;
    T data;
};

int bar(struct item<T> *item);

extern "C" {
#endif

typedef struct u8_item_t u8_item_t;
typedef struct u16_item_t u16_item_t;

int bar_u8(u8_item_t *item);
int bar_u16(u16_item_t *item);

#ifdef __cplusplus
}

#endif // __cplusplus

bar_u8 and bar_u16 are shim functions and their only purpose is to do the cast and call the template function.

4

I have found a way to resolve my problem thanks feedback from @PasserBy and @ÖöTiib

I started to mix the C++ and C code into one header file that can be included in either C or C++. I would be interested to know if this is a good idea or not (I’m obviously new to the C++ side ^^)

c(pp)_header.h

#ifdef __cplusplus

template <typename T>
struct item {
    uint32_t foo;
    T data;
};

typedef item<uint8_t> u8_item_t;
typedef item<uint16_t> u16_item_t;

#else // __cplusplus

// Opaque definitons for C
typedef struct u8_item u8_item_t;
typedef struct u16_item u16_item_t;

#endif // __cplusplus


#ifdef __cplusplus

template <typename T>
int bar(item<T> *item){
    // ...
    return 0;
}

extern "C" {
#endif // __cplusplus

int bar_u8(u8_item_t *item);
int bar_u16(u16_item_t *item);

#ifdef __cplusplus
}
#endif

I didn’t know about the limitations of templates for multi-file usage. which meant I had to put all the declarations in the .hpp and the C implementations in the .cpp

cpp_source.cpp

#include "c(pp)_header.h"

int bar_u8(u8_item_t *item) { return bar<uint8_t>(item); }
int bar_u16(u16_item_t *item) { return bar<uint16_t>(item); }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật