Author Topic: How to handle generated files from header.  (Read 2224 times)

Offline jarod42

  • Multiple posting newcomer
  • *
  • Posts: 87
How to handle generated files from header.
« on: September 05, 2022, 06:45:16 pm »
Working on premake-codeBlocks and premake-qt to generate Code::Blocks project with Qt, I encountered issues with generated source files.

I have 3 different source files type, expected generation is:

resources.qrc -> resources.cpp (-> resources.o) OK
foo.ui -> ui_foo.h (OK too)
foo.h -> moc_foo.cpp (-> moc_foo.o)

I used respectively (path simplified (tool options and complete path for bin&obj omitted)):

Code
<Option compile="1" />
<Option link="1" />
<Option weight="40" />
<Option compiler="gcc" use="1" buildCommand="qrc resource.qrc -o resource.cpp\n$compiler $options $includes -c resource.cpp -o $object" />
Code
<Option compile="1" />
<Option weight="35" />
<Option compiler="gcc" use="1" buildCommand="uic foo.ui -o ui_foo.h" />
Code
<Option compile="1" />
<Option link="1" />
<Option weight="40" />
<Option compiler="gcc" use="1" buildCommand="moc foo.h -o moc_foo.cpp\n$compiler $options $includes -c moc_foo.cpp -o $object" />

Issue for the last one with `$object` which is `foo.h.gch` for `foo.h`whereas I expect `moc_foo.o` (or any unique .o, `foo.o` would also be problematic as foo.cpp also exists).
I tried to replace directly `$object` by `moc_foo.o`, but then the link fails.

Is there a way to specify object (it is shown in UI, but as read only) or fix the issue differently?

(I previously added generated file directly in project, but build failed as the file doesn't exist at the begining of the build).
« Last Edit: September 05, 2022, 06:47:07 pm by jarod42 »

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: How to handle generated files from header.
« Reply #1 on: September 05, 2022, 07:32:02 pm »
Instead of adding custom build commands to files it is better to define a compiler build command for the file extension. This allows to specify the outputs which will get added to the build automatically, without causing an error if they don't exist yet.

In your case, this would work for the first two cases, it won't work for the last one, because this file has a standard extension and the default build command creates a precompiled header with that .h.gch extension. Are foo.ui and foo.h related? Do always exist both of them? In that case, you could add the step for foo.h to the build command for .ui files.

Offline jarod42

  • Multiple posting newcomer
  • *
  • Posts: 87
Re: How to handle generated files from header.
« Reply #2 on: September 05, 2022, 07:45:47 pm »
> Are foo.ui and foo.h related?

Not necessary.

foo.h is a regular header, and generation should be done if a MACRO (QOBJECT) is present in the file.
« Last Edit: September 05, 2022, 09:57:23 pm by jarod42 »

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: How to handle generated files from header.
« Reply #3 on: September 05, 2022, 08:36:44 pm »
I'm not familiar with Qt so i don't know how its preprocessing works.

How to you know that you have to add foo.h to the project then? Do you scan the file yourself? Would be strange if Qt requires you to do this on your own, how do the Qt build tools do this?

Offline jarod42

  • Multiple posting newcomer
  • *
  • Posts: 87
Re: How to handle generated files from header.
« Reply #4 on: September 05, 2022, 10:25:46 pm »
We have to scan ourself (or provide list manually).

CMake has AUTOMOC

Not familiar with QMake (the build tool from Qt), but it seems also scanning/processing files given to HEADERS and SOURCES entries.

It is was for my project, as workaround, I might indeed change file extension to make it easier.
But it is for a project/solution builder...

It seems we cannot have a (linkable) *.o from a *.h

At worst , I will warn that moc is unsupported for Code::Blocks.

Thanks.