#include "user_defined_header.h" not accessible at compilation

Dear all,

I work with coder and will keep the structure of my team.

I’ve added a header file containing some struct only to facilitate decoding of structured register (word and bit level), used by both “state machine block” and “C-Scripts block”.

The structure of my project is:

ProjectRoot/
│
├── My_PLECS_model.plecs
│ My_user_defines.h
└── codegen/

In my model, e.g for state machine, the #include “My_user_defines.h” in C declaration works fine, I can build my state machine without errors, but at compilation it does not works, as the path from codegen is different!


As workaround, I modified my structure as following:

ProjectRootAlternative/
│
├── model/My_PLECS_model.plecs
│ include/My_user_defines.h
└── codegen/

and modified my include in C declaration of state machine as follow:
#include “../include/My_user_defines.h”. In this case, this works correctly when editing the plecs template as well as during compilation, due to same relative path from both location model and codegen.


Is there a way to works without project structure modification (the first project structure)? Is there a solution to add search user files path relative?

Thanks for any suggestion.

Best regards.

Blaise

Blaise, what target are you using? Can you also share the version of PLECS and the TSP version you are using as well?

When I try to compile with your original arrangement everything works fine. See the attached example which I tested with the most recent public versions of the TI C2000 and STM32 targets. I set the output directory to a non-default folder titled “codegen”. The header and C files are automatically copied into that folder.

The example is attached below, from C-Scripts — PLECS 5.0 Documentation

cscript_example_external_files.zip (8.1 KB)

Dear Bryan,

I currently use PLECS 5.0.6. I use PLECS C2000 TSP V2.3.3 (PLECS C2000 TSP/cgt/ti-cgt-c2000_22.6.1.LTS).

In my case, I want to declare one local variable in C-Script. In this case it does’nt work (I can simulate my modell without error, but I can not generate code).

I want submit my exemple in my answer, but I could not send a zip file. How do I have to proceed?

Best regards.

Blaise

For security reasons *.zip files are disallowed for general users. You can post a *.plecs model and your *.c and *.h files, along with a description of your folder structure in the body of the post.

To completeI my post from yesterday, I send now my two files that are in ProjectRoot (only .plecs and .h file, as I don’t need a .c file). The output directory is named codegen

The awaited structure of my project is:

ProjectRoot/
│
├── RegisterBitOrganisationFail.plecs
├── RegisterMapFail.h
└── codegen/


Here the screenshoot of general tab too:


I can start a simulation without error, but I can not build the project. Here an extract of Diagnostics tab:

Error in build step 4 (Make): Make process exited with code 2:
“C:/Program Files/Plexim/PLECS 5.0 (64 bit)/make/make” -f RegisterBitOrganisationFail.mk ./output_TI2837x_cpu1/RegisterBitOrganisationFail.elf

…

"RegisterBitOrganisationFail_0.c", line 11: fatal error: cannot open source file “RegisterMapFail.h”
1 catastrophic error detected in the compilation of “RegisterBitOrganisationFail_0.c”.
Compilation terminated.
make[1]: *** [output_TI2837x_cpu1/RegisterBitOrganisationFail_0.obj] Error 1
make: *** [all] Error 2


Hope you can reproduce error with same structure.

Best regards.

Blaise


RegisterBitOrganisationFail.plecs (12.5 KB)

RegisterMapFail.h (419 Bytes)

Blaise, thanks for posting the files. I was able to reproduce the error with your model.

First I need to correct my earlier reply. The included files are not copied to the codegen folder automatically. My example only appeared to work because PLECS optimized the C-Script away, so its includes were never compiled. Sorry for the confusion.

During simulation the model folder is on the C-Script include search path. During the build, each C-Script is written to its own file in the codegen folder and compiled from there, so "RegisterMapFail.h" is not found. That’s also why your alternative structure works: ../include/ points to the same folder from both the model and codegen.

To keep your original arrangement, you can choose the include path based on the compiler in the C-Script Declarations:

#include <stdint.h>
#if defined(__TI_COMPILER_VERSION__)
#include "../RegisterMapFail.h"   /* target build: compiled from codegen/ */
#else
#include "RegisterMapFail.h"      /* simulation */
#endif

static union SYS_CTRL_T sys_ctrl;

This assumes the output directory stays one level below the model, per your desired workflow. The same approach should work in the State Machine declarations.

I’d also remove the sys_ctrl definition from the header and keep only the types, with an include guard:

#ifndef REGISTER_MAP_FAIL_H
#define REGISTER_MAP_FAIL_H
#include <stdint.h>

/* typedefs, struct SYS_CTRL_BITS_T and union SYS_CTRL_T as before */
                                                                                                                                                              #endif

Since each C-Script is compiled into its own file, a variable defined in the header gets defined once per C-Script, and the linker reports symbol "sys_ctrl" redefined when you have more than one. Declaring it static in each C-Script, as above, gives each block its own copy.

I tested this with two C-Scripts using PLECS 5.0.6, TI C2000 TSP 2.3.3TS, and it builds without errors.