Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read QT .pro file within program

Tags:

qt

I would like to know if there is some way to read a variable defined in the .pro file of a QT project, during runtime. the thing is that Im trying to compile cuda, for just one architecture (Sm_21), and I want to decide on runtime to use the cuda device that has that capability.

.pro file:

QT       += core gui opengl

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = hello-opengl
TEMPLATE = app


SOURCES += main.cpp\
       mainwindow.cpp \
       glwidget.cpp \



HEADERS  += mainwindow.h \
            glwidget.h \



FORMS    += mainwindow.ui


CUDA_ARCH = sm_21           # Type of CUDA architecture

I would like some way to use this CUDA_ARCH variable in my .cpp. For example

if (CUDA_ARCH == sm_21)
  then pick device 0
else
  pick device 1

Thank you very much!

like image 776
gumlym Avatar asked Feb 04 '26 23:02

gumlym


2 Answers

You can use

DEFINES += CUDA_ARCH_SM_21

and ask in the code for

#ifdef CUDA_ARCH_SM_21

I do not think its possible to directly create a "global" variable in .pro file. But you could just set your global variable CUDA_ARCH in the #ifdef block

#define CA_SM_21 0
#define CA_SM_OTHER 1
#ifdef CUDA_ARCH_SM_21
  int CUDA_ARCH = CA_SM_21
#elseif
  int CUDA_ARCH = CA_SM_OTHER
#endif

if(CUDA_ARCH == CA_SM_21)...
like image 128
Sebastian Lange Avatar answered Feb 07 '26 11:02

Sebastian Lange


You can add preprosessor macro with the value, in .pro do:

CUDA_ARCH = sm_21           # Type of CUDA architecture

DEFINES += CUDA_ARCH=$${CUDA_ARCH}

So this is basically equivalent to adding this to your C code:

#define CUDA_ARCH sm_21

Then in code you can just use the macro, like you'd use any #define, for example:

// enum is most convenient way to get the architectures as symbols
enum CudaArchEnum { sm_21, sm_22};

//... initialize a variable
enum CudaArchEnum value = CUDA_ARCH; // value = sm_21;

//.. or from your question
if (CUDA_ARCH == sm_21) {
  // pick device 0
} else {
  // pick device 1
}

You can also put it to a variable as string, like this:

const char *CudaArchStr = #CUDA_ARCH; // CudaArchStr = "sm_21"
like image 22
hyde Avatar answered Feb 07 '26 11:02

hyde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!