Initial fork

This commit is contained in:
Ricardo Mazeto 2020-01-15 12:32:27 -07:00
commit bddf45cafc
6 changed files with 90 additions and 0 deletions

BIN
bin/classEx Executable file

Binary file not shown.

3
build.sh Executable file
View File

@ -0,0 +1,3 @@
tcc -fpic -shared -o lib/libclass.so src/class.c
tcc -Llib -o bin/classEx src/classEx.c -lclass
LD_LIBRARY_PATH=lib bin/classEx Mazeto 28

BIN
lib/libclass.so Executable file

Binary file not shown.

41
src/class.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdlib.h>
#include <inttypes.h>
/* $CC -fpic -shared -o ../bin/libclass.so class.c */
/* typedef struct obj obj; */
typedef struct obj {
uint8_t * name;
uint8_t age;
} obj;
static void objInit(obj * this, uint8_t * name, uint8_t age){
(*this).name = name;
(*this).age = age;
};
obj * newObj(uint8_t * name, uint8_t age){
obj * obj = malloc(sizeof(obj));
objInit(obj, name, age);
return obj;
};
void setObjName(obj * this, uint8_t * name){
(*this).name = name;
};
void setObjAge(obj * this, uint8_t age){
(*this).age = age;
};
uint8_t * getObjName(obj * this){
return (*this).name;
};
uint8_t getObjAge(obj * this){
return (*this).age;
};
void delObj(obj * this){
free(this);
};

20
src/class.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef __CLASS_H__
#define __CLASS_H__
#include <inttypes.h>
typedef struct obj obj;
/* Constructor */
obj * newObj(uint8_t *, uint8_t);
/* Methods, setters and getters */
void setObjName(obj *, uint8_t *);
void setObjAge(obj *, uint8_t);
uint8_t * getObjName(obj *);
uint8_t getObjAge(obj *);
/* Destructor */
void delObj(obj *);
#endif

26
src/classEx.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdio.h>
#include <stdlib.h>
#include "class.h"
/* $CC -L/path/to/libclass.so -o classEx classEx.c -lclass
The shared object must be on the LD_LIBRARY_PATH
environment variable or in the folder /lib
For testing purposes, run this program with
>LD_LIBRARY_PATH="../lib" ../bin/classEx
*/
int main(int argc, char ** argv){
if(argc<2) return 1;
obj * myObj = newObj(argv[1], atoi(argv[2]));
printf("obj.name = %s\nobj.age = %d\n",
getObjName(myObj), getObjAge(myObj));
delObj(myObj);
return 0;
}