How to tell PIO to only build files that are used in src

For various reasons, I need to copy/paste a library, RadioHead, into the libs folder of my project.

However, I do not need the majority of the files in this library. Further, whenever I build my project, I get a bunch of errors of the form “x was not declared in this scope” because the unused files have dependencies I have not included.

Yes, I could just delete all unused files. This does work, but I want to make it as easy as possible to update RadioHead later on; there are many many files that need to be deleted and in odd places too.

I am wondering if there is a way I could tell PlatformioIO to only build files that are used by files in the source folder?

I have the same problem.
Maybe my post is related: https://community.platformio.org/t/platformio-vscode-insists-on-including-undefined-libraries/12888

You should create a library.json (docs) in that library’s directory and use the appropriate exclude directives to filter out the unwanted source files (docs).

The original Radiohead library already uses such a file:

Does the same file not work anymore for newer versions?

1 Like

Im having trouble getting this solution to work. My library.json is in the folder lib/RadioHead, and it contains the following

{
"name": "RadioHead",
"keywords": "rf, radio, wireless",
"description": "The RadioHead Packet Radio library which provides a complete object-oriented library for sending and receiving packetized messages via RF22/24/26/27/69, Si4460/4461/4463/4464, nRF24/nRF905, SX1276/77/78, RFM95/96/97/98 and etc. This only includes part of the library; see include property below",
"authors": {
    "name": "Mike McCauley",
    "email": "mikem@airspayce.com",
    "url": "http://www.airspayce.com/mikem/arduino/RadioHead/index.html"
},
"version": "1.101",
"downloadUrl": "http://www.airspayce.com/mikem/arduino/RadioHead/RadioHead-1.101.zip",
"export": {
    "exclude": [
        "RH_ASK.cpp",
        "RHSoftwareSPI.cpp"
    ]
}

}

I have also tried editing the exclude property so that each dependecy starts with “lib/RadioHead” or “RadioHead”, but none of these work.

I have also tried adding the lib_ldf_mode = off to my platformio.ini file, but then it cannot find any file in RadioHead.

Am I doing anything wrong?

Please upload the current state so that it’s reproducable.

What’s the exact error message? From which files are they coming?

I have uploaded the current state to here

If you run pio run, it should give the following error message

lib/RadioHead/RHSoftwareSPI.cpp:55:17: error: ‘digitalRead’ was not declared in this scope
      readData = digitalRead(_miso);
                 ^~~~~~~~~~~
lib/RadioHead/RHSoftwareSPI.cpp:55:17: note: suggested alternative: ‘digitalWrite’
      readData = digitalRead(_miso);
                 ^~~~~~~~~~~
                 digitalWrite
lib/RadioHead/RHSoftwareSPI.cpp:67:17: error: ‘digitalRead’ was not declared in this scope  
      readData = digitalRead(_miso);
                 ^~~~~~~~~~~
lib/RadioHead/RHSoftwareSPI.cpp:67:17: note: suggested alternative: ‘digitalWrite’
      readData = digitalRead(_miso);
                 ^~~~~~~~~~~
                 digitalWrite
Compiling .pio/build/raspberrypi_3b/lib9f2/RadioHead/RH_CC110.o
*** [.pio/build/raspberrypi_3b/lib9f2/RadioHead/RHSoftwareSPI.o] Error 1
lib/RadioHead/RH_ASK.cpp: In member function ‘bool RH_ASK::readRx()’:
lib/RadioHead/RH_ASK.cpp:585:13: error: ‘digitalRead’ was not declared in this scope
     value = digitalRead(_rxPin);
             ^~~~~~~~~~~
lib/RadioHead/RH_ASK.cpp:585:13: note: suggested alternative: ‘digitalWrite’
     value = digitalRead(_rxPin);
             ^~~~~~~~~~~
             digitalWrite
*** [.pio/build/raspberrypi_3b/lib9f2/RadioHead/RH_ASK.o] Error 1