PDA

View Full Version : How to define a string in a .pro file



ChristineD
9th October 2012, 19:55
Hi,

I want to define a string in a .pro file. qmake is fine. When I compile the project in VS2010, the compiler complains about VERSION_STRING.

I've tried the following and still get errors.

DEFINES += VERSION_STRING=\\\"4.3\\\"

DEFINES += VERSION_STRING=\\\"'4.3'\\\"

Any help would be greatly appreciated!
ChristineD

wysota
9th October 2012, 21:12
defines += version_string=\"4.3\"

Sorry for the lowercase, the forum software tries to be smarter than me.

ChristineD
11th October 2012, 16:36
Thank you! That gets me past that compile issue.

I'm wondering and trying to understand why there is a difference in how to define a constant string in the .pro file.
Is it compiler dependent?
Is there a QT reference that explains the syntax?

Our original project is compiled in windows, using visual studio 2008, 32 bit, and with qt4.8.1. We define VERSION_STRING in the .pro file
DEFINE += VERSION_STRING=\\\"4.3\\\" and are able to compile fine and access this string in the code.

My question came up because I am compiling in windows, using visual studio 2010, 64 bit, so I compiled qt4.7.4 for 64 bit. Using the the above define created
compiler issues but your suggestion of DEFINE += VERSION_STRING=\"4.3\" gets me past the compiler issue.

Thanks,
ChristineD

ChrisW67
11th October 2012, 22:52
The single backslash, as in C++ code, tells qmake to insert the double quote literally into the string it then places into the DEFINES variable. The value of that variable is placed literally into the Makefile that qmake later generates so that C++ compilations commands get the correct structure for the shell to execute.


// In the PRO file
DEFINES += -DTEST=\"value and space\"
// what ends up in the Makefile
DEFINES = -DTEST="value and space" -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED ...
// So the shell gets this command when you run make
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DTEST="value and space" -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED ...


If Visual Studio causes the value to be parsed twice then you would need to double the backslashes in order to get the same end result. The pair \\ collapses to a single \, and the pair \" collapses to a double-quote.


// Start with
-DTEST=\\\"value and space\\\"
// after first parser has reduced escaping
-DTEST=\"value and space\"
// after second parsing reduces escaping
-DTEST="value and space"
// which is what you want in your actual compiler command.
(The exact processing VS does is unknown to me)

ChristineD
16th October 2012, 17:14
Thanks for your explanation! It's very helpful!

ChristineD