Hello,
Basically as title says, I’m developing library that needs to define something if another library is present.
I have tried:
#if __has_include("avr8-stub.h")
#define AVR_DEBUG
#endif
#if defined(__has_include)
#warning "__has_include is supported by your compiler."
#else
#warning "__has_include is not supported by your compiler."
#endif
#if defined(AVR_DEBUG)
#warning "Debug mode enabled." // this is hilighted
#else
#warning "Debug mode disabled."
#endif
VSCode ide sees it, compiler does not.
warning: #warning "__has_include is supported by your compiler." [-Wcpp]
#warning "__has_include is supported by your compiler."
warning: #warning "Debug mode disabled." [-Wcpp]
#warning "Debug mode disabled."
^~~~~~~
Do you have any suggestions how to implement this in my library without any additional configuration from the user using the library. I know that this can be easly fixed with simple -DAVR_DEBUG in platformio.ini but i would like to make this automated without the need for any manual intervention with defining or custom scripts.
I have avr8-stub.h
included in main.cpp
before my library but my library gets compiled for itself without ever knowing anything about avr8-stub.h
.
Library is installed via lib_deps git in platformio.ini
Does the header file in question have a standard guard? If it does, you can see in your library if it is defined.
#ifndef OTHERHEADER
#define OTHERHEADER
…
#endif
If it used a #pragma once, see if there is a unique define you can use.
I can’t imagine how this is supposed to work!
Translation units (.c / .cpp files) are compiled separately. Unless they have a common header file or include the respective header file of the other library, they cannot see each other’s macros.
The only solution I can see would be to define a global macro in platformio.ini (build_flags = -Dsomemacro).
Proof of concept:
Project file structure:
.
├── lib
│ ├── LibA
│ │ ├── LibA.cpp
│ │ └── LibA.h
│ ├── LibB
│ │ ├── LibB.cpp
│ │ └── LibB.h
│ └── README
├── src
│ └── main.cpp
└── platformio.ini
LibA.h
#ifndef _LIB_A_
#define _LIB_A_
const char* functionFromLibA();
#endif
LibA.cpp
#include "LibA.h"
#ifdef _LIB_B_
static const char* message = "LibA: LibB is present";
#else
static const char* message = "LibA: LibB is NOT present";
#endif
const char* functionFromLibA() { return message; }
LibB.h
#ifndef _LIB_B_
#define _LIB_B_
const char* functionFromLibB();
#endif
LibB.cpp
#include "LibB.h"
#ifdef _LIB_A_
static const char* message = "LibB: lib A is present!"
#else
static const char* message = "LibB: lib A is NOT present";
#endif
const char* functionFromLibB() { return message; }
main.cpp
#include <iostream>
#include <LibA.h>
#include <LibB.h>
int main() {
std::cout << functionFromLibA() << std::endl;
std::cout << functionFromLibB() << std::endl;
return 0;
}
platformio.ini
[env:native]
platform = native
Output
LibA: LibB is NOT present
LibB: LibA is NOT present
sivar2311 has given an example that demonstrates the shortfall of what I suggested.
But I assure you that a simple scenario like this will work
#include “avr8-stub.h”
#include “myHeaderFile.”
…
File: avr8-stub.h
#ifndef AVR8_STUB_H
#define AVR8_STUB_H
…
File myHeaderFile.h
#if defined(AVR8_STUB_H)
…
If avr8-stub.h uses the #pragma once method this will not work and if you don’t include the header before yours in the same unit, it will not work.
This is the only way I know of to come close to what you want. I’m not aware of any method outside of build configuration files that could do this.