Compiling using CLI with multiple directories

I have been using the particle CLI to compile and flash programs to my photon. This is all working fine.
I want to create a slightly more complex program now that uses libraries and so I want to organise them into separate directories. Is it possible to add the files in these sub folders while maintaining the folder structure?

I can add them to the compilation step using a particle.include file but this results in the files all being in the root directory and not in their original sub folders.

Ideally I want a setup like:

project
|
main.cpp
|-- lib
… |-- lib1.h
… |-- lib1.cpp
…subLib
…|-- subLib.h
…|-- subLib.cpp

Where I can include into main.cpp ‘#include “lib/lib1.h”’ and lib1.h can refer to subLib.h with ‘#include subLib/subLib.h’

Is this possible with the CLI?

Yes you can keep the file structure as you got it there (since CLI v1.13.0 even without particle.include) but you have to have the include statements as if all files were in the same directory - for the time being.

But I’d suggest you have two sets of include statements (one flat, one structured) and just wrap them in conditional compile blocks like

#define FLAT_PROJECT
#if defined(FLAT_PROJECT)
#  include "lib1.h"
#  include "lib2.h"
#else
#  include "lib1/lib1.h"
#  include "lib2/lib2.h"
#endif
1 Like

Thanks for that. I was using 1.10.0 so when I updated to the latest (1.15.0 I think) it then started picking up all the files in the sub folders.

However now when I compile, it fails but doesn’t give me a reason. Is there a verbose flag or any other option I can give to get more information?

Here is an example output:

$ particle compile p .

Compiling code for p

Including:
    ...
    list of all files in the current folder and sub folders
    ...
attempting to compile firmware 
Compile failed. Exiting.

I can replicate this behaviour with a folder structure of:

src/
|-- main.cpp
|-- lib
…|–lib1.h

Where main.cpp contains only:

#include "application.h"
int main() {}

And lib1.h contains:

#ifndef _SPI_H_INCLUDED
#define _SPI_H_INCLUDED

Using the command:

$ particle compile p src/

If I delete lib1.h it compiles fine

Are you by any chance on OSx?
If so, there is an open issue

Nope, I’m on Ubuntu 14.04.

Interestingly I’ve tried compiling using the cli source from github with some extra logging added and I am getting an error back from the compile api (ApiClient.compileCode)

   ....
   body: 
   { ok: false,
     code: 200,
     output: 'Compiler timed out or encountered an error',
     errors: [] } }

Ok progress! I was incorrect in my previous statement of folder structure and lib1.h is actually nested 2 levels down like so:

src/
|-- main.cpp
|-- lib
…|-- subFolder
…|–lib1.h

If I move lib1.h up one level under lib/ then it compiles fine.

So maybe there is a bug somewhere where you can only compile files nested 1 level down. That issue you linked to does seem suspiciously relevant. Is the server code available anywhere to look at or is that closed source?

I’m not aware of the build farm open source repo, but I doubt it’s closed source (unless Broadcom license demands for that)
@Dave might know tho’

Thanks for this report. That looks like a bug in the compiler service not creating directories recursively. It’s different from the GitHub issue linked above.

We are working on supporting libraries and structured projects better through the cloud compiler. In the mean time please use a project structure with one level of subdirectories if possible.

Sure thing!

Hi!

Just wondering what is the status of this.

I have a project with several classes in multiple sub-directories. Can I use the “particle compile” command to build locally on my machine (OSX).

Thanks!

There are 2 ways to go about this:

If you want to follow the project structure documented here you can do particle project create to have the CLI create the project structure for you. It will recursively compile files in src.

You can also just run particle compile photon in a folder that has your code and it will pick up code in sub directories.