I am developing a library where the size of some variables depends on a #define and some #define which are created depending on the value of other #define.
The idea is that when a user wants to work with the library they create there own header file with all the preprocessor directives (#define) that are needed and uses this file in the same directory where there main sketch file is and not inside the library source files.
The problem is that when I include the configuration file (which has all the #define's) in the same directory where all my header files of the library are I do not have problems.
i.e.
#include <config.h>
//My Library Code...
But if I declare the configuration header file outside the source files of my Library the compiler doesn't find the #define's that were declared in the main sketch file.
This is pretty vague. Please provide a complete minimal demonstration code.
Likely the problem you're encountering is that the library's source file(s) is a separate translation unit from the user code. The best solution I've found for that is to move all the code that relies on the #define's to the library's .h file.
It sounds to me like you may be attempting to include a header file in a sketch and have defines in the included header file or defines in the sketch affect the way the library is compiled.
You can't do that. Library code is compiled separately. It is independent of the sketch and knows nothing of the sketch, its location, or any files that may be co located with it. It doesn't have to know anything about it as libraries are independent compilation units.
If the compiler cannot find an included header file, then you will a compiler error stating so.
You might run into this, if you are attempting to include a header file in your library that exists in the user sketch directory vs along side of the library code itself.
You can't do that as mentioned above.
If you are not getting an error from the compiler that the header file being included is not found, then it is being included but it isn't working as you want because of what I mentioned above.
Even if you remove the use of a header file and simply put the defines in your sketch, you can't do that either since the library code is compiled separately.
Heres an example of what I am trying to do with the library. The Foo.cpp,foo.h and otherDefinitions.h are in the Arduino\Libraries\Foo Library directory
Yes, the problem is that fooSketch.ino is a separate translation unit from foo.cpp.
As I said, you can work around this by moving the code from foo.cpp to foo.h.
A more complex solution would be to store otherDefinitions.h in the sketch folder and set SIZE_USER in that file (which is easy since otherDefinitions.h will appear as a tab in the Arduino IDE), then make this modification to the hardware packages of any boards you'll be using the code with to allow libraries to #include files from the sketch folder: http://forum.arduino.cc/index.php?topic=329709.msg2279367#msg2279367
The biggest problem with that solution is it's not really friendly to sharing the code because anyone who wants to use your code would need to go through that whole process of modifying their hardware package and repeat the process whenever they update the package (or IDE in the case of Arduino AVR Boards)
pert:
As I said, you can work around this by moving the code from foo.cpp to foo.h.
There are some caveats to doing this.
If going that route then the best way to do this would be to put the class method definitions (the code for the functions) inside the class definition inside the .h file rather than just putting the declarations inside the class definition, which is what is more commonly done with Arduino libraries.
There are other alternatives such as using constructor parameters or initializers in the constructors.
But depending on the specific code, conditionals may be better at reducing code size.
pert:
Yes, the problem is that fooSketch.ino is a separate translation unit from foo.cpp.
As I said, you can work around this by moving the code from foo.cpp to foo.h.
A more complex solution would be to store otherDefinitions.h in the sketch folder and set SIZE_USER in that file (which is easy since otherDefinitions.h will appear as a tab in the Arduino IDE), then make this modification to the hardware packages of any boards you'll be using the code with to allow libraries to #include files from the sketch folder: Adjusting serial buffer size in IDE, mk2 - #4 by Coding_Badly - Suggestions for the Arduino Project - Arduino Forum
The biggest problem with that solution is it's not really friendly to sharing the code because anyone who wants to use your code would need to go through that whole process of modifying their hardware package and repeat the process whenever they update the package (or IDE in the case of Arduino AVR Boards)
Thanks for the info, actually I'm working on Atmel Studio but wanted to know how to implement the library for the Arduino Framework. I will add the step of modifying Board.txt for the implementation of the library.
Defining the functions in the class definition is what I've done in this situation.
I certainly prefer to avoid moving code to the .h file when possible. I was looking at this from the big picture view of how to configure a library via a #define in the sketch but definitely determine whether you even need to use the preprocessor for your specific application. Sometimes there is no other option but I always prefer to avoid using the preprocessor if I can.
yudopplyr:
Thanks for the info, actually I'm working on Atmel Studio but wanted to know how to implement the library for the Arduino Framework. I will add the step of modifying Board.txt for the implementation of the library.
What it's doing is just adding the sketch folder to the compiler's include path via a -I flag.
Since I'm using a GUI for creating the #define options that the user can select, the final user will only need to copy and paste a "config.h" file to their sketch and the library will be compiled accordingly.
Btw, I already did the modifications and it worked, just as a reference for anyone who's trying the same the extra flag in the boards.txt file should be