Skip to main content
BRapo.1
Associate
August 24, 2023
Question

CUBE IDE C code sharing

  • August 24, 2023
  • 2 replies
  • 1591 views

Good morning,

I apologize if this question is a bit dumb, I'm still learning embedded programming.

I'd like to know if its possible to share code between projects in STM CUBE IDE.
The idea is to have like a "folder" with one (or many) .h and .c files. Then in the properties of each project, when I need those files I just add them to the 'Paths and Symbols'.
And then wen I want to change the behavior I only change it once for all the projects.

That I already can do. The problem is that I don't know how to get that "folder" to compile. I know that compiling is a device-targeted procedure but the code in those files that I want to include won't have any reference to any specific platform.
I want to do generic code using standard libraries so it can be included in all the project's I create regardless of the micro controller.

That's why I need it to be able to "pre compile"?? without specifying a target device? Just to know whether the code is written ok.

Thank you for your help!

    This topic has been closed for replies.

    2 replies

    Pavel A.
    Super User
    August 24, 2023

    Yes, of course, in the same way as in other software. You can share source folders among several projects. You can make a static library (for compatible MCUs). You can exclude some files from build, per project configuration. By the way, CubeIDE comes with a user guide. It is written specially to answer many questions :)

     

    Harvey White
    Senior III
    August 26, 2023

    I use a different approach.

    I have one file that's unique per project (for configuration).  In that file I have a lot of #define statements, which enable and disable subsystems, and customize others (like which SPI interface an SPI display uses, if enabled).

    In the entire project, I use a lot of #ifdef statements.

    I have one repository of all the source files.  I take the root of that subdirectory (I have graphics/hardware/system, etc files), and drag and drop it into the "core" directory of the project.  In the "INC" part of the core files, I include the "options.h" file.

    Similar projects can use the options.h file without much editing.  New projects with new features need new options in the "options.h" file.  This works well over a number of projects, but you do have to be careful about what the #defines control.

    In the import, make sure that you link to existing files.  This allows a copy, so editing the source files changes the files for all projects.  It keeps things consistent, but you have to be aware you're changing things for all the projects.