C Plugins
For this tutorial you will need a C compiler.
I will also include some information on a simple Makefile for C projects.
You will want a reliable way to backup your work!! Ex: GitHub, googlecode
There is a zip at the end of this post with all the files needed to start your C plugins (Doesn't include a C compiler or make)
Here is a template for main.c (Can be renamed to anything with a c extension)
PHP Code:
#include <main.h>
#include <string.h>
static __stdcall int a(void)
{
return 42;
}
static __stdcall int GetFuncCount(void)
{
return 1;
}
static __stdcall int GetFuncInfo(int x, void **addr, char **def)
{
switch (x) {
case 0:
*addr = (void *)&a;
strcpy(*def, "function a(): LongInt;");
break;
default:
x = -1;
}
return x;
}
and main.h
Note there is a little magic here to get rid of stdcall name mangling.
PHP Code:
#ifndef __MAIN_H_
#define __MAIN_H_
static __stdcall int a(void);
static __stdcall int GetFuncCount(void);
static __stdcall int GetFuncInfo(int x, void **addr, char **def);
int GetFunctionCount(void) __attribute__((alias("GetFuncCount@0")));
int GetFunctionInfo(int x, void **addr, char **def) __attribute__((alias("GetFuncInfo@12")));
#endif
Now you can ether decide on a Makefile or a simple sh/bat file.
Your simple bat/sh file will look something like this.
Code:
gcc -I. -c main.c -o main.o
gcc -shared -o libTest.dll main.o
You can also go the Makefile route.
For this method I ask you put all *.c files in src/
and all *.h files in includes/
Code:
.PHONY: all build clean dist todolist
PROJECT=cplugin4simb
CC=gcc
EXT=.so
ifeq ($(OS),Windows_NT)
EXT=.dll
endif
AUXFILES := Makefile README COPYING $(PROJECT)$(EXT)
PROJDIRS := src includes
SRCFILES := $(shell find $(PROJDIRS) -type f -name "*.c")
HDRFILES := $(shell find $(PROJDIRS) -type f -name "*.h")
OBJFILES := $(patsubst src/%.c,build/%.o,$(SRCFILES))
DEPFILES := $(patsubst src/%.c,build/%.d,$(SRCFILES))
SRCFILES := $(SRCFILES)
ALLFILES := $(SRCFILES) $(HDRFILES) $(AUXFILES)
WARNINGS := -Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align \
-Wwrite-strings -Wmissing-prototypes -Wmissing-declarations \
-Wredundant-decls -Wnested-externs -Winline -Wno-long-long \
-Wuninitialized -Wconversion -Wstrict-prototypes
CFLAGS := -g -std=c99 -O -I./includes -c $(WARNINGS)
all: clean build
build: $(PROJECT)$(EXT)
$(PROJECT)$(EXT): $(OBJFILES)
@$(CC) -shared -o $(PROJECT)$(EXT) $(OBJFILES)
%/.ph:
@mkdir -p $(@D)
@touch $@
build/%.o: src/%.c build/.ph
@$(CC) $(CFLAGS) $< -o $@
clean:
-@$(RM) $(wildcard $(PROJECT).tar.gz $(PROJECT)$(EXT))
-@$(RM) -r build
dist:
@tar czf $(PROJECT).tar.gz $(ALLFILES)
todolist:
-@for file in $(SRCFILES); do fgrep -H -e TODO -e FIXME $$file; done; true
Note this makefile will build your dll with ALL c files in src/ no need to change anything.
If you notice anything weird or wrong, please don't hesitate to ask questions.