How to create library project?

I want to create new Library for my own projects. Is it possible do this with PlatformIO IDE?

I can create new project that will be focused on concrete boards. But I can’t see the way to create abstract library project with just some .cpp/.h files.

Do you mean common library for a few projects?

  1. Create this common library
  2. Specify lib_extra_dirs in your platformio.ini and add path where this common library is located

See example:

The example library referenced here is not an external library but a de facto Arduino application with a setup and a loop. It remains unclear how one would go about creating an external library since the compiler will throw undefined references to setup and to loop if those are removed.

The OP seems to have been wanting to make a set of .h/.cpp files that could be shared amongst a bunch of projects, which can be done by putting the files in a folder and then referencing that folder via lib_extra_dirs as pointed out. As opposed to putting it in the lib folder, which would then be project specific.

A properly created library, would you would then #include into a main.cpp (for example) won’t cause compile errors.

I do agree that the arduino-external-libs example on github doesn’t seem to be relevant though.

By the way… this is a two year old thread! :open_mouth:

I had realized it was an old thread. Maybe I should have opened a new one? This one seemed most relevant to what I am looking for which is how to create a library project as opposed to an application. This is is how I take the OP’s meaning in the statement “just some .cpp/.h files” – that is to say without a main or a setup and loop.

It seems that one can create an external library and develop it within the context of another application, but as far as I can tell there is not a way to create and develop a library on its own with unit testing. Thus it seems to me that the question remains unanswered.

2 Likes

Have you tried platformio ci -l . command?

1 Like

$ pio ci -l .
Usage: pio ci [OPTIONS] [SRC]…
Error: Invalid value: Missing argument ‘src’

So, not sure yet what I am doing wrong, but looking at the ci document page, this does seem to be on the right track. Particularly:

" platformio ci command is useful for library developers. It allows one to build different examples without creating own project per them."

I guess some additional documentation on how to do that exactly would be super helpful. In the mean time, I’ll dig into the ci command a bit more.

thank you

1 Like

Ah, I got you… you want to test a library?

There may be some more info here also : Redirecting...

I’m assuming here that when using the pio ci option that the examples for the library will be automatically built… or that’s at least vaguely what the docs suggest.

@scott2b I’m exactly in your same situation (library project on its own with unit testing and examples): in the meantime, have you been able to solve the problem and settle with a functioning configuration?

I examined platformio ci, but its use for library projects seems more like an afterthought/workaround than a standard arrangement… I mean: my project features a dead-simple standard Arduino layout (library source code in src folder, example sketches in examples/*/*.ino), I would expect a no-brainer, straightforward declarative setup (within platformio.ini) and terse command to build it, without digging into unnecessary details (I would delve into advanced use & related intricacies at a later time, when non-trivial arrangements/operations were actually needed).

For what it’s worth, on Windows, I use this to have all the examples built on demand - I have it sat in the e:\repos\pfeerick folder and call it from the pio terminal. On the github repo, each commit is run through the arduino-builder, which builds each example.

@echo off

set LIB_DIR=E:\repos\pfeerick\elapsedMillis
set EXAMPLES_DIR=E:\repos\pfeerick\elapsedMillis\examples
set BOARD_TYPE=uno

for /D %%D IN ("%EXAMPLES_DIR%\*") DO (
    pio ci --lib %LIB_DIR% --board %BOARD_TYPE% %EXAMPLES_DIR%\%%~nxD
)

Hi, I am having the same question:

I have a piece of code “B”, which uses a library called “C”. I set up “B” as a normal platformio project and “C” was set up as a typical library via platformio library manager.

Now I want to use “B” in different projects such as “A1”, “A2”, “A3”, so that in the future if I need to change “B”, it will be reflected in all projects that uses it.

Is it possible to do what I want? Thanks in advance!

You have 2 options:

  1. Publish library to the registry
  2. For local use, check symlink

Thank you for your reply! For the symlink option, will the “package” folder be the same as my “B” “project” folder created with PIO project wizard?

Then I will just add a manifest file and it should work?

I am having a similar issue – I see lots of existing libraries in the registry that a) don’t have a platformio file, and b, reference arduino.h. When I try this, platformio doesn’t see it. I created a more detailed question here, but no one has replied to it yet. Any thoughts from this thread?