Home > Blockchain >  Extern struct builds on aCC (HP-UX) but not on macOS (Xcode)
Extern struct builds on aCC (HP-UX) but not on macOS (Xcode)

Time:02-15

I have the following headers file that I share across two source files:

sth.h:

#define NBALLS 10
#define NBOTS 2

struct {
    float px, py, pz, vx, vy, vz;
}bots[NBOTS];

struct {
    float px, py, pz, vx, vy, vz;
}balls[NBALLS];

That I consume the same way across two source files: a.c:

#include "sth.h"
void makeBalls(void) {
    int i;
    srand(time(NULL));
    for (i=1;i<NBALLS;i  ) {
        balls[i].px = MIN   randf()*(MAX-MIN);
        balls[i].py = MIN   randf()*(MAX-MIN);
        //pz[i] = 0.9;
        balls[i].pz = MIN   randf()*(MAX-MIN);
        balls[i].vx = -1.5 randf()*3.0;
        balls[i].vy = -1.5 randf()*3.0;
        balls[i].vz = -1.0 randf()*2.0;
    }
}

and b.c:

#include "sth.h"
void makeBots(void) {
    int i;
    srand(time(NULL));
    for (i=1;i<NBOTS;i  ) {
        bots[i].px = MIN   randf()*(MAX-MIN);
        bots[i].py = MIN   randf()*(MAX-MIN);
        //pz[i] = 0.9;
        bots[i].pz = MIN   randf()*(MAX-MIN);
        bots[i].vx = -1.5 randf()*3.0;
        bots[i].vy = -1.5 randf()*3.0;
        bots[i].vz = -1.0 randf()*2.0;
    }
}

In HP-UX, it builds and runs fine. In latest macOS, it complains of duplicate symbols.

If I make the following change in sth.h:

static struct {
    float px, py, pz, vx, vy, vz;
}bots[NBOTS];

static struct {
    float px, py, pz, vx, vy, vz;
}balls[NBALLS];

It builds and runs fine. However, naturally, my program doesn't work.

I tried following the recommendation here: Declaring an extern struct template in header file for global use in c files

But then Xcode complains that

bots[i].px = MIN   randf()*(MAX-MIN);

is not a vector or pointer.

I'm very puzzled to why it works under HP-UX. I'm learning C (very beginner) and trying to make sure my code remains portable and also learning how to handle endianness issues.

Thanks, as always.

edit: The change I made according to the recommendation, answering to the comment below: sth.h:

#define NBALLS 10
#define NBOTS 2

struct {
    float px, py, pz, vx, vy, vz;
}botsZ[NBOTS];

struct {
    float px, py, pz, vx, vy, vz;
}ballsZ[NBALLS];

extern struct botsZ bots;
extern struct ballsZ balls;

Xcode returns on a.c and b.c: "Subscripted value is not an array, pointer, or vector" on every line of the function inside the for loop.

And why does aCC build it fine? I think MIPSPro also builds it ok (I will test once I return from trip)

CodePudding user response:

botsZ is an array of 2 instances of an anonymous struct defined like htis:

struct {
    float px, py, pz, vx, vy, vz;
};

and here's the definition of that array, in the header file:

#define NBOTS 2

struct {
    float px, py, pz, vx, vy, vz;
}botsZ[NBOTS];

The following

extern struct botsZ bots;

declares an extern variable named bots with the type struct botsZ, but there is no such type. botsZ is an instance.


Suggestion:

  • Define the structs and typedefs you need in the .h file
  • extern declare the instances
  • define instances in the .c file

Example:

sth.h

#ifndef STH_H
#define STH_H

#define NBALLS 10
#define NBOTS 2

typedef struct {
    float px, py, pz, vx, vy, vz;
} botsZ;

typedef struct {
    float px, py, pz, vx, vy, vz;
} ballsZ;

extern botsZ bots[NBOTS];
extern ballsZ balls[NBALLS];

#endif

sth.c

#include "std.h"

botsZ bots[NBOTS] = {0};
ballsZ balls[NBALLS] = {0};
  • Related