Automatically split (refactor) .h into header and implementation (h+cpp)
Asked Answered
I

4

16

When writing C++ code, I often start by writing full 'implementation' code in my header files, then later need to refactor the implementation into a .cpp file.

This is great, but I find this process laborious, but otherwise pretty easy, so I wondered about whether is there any automated way to do this?

Specifically, I want to convert all class and function definitions in the .h to declarations, and have them declared in a new .cpp file.

I'm using xcode, but I am open to any solutions.

Informality answered 25/6, 2012 at 6:28 Comment(2)
XCode? I thought that meant ObjectiveC(++), not "plain" C++.Halfhardy
XCode is just a generic IDE which focuses on Objective C. Wasn't sure whether to bother mentioning it, but better to provide too much info. ;)Informality
S
10

There is Lazy C++ where you only write one .lzz file and it generates .h and .cpp for you.

I am really looking forward for C++ modules where you only write .cpp and the import file is generated automatically. But we will have to wait for a few years even though Clang has started to implement modules. Here are some examples.

Sabra answered 25/6, 2012 at 6:32 Comment(2)
Hey, I like it! So you could have a makefile rule/dependency generate the cpp and h when the .lzz changes. Nice.Informality
I like the distinction of having the class definition separate from the implementation. Allows you to easily see the interface in one place without it being polluted with all the implementation details. This is what makes Java files so hard to parse and read.Pliske
L
1

You can use some tools such as Makeheaders

http://www.hwaci.com/sw/mkhdr/

but in general, these tools are not complete, especially meeting new c++11 files.

Lewd answered 25/6, 2012 at 6:35 Comment(2)
Mmm, looked promising, but took a lot of tweaks to get it to compile (had to cast dozens of void pointers returned by malloc to their correct types). Compiled, but gave no output when I ran it. Any ideas?Informality
Oh, it's similar to lzz. So you can continue to use lzz :)Lewd
H
1

You may be also interested in Eclipse's function "Refactor/Toggle function". It not always work properly however.

Hoopes answered 7/2, 2014 at 12:8 Comment(0)
A
0

C++20 modules essentially do that for us

As mentioned at: https://quuxplusone.github.io/blog/2019/11/07/modular-hello-world/ clang 2019-11 implements it along:

clang++ -std=c++2a -c helloworld.cpp -Xclang -emit-module-interface -o helloworld.pcm
clang++ -std=c++2a -c -fprebuilt-module-path=. -o helloworld.o helloworld.cpp
clang++ -std=c++2a -fprebuilt-module-path=. -o main.out main.cpp helloworld.o

where:

  • helloworld.cpp contains the implementation
  • helloworld.pcm is a precompiled module, basically an auto-extracted header from the .cpp (but in a clang internal language format) which gets used by main.cpp without an .hpp

So basically clang is the tool, and thus parsing is perfect.

Arbitress answered 13/5, 2020 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.