Restructuring code in Particle Workbench

I would like to restructure my code to have certain .h and .cpp files place in appropriate subdirectories. Is there a way to do this without changing all my includes to point to the subdirectory first.

I was hoping there might be something like

INCLUDE_DIRS=src,src/subdir

that I could put in the project.properties file but that doesn't seem to work.

Create a build.mk file in the src directory. Note that it must be there, not at the top level!

[See updated version below, as this is missing a few lines]

# Place this custom makefile here: <project-path>/src/build.mk

INCLUDE_DIRS += $(SOURCE_PATH)/src/testsubdir  # add user sources to include path

This will add src/testsubdir to the include path; modify as needed.

2 Likes

Worked like a charm, thanks @rickkas7

Hey @rickkas7 Well I thought it worked because I was getting no compiler errors but it appears to cause something else to be compiled rather than my source files. For example I created a new project with the start blink test code. Compiled to 5.9.0 and flashed it with success.
Added in the build.mk file and made sure it was in the src directory.
Did a clean and compile.
Compiles successfully, flashes but does not execute the code that I have in setup and loop. It also seems to go to a flashing green, then solid green, then the two orange, etc. The device does not have access to the internet but I am in Manual mode so not sure what code is being compile and loaded when I add this build.mk file.
Contents of the file are just the one liner you provided.
Behaves the same whether the testsubdir exists or not.
Here is my main.cpp


#include "Particle.h"

SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);

void setup() {
  pinMode(D7, OUTPUT);
}

void loop() {
  digitalWrite(D7, HIGH);
  delay(1000);
  digitalWrite(D7, LOW);
  delay(1000);
}

Here is my file system in the project


Contents of build.mk

INCLUDE_DIRS += $(SOURCE_PATH)/src/testsubdir

Hmm... maybe you need to add the boilerplate still.

## Standard behavior must be included here
INCLUDE_DIRS += $(SOURCE_PATH)/$(USRSRC)  # add user sources to include path
CPPSRC += $(call target_files,$(USRSRC_SLASH),*.cpp)
CSRC += $(call target_files,$(USRSRC_SLASH),*.c)

APPSOURCES=$(call target_files,$(USRSRC_SLASH),*.cpp)
APPSOURCES+=$(call target_files,$(USRSRC_SLASH),*.c)

## Custom stuff can be added here
INCLUDE_DIRS += $(SOURCE_PATH)/src/testsubdir

That appeared to do it. Hooray. The light blinks. Now back to my original project.

I was thinking for a while, dang I am good at this restructuring. No compile errors after every change. haha

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.