Luca Luca - 1 month ago 9
C++ Question

Referring to a CMAKE variable from code

I am using

CMAKE 3.4.3
on Windows and what I am trying to do is set a path in CMAKE and try to refer to that in my C++ file.

What I tried was as follows:

In CMakeLists.txt file

ADD_DEFINITIONS(-DNV12_2_ARGB_PTX_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ptx")


Now, I try and refer to it from my C++ file as follows:

#ifdef NV12_2_ARGB_PTX_DIR
#define PTX_DIR D_NV12_2_ARGB_PTX_DIR
#endif


And when I try to refer to it as:

std::cout << PTX_DIR << std::endl;


I get the error:

'C:/Users/Luca/project/src/lib/ptx': No such file or directory


Also, Visual studio intellisense complains:

IntelliSense: identifier "PTX_DIR" is undefined


Not sure why it wants to open a file with this variable...

Answer

The problem is with your use of add_definitions. You're effectively passing the value of ${CMAKE_CURRENT_SOURCE_DIR}/ptx as an additinal argument on the compiler's command line, which the compiler probably interprets as a source file it should compile. Check the full command line invoked to be sure.

You probably intended this:

add_definitions(-DNV12_2_ARGB_PTX_DIR="${CMAKE_CURRENT_SOURCE_DIR}/ptx")

Note that you may have to play around with escaping the quotation marks to get them all the way down to C++. Alternatively, you could use configure_file().

Comments