0

Im working with the TI Z-Stack znp-project and implement a own functions in a separate file (bc.c + bc.h). Although I'm fresh to C I managed all fine except one thing:

in the file znp_app.c I need to reference a queue for uart-tx-ing:

static osal_msg_q_t npTxQueue; 

to achieve what I now wrote in the znp_app.c in my own bc.c (where I include znp_app.h).

void bc_sendResponse(...)  // in bc.c
{
    ...
    npSendForBc(bcMSGp);   // want to call "osal_msg_enqueue(&npTxQueue, pBuf);" here
    ...
}

void npSendForBc(uint8 *pBuf)  // workaround function in znp_app.c
{
    osal_msg_enqueue(&npTxQueue, pBuf); // need this in bc.c
}

I tried to define the queue in the znp_app.h, but the compiler don't appreciates it. I also tried do write some kind of "getter", but it also was not supported. Please show me the correct syntax, ty.

EDIT 1

I already tried to

extern osal_msg_q_t npTxQueue; // in znp_app.h

but I get this error while linking:

error: Error[e46]: Undefined external "npTxQueue" referred in bc ( C:\...\Z-Stack 3.0.1\Projects\zstack\ZNP\CC253x\CC2531-ZNP-with-SBL\Obj\bc.r51 ) 

As getter I tried (with an extern in znp_app.h)

osal_msg_q_t getQueues()
{
    return npTxQueue;
}

I cant exactly remember the error code.

alk
  • 69,737
  • 10
  • 105
  • 255
Abgehter
  • 151
  • 1
  • 9
  • 1
    That the variable is marked as `static` is a way to make it "private", which means you should never need to use the variable in another source file. If you do, then you probably have a flaw in your design. – Some programmer dude Jun 17 '18 at 12:10
  • "*I also tried do write some kind of "getter", but it also was not supported*" could you elaborate a bit more on this? What does "not supported" mean? – alk Jun 17 '18 at 12:28
  • @alk I edited my question, but i cant exactly remember the error and think its a no-c-way^^ – Abgehter Jun 17 '18 at 13:46
  • 1
    You still have the code, right? Build again and it'll give you the same error. – Useless Jun 17 '18 at 16:52
  • "*i cant exactly remember the error*" Then what about [this](https://en.wikipedia.org/wiki/Lab_notebook) though ...? :-) – alk Jun 17 '18 at 17:28

2 Answers2

1

To make npTxQueue an ugly global:

  • Put into znp_app.h

    extern osal_msg_q_t npTxQueue; 
    
  • and into znp_app.c put (without any static)

    osal_msg_q_t npTxQueue; 
    
  • and include into znp_app.c znp_app.h as well (before the definition of npTxQueue).

  • Finally include znp_app.h into where you need npTxQueue.


Alternativly use a getter by

  • moving the definition of osal_msg_q_t to znp_app.h (if not in there already).

  • and add (below the definition of osal_msg_q_t) the prototype of the getter to znp_app.h as well:

    osal_msg_q_t GetIt(void);
    
  • and add the getter's implementation to znp_app.c:

    osal_msg_q_t GetIt(void)
    {
      return npTxQueue; 
    }
    
  • Finally include znp_app.h into where you need npTxQueue.

alk
  • 69,737
  • 10
  • 105
  • 255
0

Static in a global scope means it is only visible in that file. You're confusing it with static in a function I suspect. See this answer for details:

What does "static" mean in C?

A global variable is implicitly the same as a static variable in a function, you don't need the static at the global scope in your code. You will then also want to define the variable using the extern keyword in your header file, as per:

How do I use extern to share variables between source files?

Michael
  • 1,355
  • 2
  • 12
  • 15
  • I tried (and now again) (put in .h with extern and without static) but i get this error: Error[e46]: Undefined external "npTxQueue" referred in bc ( C:\...\Z-Stack 3.0.1\Projects\zstack\ZNP\CC253x\CC2531-ZNP-with-SBL\Obj\bc.r51 ) on "make" while compile says no errors – Abgehter Jun 17 '18 at 13:33