0

Following an example I saw in a repo, I added a "RESOURCE_PATH" variable using the -D flag in my main CMakeLists:

ADD_DEFINITIONS(-DRESOURCE_PATH="${PROJECT_SOURCE_DIR}/../Resources") 

I was then able to access the variable in my code:

gHelloWorld = SDL_LoadBMP(RESOURCE_PATH "/hello_world.bmp");

My question is: how does this work? According to the doc, this is adding RESOURCE_PATH to CMakeCache.txt. Is everything in the cache accessible from my project like this? Is this some hack that'll break in certain cases?

Lastly, Eclipse gives me a "Bad character sequence encountered" warning on the lines where I use RESOURCE_PATH. Assuming this is a proper approach, how can I get rid of those warnings?

Archduke
  • 333
  • 1
  • 10
  • 2
    This is simply defining a preprocessor macro, see https://cmake.org/cmake/help/latest/command/add_definitions.html –  Dec 30 '18 at 17:41
  • Your first couple of questions are confusing because you haven't showed anything that would add any variable into CMakeCache.txt. The only variable shown is `${PROJECT_SOURCE_DIR}` and that isn't cached. "RESOURCE_PATH" isn't a variable it is a compiler definition. – fdk1342 Dec 30 '18 at 19:31
  • I think I get where my misunderstanding was. Is "-D" a convention meaning "define" in general or something along those lines? I saw the -D used here and assumed it was serving the same function as calling "cmake -D" https://cmake.org/cmake/help/v3.0/manual/cmake.1.html – Archduke Dec 30 '18 at 21:30
  • You're confusing `-D` for cmake cache variables, and `-D` for creating a definition able to be used by the preprocessor. `ADD_DEFINITIONS` with `-D` creates the variable (see [here](https://cmake.org/cmake/help/v3.0/command/add_definitions.html), [here](https://stackoverflow.com/questions/5213800/how-to-define-c-preprocessor-variable-in-makefile) and [here](https://www.rapidtables.com/code/linux/gcc/gcc-d.html) for further details) – Steve Lorimer Dec 31 '18 at 15:41

0 Answers0