Home/Support/Support Forum/Is it possible to declare variables accessible only to a particular lib?
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

Is it possible to declare variables accessible only to a particular lib?

+1 vote
Hi,

Is it possible to declare variables accessible only to a particular lib?
For exeample if i declare a variable in a library only the functions in that library should
have access to the variable.

Is that possible with DynamicC?

Thanks,
Luis Silva
asked Sep 26 in Rabbit Software by lfrs New to the Community (4 points)

Please log in or register to answer this question.

1 Answer

0 votes
 
Best answer
Dynamic C has a unique compiler that places almost everything into the global scope.

Variables declared between BeginHeader/EndHeader comments are visible outside of the library.

Variables declared after an EndHeader (with the code that's linked into your firmware) are only visible to the code in that section. But note that code following the next EndHeader comment (or before the BeginHeader comment) cannot see it either.

But when I say visible, I just mean that other code won't see the declaration. If another library or your main .C file try to declare variables with the same names, you'll see compiler errors.

In Dynamic C 10, you can use the "Project Explorer" (from the Window menu) to add multiple .C files to your project. But they might share a single scope (I can't recall, and don't actually recommend using that feature).

Bottom line is that you should code your libraries with scoped variable/function/macro names (use a prefix, similar to the way "board_update.lib" uses "bu") to avoid name conflicts. Prefix private variables/functions for a library with "_" and then avoid referencing them in other code outside of the library.
answered Sep 26 by TomCollins Veteran of the Digi Community (1,437 points)
selected Sep 27 by lfrs
Thanks TomCollins
...