Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referring to a CMAKE variable from code

Tags:

c++

cmake

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

like image 310
Luca Avatar asked Oct 24 '16 12:10

Luca


People also ask

How do you use variables in CMake?

You access a variable by using ${} , such as ${MY_VARIABLE} . CMake has the concept of scope; you can access the value of the variable after you set it as long as you are in the same scope. If you leave a function or a file in a sub directory, the variable will no longer be defined.

How do I print a variable value in CMake?

Run CMake and have a look at the cache with the ccmake GUI tool. Then you'll get all the variables. Or run CMake with -LH then you will get all variables printed after configuration.

How do I view environment variables in CMake?

Use the syntax $ENV{VAR} to read environment variable VAR . To test whether an environment variable is defined, use the signature if(DEFINED ENV{<name>}) of the if() command. For general information on environment variables, see the Environment Variables section in the cmake-language(7) manual.


1 Answers

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

like image 108
Angew is no longer proud of SO Avatar answered Oct 04 '22 03:10

Angew is no longer proud of SO