Headers and arrays

Hopefully you get what I'm trying to do from the below, if it is possible then could you explain to me how as I am currently drawing blanks on ideas to try.
_build.h
1
2
3
4
5
6
7
8
9
10
11
12
13
typedef void (*zxInitFree)( void );
enum zx__InitFree_enum
{
    zx__InitFree_core      = 0,
    zx__InitFree_FSO       = 1,
    zx__InitFree_commonDir = 2,
    zx__InitFree_count
};

static zxInitFree zx__Init[ zx__InitFree_count ] = {NULL};
static zxInitFree zx__Free[ zx__InitFree_count ] = {NULL};
void zxInitFramework( void );
void zxFreeFramework( void );

fso.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void zxInitCommonDir( void )
{
#ifdef ZXWIN
    zx_sys_root = getenv("SYSTEMDRIVE");
    zx_usr_home = getenv("HOMEPATH");
#else
    struct passwd *usr = NULL;
    zx_sys_root = "/";
    zx_sys_core = "/";
    zx_sys_usrs = "/home";
    usr = getpwuid(getuid());
    zx_usr_docs = usr->pw_dir;
#error TODO: Finish Linux version of zxInitCommonDirs
#endif
}
zx__Init[ zx__InitFree_commonDir ] = zxInitCommonDir;

Compiler output
1
2
3
4
||=== Build: libzxs-x64-d-vc in libzxs (compiler: MSVC2013-x64) ===|
src\zxs\fso.c|311|error C2369: 'zx__Init' : redefinition; different subscripts|
src\zxs\fso.c|311|error C2075: 'zx__Init' : array initialization needs curly braces|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

If that's not enough I'll try my best to explain here:
I'm trying to setup an array for my custom frameworks initialization functions to insert themselves into ready for calling by a single function, I'm trying this way so that I can avoid using #ifdef to include and then call these functions.

Edit: grammer mistake
Last edited on
Yes. The header is only declaring it though. Do not assign the array in the header.
The array also needs to be defined in the implementation file. Whenever other modules wish to use zx__Init, they need to link to the module that defines it, not just include the header.

While I don't think it's necessary (through some grace of the C standard)(I might also be wrong), you should declare the array in the header with extern.
still produces the same error, I thought of something else instead though, think this will work?
header
1
2
3
#define ZX__INIT_CORE
#define ZX__INIT_FSO
//etc 

inheriting header
1
2
#undef ZX__INIT_CORE
#define ZX__INIT_CORE zx__initCore(); 

_build.c
1
2
3
4
5
6
7
void zxInit( void )
{
    ZX__INIT_CORE
    ZX__INIT_FSO
    ZX__INIT_COMMONDIR
    // etc
}


Edit:That failed but I went with an alternative
1
2
3
4
5
6
7
8
#define ZX__INIT_CORE
#define ZX__INIT_FSO
#define ZX__INIT_COMMONDIR

#define ZXINIT \
    ZX__INIT_CORE \
    ZX__INIT_FSO \
    ZX__INIT_COMMONDIR 
Last edited on
Topic archived. No new replies allowed.