Importing a library with global arrays

This is a repost - other users cannot post replies in that forum.

In EasyC V4, I created a library to house all the things I normally use in competition projects, but when I go to import it, I get an error message “Invalid library gobal variable name!”
My library has global arrays, which I think may be the cause, but it also has some global variables of a user-defined type. Are the arrays the problem? How can I work around this? (The arrays don’t need to be visible to the user of the library, nor do the custom-type variables.)

Have you used this library before with other compilers? It sounds more like you may have named a variable the same as a reserved word or global that EasyC may itself be using. You need to isolate the error down to a specific line of code in the library, actually, is this a “real” library you are linking against or C code you are compiling as user functions?

No, it is an actual EasyC V4 Library, *.ECLX, and I made it on the same version of EasyC and same computer as the empty program I tried to import it in to.

To reproduce:
Make a new autonomous project, add one global array (eg char test[3]), save it as a library, then make a new competition project and try to import the library. This gives me the error message “Invalid library global variable name!”

Ok, I see the issue you are having, looks like a bug to me. A workaround would be to define the global arrays in UserInclude.h or just add them to the globals of the new code (ie. don’t define them in the library) this would depend on how many you have as to whether its viable, or perhaps they could be static arrays defined within the function that uses them. I have to admit that most non-robotics code I write generally tries to avoid globals, they have their uses but I prefer to pass parameters. If several functions want to operate on an array, each function can operate on a pointer to the array passed as a parameter.

Thanks, I was going to try that next - but, I have to use extern, so where do I put the actual definitions to make the linker happy?

The usage of the array is to store channel offsets for the analog joysticks to help with knowing the center of the joysticks without having to resort to deadbands. I could use a bunch of variables (Rem1Ch1, Rem1Ch2, …, Rem2Ch1, Rem2Ch2, …) but that would be a hassle.

Thanks for your help so far!

EDIT: I figured it out; I added a new source file and named it UserInclude.c (to match the header) and put the definition in there, with the extern statement in UserInclude.h for the rest of the source files to find it. Happy Linker = happy programmer :slight_smile:

EDIT: OK, well, now when I import the library, the UserInclude.h and UserInclude.c don’t carry over under “Library Files” - how can I make this happen? I would also like the ReadMe.txt to do the same…

So I’m not really an EasyC programmer, however, I would try something like this to avoid using globals at all.

Create a new project which will be the library

Add a new function called InitArray that returns an integer pointer, add a variable “static int MyArray[4]” and initialize it if needed. return “MyArray” from the function. The C code would be as follows.

int * InitArray ( void )
      static int MyArray[4] = {1,2,3,4}; 

      return MyArray ;

Create a new function called MyFunction or similar. Ad a variable which is an int pointer, get the array by calling InitArray and use the returned pointer as an array as normal. C code may look like (for example, just junk code here).

void MyFunction ( void )
      int *p; 

      // get the pointer to my "global" variables
      p = InitArray();
      PrintToScreen ( "hello\n" ) ;
      if ( p != NULL )
          PrintToScreen ( "array contents are: %d %d %d %d\n" , p[0], p[1], p[2], p[3] ) ;

          // use p as array
          p[1] = 123 ;

Save as a library.

Create new project, import library, use functions. I tested it and it works for me. Now I have to put RobotC back on my cortex.


Thanks, that’ll work great!

Now, one last question; EasyC is designed such that its auto syntax mechanic makes it very hard to use function pointers, but they are supported by the compiler. My solution is to use a typedef for a function pointer type and then use it like “VoidFUncVoid MyFunc = &ThatFunc;” and it works, but how can I get this to be imported with the library?

Thank you so much for your help so far :slight_smile:

Not quite sure what you are asking, you can always use the user code block, for example, add a user code block to define the variable that is a function pointer. Then another to assign to the function like this.

Edit: Actually the & is not needed, can just be func = MyFunction;

It compiles but I have not tested on hardware. Is that what you meant or did you want the function pointer as a global ?

Edit: I attached the test library I used.
easyc_3.jpg (2.16 KB)

Well, I would need to be able to accept the function pointers as function parameters. Is that possible without the typedef method? I can store them globally the same way as the array.

If it comes to it it would be acceptable to pass them as void pointers, but it’s just a preference to avoid that.


So you want to pass functions as parameters to another function to be used as callbacks? I would just send them as void pointers and then cast them before using as a callback.

An alternative to all this is to stop trying to use an EasyC library and turn all your code into C source files. Use EasyC to create the competition template code and then just jump into your own code bypassing all the EasyC flow chart limitations. Under the hood EasyC is using a gcc compiler, it creates a main.c, mainIO.c, globals.c and some headers but them compiles and links to any c files you create.

Thanks! I had forgotten about Templates - I think I’ll just do that. I appriciate all your help!