Suppose that I want to create a library that needs a particular STL function (C++ include file.)
Some boards have relatively complete C++ support, and the .../include/c++ directory, so ... no problem!
But AVR doesn't have these included by default.
Some of them are compatible with the AVR C++ compiler anyway (purer templates with no libc++ or dynamic allocation requirements), so in theory I could just copy the necessary include file into my library...
If I do that, I now have a conflict on the other platforms - which file will they pick up? Standard C++ (if it exists), or the library's copy (which might not match the compiler version)? Is there a way to force the order of the include search path?
(The feature in question is "initializer_list", if that makes any difference.)
With some help from Microsoft Copilot - there may be a workaround for AVR boards, which is converting initializer list into array first and then ustig the array in the class template constructor. Like:
template<typename T, size_t N>
class myVector {
public:
T data [N];
myVector (const T (&arr) [N]) {
for (size_t i = 0; i < N; ++i) {
data [i] = arr [i];
}
}
void print () const {
for (size_t i = 0; i < N; ++ i) {
Serial.println (data [i]);
}
}
};
// Helper function to create aray from an initializer list
template<typename T, size_t N>
myVector<T, N> A (const T (&arr) [N]) {
return myVector<T, N> (arr);
}
void setup() {
myVector<int, 3> v ( A ( {1, 2, 3} ) );
v.print ();
myVector<int, 3> u = A ( {4, 5, 6} );
u.print ();
}
void loop() {
}
Are you asking which one they will use when your library is being compiled? Or are you asking which one they will use when compiling sketches that don't use your library?
The way the Arduino discovery system works is to tentatively run the preprocessor on the sketch. If preprocessing fails due to a "No such file or directory" error for an #include directive, then the libraries folders are searched for a library that provides that header file.
So in a case where the path of the STL header from the platform's toolchain is already present in the platform's predefined compiler "search paths", library discovery will not be performed for that header file and thus that #include directive in the sketch program would not cause the discovery of your library. So your library will not be used when compiling a sketch program for a board of such a platform, unless the program also contained an #include directive for another header file which triggered the discovery of your library.
For the predefined search paths, it would only be possible by either editing the configuration files of the boards platform or compiling the sketch by using Arduino CLI directly (because Arduino CLI allows you to define platform properties via the --build-property command line flag).
But for the paths of discovered libraries, the order of the -I flags for the library paths that are added to the compilation commands is determined by the order of the #include directives in the sketch program source code. So you can manipulate the order of those specific flags relative to each other by changing the positions of the #include directives in the sketch source code.
The intent to to have a library that compiles on any board.
On AVR boards (with the least C++ support), it should include a private copy (in the library) of initializer_list, but on a 32bit board, it should include the STL version that is part of the compiler (.../tools/.../include/c++/initializer_list)