Hide structure definition in static library

I need to provide the C static library to the client and need to be able to make the structure definition inaccessible. In addition, I need to be able to execute code before the main when initializing the library using a global variable.

Here is my code:

private.h #ifndef PRIVATE_H #define PRIVATE_H typedef struct TEST test; #endif private.c (this should end up in a static library) #include "private.h" #include <stdio.h> struct TEST { TEST() { printf("Execute before main and have to be unavailable to the user.\n"); } int a; // Can be modified by the user int b; // Can be modified by the user int c; // Can be modified by the user } TEST; main.c test t; int main( void ) { ta = 0; tb = 0; tc = 0; return 0; } 

Obviously this code does not work ... but show me what I need to do ... Does anyone know how to make this work? I google quite a bit, but can not find the answer, any help would be greatly appreciated.

TIA!

+6
c struct static static-libraries
source share
3 answers

If you use gcc, you can use the constructor attribute,

 void runs_before_main(void) __attribute__((constructor)) { ... } 

From gcc documentation

The constructor attribute calls a function that will be called automatically before execution enters main (). Similarly, a destructor attribute calls a function call automatically after main () has completed or completed (). Functions with these attributes: useful for initializing data that will be used implicitly during program execution.

You can specify an optional integer priority to control the order in which the constructor and destructor functions are executed. A constructor with a lower priority number is executed before a constructor with a higher priority number; opposite ratio for destructors. So, if you have a constructor that allocates a resource and a destructor that frees the same resource, both functions usually have the same priority. The priorities for the constructor and destructor functions are the same as for the namespace scope C ++ Objects

If you want to hide the structure from users, declare the structure in the header, but define it in the c file, skipping pointers. As an example:

 // foo.h typedef struct private_foo foo; foo * create_foo(void); void free_foo(foo * f); // foo.c struct private_foo { int i; } foo * create_foo(void){ foo * f = malloc(sizeof(*foo)); if (f) f->i = 1; return f; } ... 

foo->i cannot be drawn outside of foo.c

+7
source share

If you want client code to be able to use "ta = ...", you cannot hide the structure definition. What you want is called an opaque type, which will look something like this:

  public.h:
 struct foo;
 set_a (struct foo *, int);
 struct foo * new_foo (void);

 main.c:
 #include <public.h>
 int main (void)
 { 
     struct foo * k;
     k = new_foo ();
     set_a (k, 5);
 }

The structure definition is available only to the library. If you do not make the library source code available, you can completely hide it from library users.

+3
source share

There is no portable way in C for your code to work before main() . I would just maintain the initialised flag in your library, set it to false, and then refuse to do anything until your init function is called.

How in:

 static int initialised = 0; int init (void) { // do something. initialised = 1; return ERR_OK; } int all_other_functions (void) { if (!init) return ERR_NOT_INITED; // do something. return ERR_OK; } 
+2
source share

All Articles