Library usage question

Version: 2.0.4

Date: 2023-02-27T16:14:28.576Z

CLI Version: 0.31.0

Nano 33 BLE

I thought I knew libraries but it is obvious I don’t

I have a .INO sketch that has the following line:

#include "PS2Keyboard_nano_33_ble.h"

The sketch compiles and sota works.

However, although not explicitly declared, "PS2Keyboard_nano_33_ble.cpp"

seems to get loaded.

I “know” this because I change a declare in the .cpp file from “return =-1” to

“return = -17”. This change causes my monitor to print out the new value.

However, when I add the line #include "PS2Keyboard_nano_33_ble.cpp" I get many

compile errors. i.e. duplicate definitions, etc. Probably legit.

Why doesn’t this happen when the .cpp file is not declared?

Or, why does the .cpp get loaded when not “#included”?

Thanks,

Larry

the source code for libraries is located in specific directories known to the Arduino IDE.

the #include in an .ino file identifies the library by name. when the .ino is built, it along with other files including the library files are copied into a build directly from when the binary is built and downloaded to the board

sounds like you copied the library file to the directory containing the .ino file, hence there are multiple copies and duplicate definitions

When you do this

#include "PS2Keyboard_nano_33_ble.h"

the compiler will firs look in the sketch directory for the file and if not found then in the sub folders of the libraries folder of your Sketchbook. Loading a .h file implies that there is a corresponding .cpp in the same folder and if so then that is loaded too

So, I don't understand why you say

(Profile - UKHeliBob - Arduino Forum);
I did not know that loading the .h file implied that there was a corresponding .cpp file
that automatically gets loaded.

Is including the code #include "PS2Keyboard_nano_33_ble.cpp" wrong?
I am trying to get a morse code keyboard that worked on the simple NANO
to work on the NANO 33 BLE. The problem is leading me to the .cpp file.

I wanted to bring it(.cpp) into the IDE for troubleshooting.
The only way I know to do that is by declaring it an #Include.

Thanks,
Larry

gcjr,
That is a fear of mine.
I tried to be very careful.
but....

Thanks,
Larry

yes.
the include copies that file into the file the #include is in. if there's also a copy of the file in the directory with the .ino, then there's effectively two copies of the code

To make it show up as a tab in the IDE, copy the file to your sketch directory. It will always be compiled as part of your sketch.

NOTE: I think the problem is having:
#include "PS2Keyboard_nano_33_ble.h"
in your sketch without having "PS2Keyboard_nano_33_ble.h" in your sketch directory. That will cause the compiler to look through all of the library directories for the library that contains "PS2Keyboard_nano_33_ble.h". If it finds that file in a library it will include all of the .cpp files for that library in your build. IF you have a copy of "PS2Keyboard_nano_33_ble.cpp" in your sketch directory AND in the library that contains "PS2Keyboard_nano_33_ble.h" you will get BOTH included in your build and a lot of duplicate definitions.

If you put "PS2Keyboard_nano_33_ble.cpp" in your sketch directory so it shows up as a tab in your sketch, also do that for "PS2Keyboard_nano_33_ble.h".

John,
My sketch directory has the directory "PS2Keyer_NANO_33_BLE".
Under that directory is "PS2Keyer_NANO_33_BLE.ino" and another directory
named libraries.
Under the libraries directory are four folders, Adafruit_BusIO, Adafruit_GFX_Library, Adafruit_SSD1306, and the important one:PS2Keyboard_nano_33_ble.
It contains the directory "examples" and the .h and .cpp files.

I'm messing with the library because the .h file contained the line "include avr/io.h.
This isn't in the core for the NANO 33 BLE and prevented compiling.
I remarked it out and am trying to see what trouble I am in.

I got the .cpp into the IDE by unremarking its #include, right clicking it and selecting "go to Definition. Then I remarked the .cpp out again.

Thanks 1.0x10^7
Larry

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