Please take a minute to review and accept our Terms of Use.
Welcome to the PLECS User Forum, where you can ask questions and receive answers from other members of the community.

Many technical questions regarding PLECS are answered on the Technical Solutions page of our website. Tutorial videos, specific application examples, and pre-recorded webinars are available on our YouTube page. Please follow us on LinkedIn for the latest Plexim news.

how to share global variable in different c script?

+1 vote
1,287 views
I have already wrriten some complex c code and run them in hardare experiment. I want to implement a simulation model using Plecs, but I found it difficult to pass customed data(defined by struct) by signal line. Thus I want to write a global c source code and include it in c-script if it is needed. Two questions follows:

1. My C code is separated in .h and .c. (e.g. control.h and control.c). In c-script, shold only control.c be included in code declarations?

2. A global variable is assigned in one place and used in another place, this variable is complex which contains many elements. How to implement?
asked Sep 16, 2020 by yaoyaoyao (15 points)

1 Answer

0 votes
To clarify, are you trying to assign a value to in "C-Script1" (e.g. g_data.a=123) and then read that same variable in "C-Script2" and expect the value set by "C-Script1"?

This would not be possible with multiple C-Scripts within PLECS.  The variables in each C-Script are local to that script. Consider, how would one prevent the two scripts from writing different values to the same variable in the same time step?

Your options are to incorporate everything into one C-Script, one DLL block, or pass the vectorized signals between the inputs and outputs of multiple C-Script or DLL blocks.
answered Sep 19, 2020 by Bryan Lieblick (1,853 points)
edited Sep 22, 2020 by Bryan Lieblick

Hi Bryan,

I have a follow up question regarding global variables in C-Scripts. 

You are writing that all variables in Code declarations are global in the local C-Script, but they are not accessible from other C-Scripts. This seems logical and it corresponds with my simulations (Normal and CodeGen). 

However, as soon as I want to generate code for the RT Box the compiler writes an error that the same variable is defined multiple times (same happens for functions). Renaming all variables and functions for each C-Script resolves this problem. Therefore my question, are they just disturbing each other's or are they now global for all C-Scripts?

Good observation!  When running a simulation in PLECS, every C script always has a private symbol space.

However, when the code is generated and compiled. The code of each C script is written into a .c file, which is then linked to the main .c file. If the same global, non-static variable then appears in multiple C-Script files, there is a link error.

The solution is to declare the variables as static, using the "static" prefix before all variables in the "Code declarations" tab. Then you can include multiple copies of the same C-Script within the model without generating an error when compiling for the RT Box. You'll notice this is the convention used in most C-Scripts included in demo models and the PLECS library.
Thank you for your clarification and giving the solution with the "static" prefix. This works now as expected.
...