0

In the PRO file of my Qt5 C++ desktop project I have the following declaration:

DEFINES += PROJECT_VERSION=\\\"1.0.0.0\\\"

In the header file of a class, say Foo, I declare and define a method to obtain the version like this:

QString version() { return PROJECT_VERSION; }

When I try to compile the code, a get the following error:

error: C2065: 'PROJECT_VERSION': undeclared identifier

enter image description here

As shown in the screenshot, PROJECT_VERSION is properly color coded and the tooltip is correct too, for which it seems to me that it is recognized. Then why is this error?

I don't have an idea what could cause this and how such thing is possible in a first place. Any help will be much appreciated.

scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • 1
    Well, it isn't declared. It doesn't appear in any TU. You inject it from outside. – StoryTeller - Unslander Monica Jan 07 '18 at 13:50
  • @StoryTeller, what is a TU? Besides, I use PROJECT_VERSION in a couple of places and it is recognized. – scopchanov Jan 07 '18 at 13:52
  • 2
    Translation Unit. What the compiler parses to see preprocessing tokens and identifiers. The fact that the compiler knows what this macro is due to some external forces, doesn't mean static analysis tools have that information. – StoryTeller - Unslander Monica Jan 07 '18 at 13:53
  • @StoryTeller, I have been following this example: https://stackoverflow.com/a/41650860/5366641 according to it such thing should work. And it does, but not everywhere. – scopchanov Jan 07 '18 at 13:57
  • @StoryTeller, furthermore, I don't understand what static analysis tools do you mean. The error is issued by the compiler. – scopchanov Jan 07 '18 at 14:01
  • 1
    Well, you have a clear configuration problem. Hopefully someone who is familiar with your toolchain will come along. I myself have limited experience with this particular one. – StoryTeller - Unslander Monica Jan 07 '18 at 14:01
  • @StoryTeller, I see. In any case, I appreciate the time you've spent on the issue. – scopchanov Jan 07 '18 at 14:04
  • 1
    @StoryTeller, btw, the "toolchain" seems to me as a hint in the right direction, hence I've retagged the question to be more specific about that. – scopchanov Jan 07 '18 at 14:12

1 Answers1

2

Im my desire to present only the relevant information, I have oversimplified the description of the problem.

It turns out, that another Project B, from the SUBDIRS hierarchy is including the header with the version() method. The declaration of PROJECT_VERSION takes place in the PRO file of Project A, part of which is the header, but not in the PRO file of Project B, where it is included. Qt Creator sees PROJECT_VERSION from Project A and the code completion, the color coding and the tooltips work (I think that is what @StoryTeller meant under "static analysis"). The compiler, however, does not, so it complains about that.

The confusion was caused by the fact, that the error did not occur in Project B, where the header is included, but in Project A, where the header AND the declaration of PROJECT_VERSION are. So at first it seemed like a contradiction.

Solution: Added DEFINES += PROJECT_VERSION=\\\"1.0.0.0\\\" to the PRO file of Project B and now it compiles like a charm.

Once again, many thanks to @StoryTeller for the useful hints.

scopchanov
  • 7,966
  • 10
  • 40
  • 68