Building a library without linking attempt
Problem
Library projects contain their sources in src/ and include/ and don't have a main() function. Building such a project first compiles the code and subsequently tries to link it unnecessarily. The linker, however, fails by complaining about a missing main() or missing setup() and loop() (depending on whether, e.g., the arduino framework is in use).
There is no project parameter in platformio.ini to tell pio not to link a library project. There is also no build parameter to the compiler (eg. gcc) to suppress the linker since the linker is started exclusively by pio. A custom build script could be used, but it should not be required for a common task like building a library project.
Expected behavior
Building a library project should contain the steps of compiling, optionally creating a library archive (.a), and optionally including an archive index, but no linking step.
- A project should be markable as a library project in
platformio.ini(eg.type = librarywithtypedefaulting toapplication) to omit the linking step and avoid a linking error and hence a build error. - Additionally, a compile-only build option should exist that compiles without linking to quickly build a library project or check the code for compile errors on any project, saving the linker time.
+1
furthermore the in deep hard coded gcc structure should be made configurable to make it possible to use an other compile / linker than gcc. It is nearly impossible to use clang / llvm with Platformio
Have you tried https://docs.platformio.org/en/latest/manifests/library-json/fields/build/libarchive.html ?
@ivankravets Thanks for the link, Ivan. Unfortunately, the page doesn't exist (404).
Sorry, the link has been fixed.
I checked the libArchive option both in library.json and as lib_archive in platformio.ini, and neither seems to have the intended behavior.
In library.json, the property declares how the library wants the build process of a main application (firmware) to use the library (building an archive [true] or just treating it as .h and .c files [false]). In platformio.ini, the property controls the same treatment for all libraries of the project. So, neither of the two options is even intended to achieve the goal of omitting the unnecessary linking of a library project.
To be more precise: the linking phase of a library project is a step that creates a DLL, .so, or .a file. It should not intend to make an EXE, executable main application, or firmware, or link the library to any main application. When building a library, there is no main application involved, just the library itself.
Looking at other C/C++ IDEs (CDT, CLion, VS, QtCreator, ...), they all have an option to declare a project as either a main application or a library, because linking a library is done differently. Such an option doesn't exist in PIO, and every project is treated as a main application project, which finally results in an attempt to create an ELF file. On pure library projects (say, I have a menu library which will never be linked as a main application but used in main applications), the last linker step will always fail, making it inappropriate in CI.