Luca Luca - 1 year ago 72
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


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

#ifdef NV12_2_ARGB_PTX_DIR

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 Source

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:


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().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download