C++ forward declaration

51 views
Skip to first unread message

Ádám Balázs

unread,
Jun 7, 2019, 9:54:41 AM6/7/19
to Cap'n Proto
Hi all,

I try to use Cap'NProto in a CMake based C++ project. CMake runs capnp tool to generate headers/sources and adds them to the project. With this approach I eliminate the inconsistent states, however the generated files are touched every time CMake runs, so the compiler recompiles them during the build. With forward declaration it wouldn't be a big deal, but as far as I understand the generated code I can not forward declare builders and readers since they are nested classes. This is a really unfortunate situation, because the headers using the generated code are used in many components (configurations). Can I somehow forward declare generated classes? Could you advise some technique to solve the described situation? Is it intended to prohibit forward declaration?

Thank you in advance: Adam

Kenton Varda

unread,
Jun 9, 2019, 9:51:46 AM6/9/19
to Ádám Balázs, Cap'n Proto
Hi Adam,

Is cmake running the capnp tool every time, or only when the capnp files change? I would suggest configuring it to only re-run the code generation when the .capnp files have changed. I don't personally know cmake but it surely supports this.

Unfortunately you are correct that there's no good way to forward-declare the nested types generated by the capnp tool.

-Kenton

--
You received this message because you are subscribed to the Google Groups "Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to capnproto+...@googlegroups.com.
Visit this group at https://groups.google.com/group/capnproto.
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/d84ad038-22be-48c0-aac3-050fafc18ca1%40googlegroups.com.

Stuart Dootson

unread,
Jun 9, 2019, 10:35:27 AM6/9/19
to Kenton Varda, Cap'n Proto, Ádám Balázs
To view this discussion on the web visit https://groups.google.com/d/msgid/capnproto/CAJouXQ%3DSbRggayxzM1goybtb-%3D6LsEKMAoVeMJTDH-envvBw%3DQ%40mail.gmail.com.

There’s a function (CAPNP_GENERATE_CPP) defined in the file ‘CapnProtoMacros.cmake’ which has CMake code to generate C++ files for capnp files. It looks like it’s set up properly to only run when required (by which I mean it uses ‘add_custom_command’ with appropriate output & dependency parameters set up). I’ve used the same setup for other codegen tasks, and it’s worked fine.

The custom command generates the source (by default) in CMAKE_CURRENT_BINARY_DIR, which is what you’d expect for CMake. It’ll regenerate the files for a new build directory, but after that should manage dependencies correctly after that.

Stuart

Harris Hancock

unread,
Jun 9, 2019, 11:32:27 AM6/9/19
to Stuart Dootson, Kenton Varda, Cap'n Proto, Ádám Balázs
The samples directory has an example of how to use capnp_generate_cpp(): https://github.com/capnproto/capnproto/blob/master/c%2B%2B/samples/CMakeLists.txt#L24

As Stuart said, it only runs when required.

Ádám Balázs

unread,
Jun 12, 2019, 4:39:31 AM6/12/19
to Harris Hancock, Stuart Dootson, Kenton Varda, Cap'n Proto
Thank you very much, it works :)
Reply all
Reply to author
Forward
0 new messages