I tested this using g++ and gcc and it worked! I used asm labels to give the symbol a custom name.
product.h
#ifndef BCC702_PRODUTO_H
#define BCC702_PRODUTO_H
#include <string>
#include <ostream>
class product {
public:
/// Create product
product(int id, double price, char* name);
/// Print product
void print() asm("Asm_product_print");
void set_name(char* name) asm("Asm_product_set_name") ;
private:
int id_;
double price_;
char* name_;
};
#endif //BCC702_PRODUTO_H
product.cpp
compiled to object using g++
#include "product.h"
#include <iostream>
void product::print() {
std::cout << name_ << std::endl;
}
product::product(int id_, double price_, char* _name_) :
id_(id_), price_(price_), name_(_name_) {}
void product::set_name (char* _name_) {
product::name_ = _name_;
}
main.c
compiled with gcc
typedef struct{
int id_;
double price_;
char* name_;
} product;
void Asm_product_set_name(product* p, char* string);
void Asm_product_print(product* p);
char* example = "hello world!";
int main(){
product p;
Asm_product_set_name(&p, example);
Asm_product_print(&p);
return 0;
}
Finally, link objects using g++
. And the test result is:
Hello world!
However, I'm not sure whether it will work with other c++ compilers.