The glfw3-library can be made to include vulkan using a definition before the include:
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
i want to do a similar thing, but for debug vs non-debug-mode. I tried the following:
main.cpp:
#define DEBUG_ENABLED
#include "someFile.hpp"
someFile.hpp:
#ifdef DEBUG_ENABLED
std::vector<const char*> _debugExtensions = {
VK_EXT_DEBUG_UTILS_EXTENSION_NAME
};
#endif
#ifndef DEBUG_ENABLED
std::vector<const char*> _debugExtensions = {};
#endif
I checked if that 'passing of definitions' across the header-files worked, by printing the size of the vector, but received zero which indicates that the compiler didn't consider the DEBUG_ENABLED to be defined inside the someFile.hpp.
I then looked on stackoverflow to see how i could react to the definitions of an extern file, and found this post:
Is it possible to use #define from other cpp file?
The accepted answer there claims that
Defines inside a source file aren't seen by other translation units. Implementation files are compiled separately.
My problem with this answer is that my experience tells me the contrary, as the included glfw mentioned above clearly reacted to the GLFW_INCLUDE_VULKAN definition i made.
How might one be able to achieve such behaviour (being able to react to definitions made in an extern source-file) ?
I was asked to create a "minimal reproducable example".
This example showed that the problem really is a different one that i assumed.
The example was successfull in not reproducing the error, but it helps to answer a part of my initial question.
Don't consider this a good solution! This example is provided for clarification of cpp's behaviour, not as a solution to implement in a project!
main.cpp:
#define DEBUG_ENABLED
#include "someFile.hpp"
int main() {
Test test = Test();
test.doCount();
return 0;
}
someFile.hpp
#pragma once
#include <vector>
class Test {
public:
#ifdef DEBUG_ENABLED
std::vector<const char*> _debugExtensions = {
"one",
"two"
};
#endif
#ifndef DEBUG_ENABLED
std::vector<const char*> _debugExtensions = {};
#endif
void doCount();
};
someFile.cpp:
#include <stdio.h>
#include "someFile.hpp"
void Test::doCount() {
printf("count: %lu", _debugExtensions.size());
}
compiling, and running this with:
g++ -g main.cpp someFile.cpp -o out
./out
shows that definitions indeed get preprocessor-directives in included header-files to react accordingly.
The misbehaviour (as pointed out by comments) results from different includsions of the header, of which only a subset defines the DEBUG_ENABLED.
The example here is still prone to that problem!!
(credits to the people commenting)
For a solution there are multiple possibilities:
g++ -DDEBUG_ENABLED