0

I am programming a fighting game in C with a kind of API for allowing the add of plugins (shared library). Each plugin is a kind of artificial intelligence which can play the game. A decision can be taken each end frame during the game. My plugins has this kind of structure :

#include "acquisition.h"

void decision_frame(Decision_frame* d){

void* me = tell_me_who_i_am("my_plugin.so");

if (some_information_about(me) == foo){
    d->turn_left = 12;
  }
  /* bla bla bla */
}

So, I realize I am trying to implement some objects in non oriented object language and, due to that, the "void* me" is a variable which identify the caller. The header acquisition look like :

#ifndef _GUARD_
#define _GUARD_

typedef struct{
  int move;
  double angle_move;
  double angle_gun;
  double angle_radar;
  int shot;
}Decision_frame;

void* tell_me_how_i_am(char* plug_name);

Bar some_information_about(void* me);
Foo other_information_about(void* me);

#endif

My acquisition.c (kind of API part...) file currently contains that :

Space* get_space(void){
  static Space S;
  printf("Space adress (by getter) : %p\n", &S);
  return &S;
}

Finally, my main do that :

1 -> Space initialization (use of getter) to set a local variable in main pointing to the Space. (POINT ONE) 2 -> Do some allocation memory inside field of my struct Space. 3 -> recall of the getter after a bunch of dlopen over the plugins bar.so (POINT TWO)

I thus have by some trashly verbose :

Space address (by getter) : 0x6071c0 (POINT ONE)
Space address in main : 0x6071c0
Auto-loading of plugins...
2 potential plugins founds : 
- alea_all.so... loading OK
- full_dummy.so... loading OK
Space address (by getter) : 0x7ff4e6c650a0 (POINT TWO)
Space address in main : 0x6071c0

I imagine mixing position independent code and non position independent code is bad!!!!!!! I handle the compilation as follow :

bin/acquisition.o: src/acquisition.c includes/acquisition.h
      $(GCC) -fpic -c src/acquisition.c -o bin/acquisition.o $(CFLAGS)

plugins/full_dummy.so: plugins/full_dummy.c bin/acquisition.o
      $(GCC) -c -fPIC plugins/full_dummy.c -o plugins/full_dummy.o $(CFLAGS)
      $(GCC) -shared -o plugins/full_dummy.so plugins/full_dummy.o bin/acquisition.o $(CFLAGS)

I do not know : how much a -fpic code could depend from non -fpic code (because a lot of non fpic modules uses the static variables from acquisition.c) ? How much static variables could be used in this kind of pattern ?

And is there a trivial, safe and nice way to design this kind of problem in Pure C ? I mean, plugin some AI on a same program...

Sorry for my English and for being long...

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
user3005788
  • 45
  • 1
  • 1
  • 4
  • Please just ask one question at a time, this is far too broad for SO. – Jens Gustedt Mar 27 '14 at 13:25
  • I am sorry two times : first, your comment is true, the question is badly formulated and it is still hard for me to point out the real point of my problem second, the answer is relatively given in the answer of the question [Global variables, shared libraries and -fPIC effect](http://stackoverflow.com/questions/7298186/global-variables-shared-libraries-and-fpic-effect?rq=1) and the link inside the answer. I do not how to mark this question as invalid or doublon... – user3005788 Mar 27 '14 at 15:56

0 Answers0