0

I am trying to find out what is wrong with my code. I have this code and everything is properly included, but the compiler said Symbol 'b_t' could not be resolved:

#ifndef slow_CAL_H
#define slow_CAL_H

/* slow Struct data */
typedef struct slow_struct_tag {
    const int16_T b_t[7]; 
} slow_struct_type;

extern const slow_struct_type slow_struct;
extern const slow_struct_type *slow_struct;
#endif

slow_private.h

#ifndef slow_private_h_
#define slow_private_h_
#include "slow_cal.h"

#endif 

slow.c

#include "slow.h"
#include "slow_private.h"

int main() {
    foo(&b_t[0]);
    return 0;
}

void foo(int16_T *pY) {
    *pY++;
}

This foo() is not the real function, the real function is the following:

void INTERPOLATE_S16_S16_ZERO(int16_T *pY, int16_T yL, int16_T yR,
int16_T  x, int16_T xL, int16_T xR)
{
    int32_T bigProd;
    int16_T yDiff;
    int16_T xNum;
    int16_T xDen;
    *pY = yL;

    /* If x is not strictly between xR and xL
    * then an interpolation calculation is not necessary x == xL
    * or not valid.  The invalid situation is expected when the input
    * is beyond the left or right end of the table.  The design is
    * that yL holds the correct value for *pY
    * in invalid situations.
    */
    if ((xR > xL) && (x > xL) ) {
        xDen = xR;
        xDen = (int16_T)(xDen - xL);
        xNum = x;
        xNum = (int16_T)(xNum - xL);
        yDiff = yR;
        yDiff = (int16_T)(yDiff - yL);
        bigProd = yDiff * xNum;
        yDiff = div_s16s32(bigProd, (int32_T)xDen);
        *pY = (int16_T)(*pY + yDiff);
     }
}

However, I don't want to post a chaotic code with five or six different files.

embedded_guy
  • 1,939
  • 3
  • 24
  • 39
pop rock
  • 517
  • 1
  • 6
  • 14
  • What is "slow_cal.h" ? – Jabberwocky Oct 21 '15 at 14:46
  • 2
    That code doesn't even compile, does it? You have two variables with the same name. – Carl Norum Oct 21 '15 at 14:46
  • @MichaelWalz it's a header file with the definition of calibration variable structure with storage – pop rock Oct 21 '15 at 14:47
  • @CarlNorum What do you mean ? i cannot find this variable. – pop rock Oct 21 '15 at 14:49
  • 1
    are you allow to do? is same name, different type extern const slow_struct_type slow_struct; extern const slow_struct_type *slow_struct; – Nick Oct 21 '15 at 14:49
  • @CarlNorum. The compiler may not get that far. The OP's error hits first. – Mad Physicist Oct 21 '15 at 14:50
  • @CarlNorum the one is pointer and the other is the struct. Is it false ? – pop rock Oct 21 '15 at 14:54
  • @poprock `extern const slow_struct_type slow_struct; extern const slow_struct_type *slow_struct;` : here you are declaring `slow_struct` once as `slow_struct_type*` and once as `slow_struct_type`. It's almost like declaring `int x; char x;`. – Jabberwocky Oct 21 '15 at 14:54
  • 1
    @MichaelWalz It is _exactly_ like declaring `int x; char x;`. The two types are not compatible at all. – Emil Laine Oct 21 '15 at 14:58
  • @CarlNorum Could you explain the error of the operating system ? The compiler make the process without a warning or an error. – pop rock Oct 21 '15 at 14:58
  • @poprock so your compiler is compiling this piece of code without warning/error ? Which compiler do you use ? – Jabberwocky Oct 21 '15 at 15:00
  • @poprock You have to 1) get a proper standards-compliant compiler (suggested: clang or gcc), 2) enable warnings in it (`-Wall -Wextra` at a minimum, possibly `-Weverything` on clang), 3) enable standard conforming mode (`-pedantic-errors`, i.e. no custom compiler extensions). – Emil Laine Oct 21 '15 at 15:00
  • @poprock, whereas we do strongly prefer you to present *minimal* code that demonstrates your problem, that does still have to be complete in the internal consistency sense. Missing declarations and other errors introduced in the process abbreviating your code interfere with diagnosing problems even more than piles of extraneous code do. – John Bollinger Oct 21 '15 at 15:33
  • @John Bollinger I wrote a minimal code and i didn't miss a declaration or an error, the problem was only in foo() because i have forgotten that the variable is constant. The solution is on my comment at the Zenith's answer and some other developers have found some other issues. So i think you are undue and you want only to vote down. Look please again the html tip `up vote` – pop rock Oct 21 '15 at 15:43

2 Answers2

3

b_t is a member of slow_struct_type, and thus you need an object of type slow_struct_type first before any b_t can exist.

Even then you would refer to b_t via the object that holds it, i.e.

  • x.b_t if x is of type slow_struct_type, or
  • x->b_t if x is of type slow_struct_type*.

After that you will have to fix the multiple declarations of slow_struct with different types:

extern const slow_struct_type slow_struct;
extern const slow_struct_type *slow_struct;

You can't have both: either slow_struct is a const slow_struct_type *, or it is a const slow_struct_type, but not both.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • you mean like this `foo(&(slow_struct->b_t[0]));` – pop rock Oct 21 '15 at 14:52
  • 1
    @poprock Yes. After you decide whether `slow_struct` is a `slow_struct_type` or a `slow_struct_type*`. It can't be both. – Emil Laine Oct 21 '15 at 14:53
  • The other guys above say that i am wrong if i use the same name for both types. Are you Agree ? – pop rock Oct 21 '15 at 14:56
  • @poprock Yes, in C, identifiers must be unique, and re-declarations of different types are not allowed. – Emil Laine Oct 21 '15 at 14:57
  • Do you have a reference from something like ISO, so i could assert this issue ? – pop rock Oct 21 '15 at 15:20
  • @poprock: "All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined" (C2011, 6.2.7/2). "Compatible type" is defined in C2011, 6.2.7/1 and other sections referenced from there; for our purposes, suffice it to say that pointer types can be compatible only with other pointer types (but not *all* other pointer types). Equivalent provisions exist in C99 and C90. – John Bollinger Oct 21 '15 at 15:43
  • @JohnBollinger Thank you and sorry if i was "jerky" but sometimes the users are undue with (my) questions. – pop rock Oct 21 '15 at 15:47
1

Your code is seriously broken, and will not compile. Perhaps what you really want to ask about boils down to this:

#include <stdint.h>

/* slow Struct data */
typedef struct slow_struct_tag {
    const int16_t b_t[7]; 
} slow_struct_type;

extern const slow_struct_type slow_struct;

void foo(int16_t *pY);

int main() {
    foo(&slow_struct.b_t[0]);
    return 0;
}

void foo(int16_t *pY) {
    *pY++;
}

Relative to your code, that eliminates a missing header (slow.h), corrects data types (int16_u, int16_T -> int16_t), includes stdint.h for the declaration of int16_t, removes a duplicate declaration of global variable slow_struct, makes foo()'s prototype visible in main(), and in the call to foo() uses slow_struct.b_t in place of bare b_t (which does not exist in any code you presented).

Even in that case, problems remain. Although you can use a pointer to a struct member as a function argument, the particular usage above is not const-correct. As a result, if the compiler accepts it (which will depend to some extent on compilation options) then it may break at run time, and if not then it will produce the surprising effect of a const variable's value changing.

Note also that in this particular case, &slow_struct.b_t[0] is rather verbose, as in that context it means exactly the same thing as slow_struct.b_t. Additionally, that code uses global variable slow_struct before initializing it (indirectly through *pY++ in foo()), and therefore exhibits undefined behavior.

Update:

Regarding the specific error message revised into the question, "Symbol 'b_t' could not be resolved", the problem is what I already said: there is no variable b_t declared anywhere in the code you presented. The type slow_struct_type has a member of that name, but you can only refer to such a member in the context of an overall slow_struct_type object.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • This is not the whole of code, because the whole of code is from MATLAB generated and is in 7 different files, so i tried to make an example. except the `main()` and the `foo()` the rest code is the same. – pop rock Oct 21 '15 at 15:17
  • @poprock, does my deduced code then *not* capture the essence of your question? Even after your edit, it's not evident to me that that would be the case. – John Bollinger Oct 21 '15 at 15:36