so how do I write so called portable code to be used on multiple projects if I can't call one project specific file that is in the main folder of the program. So project specific files have to go into my general libraries folder or I'll have to create yet another global folder to put all project specific stuff in...... So much for an IDE!
There are a couple of ways of connecting portable library-type code to project-specific code. Which works best depends on the nature of the project and the library.
1. Your library has no dependencies on project-specific code or data: In this case your library declares its public functions (and if necessary, variables) to the project code via a header file that the project #includes. This is the simplest case.
2. Your library depends on a set of project-specific functions that will not change as the application runs: In this case your library can declare (but not define!) a set of functions that are defined within the project. Your library will know that those functions exist because it declared them, and as long as the linker can find their definitions (which your IDE should take care of) everything will come together properly. Your library could optionally provide weak definitions (
__attribute__((" weak")) in GCC), which allows the library to provide default functions that your project may or may not override.
3. Your library depends on a set of project-specific functions that may change depending on the application state: In this case your library might declare a set of function pointers to which your project will assign the addresses of functions that provide the project-specific code. This is common if the library will need to access different physical interfaces within the application, IE, fatfs, which might target both on-board flash and an SD card. Often the function pointers are gathered in a struct type provided by the library.
As a general rule, your application should reach into the library and not the other way around. If the library needs to act on data 'owned' by the application, then it's often preferable for the application to pass a reference to that data into the library. If the library and the application really need to have direct access to a shared variable, then you can define that variable in the library and expose it to the application via the library's header file. The
extern keyword is helpful here, as it allows you to declare a variable without redefining it.
You might have a situation where you want to configure a library for your application via the preprocessor--in other words, you want to be able to #define a bunch of things in your project that influence the behavior of the library. In this case, your library can do something like
#include "lib_conf.h", where lib_conf.h is stored in one of your compiler's search paths.
It's really crucial to understand
the difference between a definition and a declaration, so if that's not yet clear to you, definitely read up on the subject as it's crucial to understanding how a pile of C comes together into a working program.