Automatically split (refactor) .h into header and implementation (h+cpp) - c++

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.

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.

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.

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

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.

Related

How can I compile three files together? [duplicate]

I've just inherited some C++ code that was written poorly with one cpp file which contained the main and a bunch of other functions. There are also .h files that contain classes and their function definitions.
Until now the program was compiled using the command g++ main.cpp. Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the g++ main.cpp command?
list all the other cpp files after main.cpp.
ie
g++ main.cpp other.cpp etc.cpp
and so on.
Or you can compile them all individually. You then link all the resulting ".o" files together.
To compile separately without linking you need to add -c option:
g++ -c myclass.cpp
g++ -c main.cpp
g++ myclass.o main.o
./a.out
Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the "g++ main.cpp" command?
Compiling several files at once is a poor choice if you are going to put that into the Makefile.
Normally in a Makefile (for GNU/Make), it should suffice to write that:
# "all" is the name of the default target, running "make" without params would use it
all: executable1
# for C++, replace CC (c compiler) with CXX (c++ compiler) which is used as default linker
CC=$(CXX)
# tell which files should be used, .cpp -> .o make would do automatically
executable1: file1.o file2.o
That way make would be properly recompiling only what needs to be recompiled. One can also add few tweaks to generate the header file dependencies - so that make would also properly rebuild what's need to be rebuilt due to the header file changes.
.h files will nothing to do with compiling ... you only care about cpp files... so type g++ filename1.cpp filename2.cpp main.cpp -o myprogram
means you are compiling each cpp files and then linked them together into myprgram.
then run your program ./myprogram
I know this question has been asked years ago but still wanted to share how I usually compile multiple c++ files.
Let's say you have 5 cpp files, all you have to do is use the * instead of typing each cpp files name E.g g++ -c *.cpp -o myprogram.
This will generate "myprogram"
run the program ./myprogram
that's all!!
The reason I'm using * is that what if you have 30 cpp files would you type all of them? or just use the * sign and save time :)
p.s Use this method only if you don't care about makefile.
You can still use g++ directly if you want:
g++ f1.cpp f2.cpp main.cpp
where f1.cpp and f2.cpp are the files with the functions in them. For details of how to use make to do the build, see the excellent GNU make documentation.
As rebenvp said I used:
g++ *.cpp -o output
And then do this for output:
./output
But a better solution is to use make file. Read here to know more about make files.
Also make sure that you have added the required .h files in the .cpp files.
You can use several g++ commands and then link, but the easiest is to use a traditional Makefile or some other build system: like Scons (which are often easier to set up than Makefiles).
If you want to use #include <myheader.hpp> inside your cpp files you can use:
g++ *.cpp -I. -o out
I used to use a custom Makefile that compiled all the files in current directory, but I had to copy it in every directory I needed it, everytime.
So I created my own tool - Universal Compiler which made the process much easier when compile many files.
You can do that using a single command assuming all the needed .cpp and .h files are in the same folder.
g++ *.cpp *.h -Wall && ./a.out
It will compile and execute at the same time.
when using compiler in the command line, you should take of the following:
you need not compile a header file, since header file gets substituted in the script where include directive is used.
you will require to compile and link the implementation and the script file.
for example let cow.h be header file and cow.cpp be implementation file and cow.cc(c++ files can have extension .cpp, .cc, .cxx, .C, .CPP, .cp) be script file.
Since gcc compiler notation for c++ file is g++, we can compile and link the files using
$g++ -g -Wall cow.cpp cow.cc -o cow.out
options '-g' and '-Wall' are for debugging info and getting warning for errors. Here cow.out is the name of the executable binary file that we can execute to run the program. it is always good to name your executable file otherwise name will be automatically given which might be confusing at times.
you can also do the same by using makefiles, makefiles will detect, compile and link automatically the specified files.
There are great resources for compilation using command line
enter link description here
~/In_ProjectDirectory $ g++ coordin_main.cpp coordin_func.cpp coordin.h
~/In_ProjectDirectory $ ./a.out
... Worked!!
Using Linux Mint with Geany IDE
When I saved each file to the same directory, one file was not saved correctly within the directory; the coordin.h file. So, rechecked and it was saved there as coordin.h, and not incorrectly as -> coordin.h.gch. The little stuff.
Arg!!

How do I generate and use precompiled headers with Clang++?

The official docs state how precompiled headers are to be used through the -cc1 interface, like so to generate them:
$ clang -cc1 test.h -emit-pch -o test.h.pch
And to use them:
$ clang -cc1 -include-pch test.h.pch test.c -o test.s
The problem is that the -cc1 interface is way too low-level to be used by developers from the CLI. In fact, the regular high-level interface ultimately calls into the low-level -cc1 interface by supplying it with a very large set of arguments that are necessary for its correct operation, for example the include paths appropriate for the compile time system. Without these arguments, the -cc1 interface has no prayer of working:
$ clang++ -cc1 /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h -emit-pch -o std.pch
/usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33:10: fatal error: 'cassert' file not found
#include <cassert>
^~~~~~~~~
1 error generated.
Is there a way to use precompiled headers from the high-level interface, such that a developer may conveniently tap into this feature during their daily work?
I don't know why the clang docs do not explain this, but indeed as #selbie surmises, it is possible to use Clang precompiled headers (PCH) without using -cc1.
To generate a PCH file:
$ clang -c -o big-header.hh.pch big-header.hh <other options>
This is the normal compile command, except the input file has the .hh (C++ header) extension. clang will recognize that means to generate a PCH. Alternatively, pass -xc++-header before the header file name if you do not want to use that extension (or another, like .H or .hpp, that is unambiguously C++).
You can tell that big-header.hh.pch is not object code (despite the -c on the command line) because file will say it is "data" (at least my version does) rather than object code. To be extra sure, run strings big-header.hh.pch | head. The first line should be "CPCH" (meaning "Clang PCH").
To use the PCH file:
$ clang -c -include-pch big-header.hh.pch <other compile options>
The addition of -include-pch big-header.hh.pch is the key step that is different compared to gcc. Clang will not automatically pick up PCH files just due their name.
The above was tested with Clang+LLVM-14.0.0 on Linux.
I think the root of your problem is that your filename is test.h and clang thinks you are compiling C code, not C++ code. Hence, when you include <cassert>, clang doesn't know it should be looking at the C++ include path. Try naming your file test.hpp. You only have to name the file you want as the precomp header with .hpp. You can keep all your header files with .h extensions.
In any case, I might be getting this confused with gcc/g++, but Clang follows the same behavior whenever I compile my code on Mac. This is how I make use of precompiled headers. Read on...
If you've got a C++ header file you want to precompile, just compile it as any other .cpp file. Notice that I'm using .hpp as the file extension so the compiler picks it up as a C++ header file.
clang -c precomp.hpp
This will produce precomp.hpp.gch
Now to use the precomp by any other ordinary C++ file, just include the ordinary .hpp file:
// main.cpp
#include "precomp.hpp"
void func1() {...}
void main() {...}
The compiler will automatically use the corresponding .gch file if its present in place of the original .hpp file.

Decrease clang compile time with precompiled headers

I am working on a database project that compiles queries (expressed in some higher level language) into c++ code. This code is compiled and executed by the database. That part works perfectly fine.
Right now, I am trying to reduce the compile time for the C++ query code. I was wondering whether I can use precompiled headers to gain performance here.
The query is translated into a file called Query.cpp which includes library/src/Database.hpp. The Database.hpp file includes further files like StandardTypes.hpp and so on. Can I precompile all those header files to speed up the compilation of Query.cpp? If yes, how can I do that? I could not find any good example for precompiled headers so far, only some really basic stuff.
I use the following command to compile Query.cpp:
clang++ -fPIC -std=c++11 Query.cpp -I./library/src/ -shared -o libquery.so;
to create pre-compiled header include all the headers you don't change into Query.h and use:
clang -cc1 Query.h -emit-pch -o Query.h.pch
to use the pre-compiled header type:
clang -cc1 -include-pch Query.h.pch Query.cpp -shared -o libquery.so;
Query.cpp needs to include Query.h

include class and compile with g++

Im a beginner in C++ and working with unix. So here is my question.
I`ve written few lines in the main-function, and i needed a function, that is defined in the c_lib - library.
main.cpp:
#include <iostream>
#include "c_lib.cpp"
int main()
{
return 0;
}
i want to execute it on the terminal, so i wrote
g++ -c c_lib.cpp
g++ -c main.cpp
g++ -o run c_lib.o main.o
Until here, there is no error report.
Then
./run
I get the error
error: ./run: No such file or directory
What's wrong?
Including a .cpp is not usually done, usually only headers are included. Headers usually contain the declarations that define the interface to the code in the other .cpp
Can you show us the source of c_lib? That may help.
As the source of c_lib is #included, there is no need to compile it seperately. In fact this can/will cause errors (multiple definitions being the first to come to mind). You should only need to do:
g++ -o run main.cpp
to compile your code in this case.
(When using a header (.h), you will need to compile the implementation (.cpp) seperately)
Compile with warnings turned on:
g++ -Wall -Wextra -o run main.cpp
and you will get more output if there are problems with your code.
Is the run file being output by gcc? You can test by calling ls in the terminal (or ls run to only show the executable if it is present).
If the executable is present, it could be that it isn't marked as runnable. I'll go into that if it is a problem as it is outside the general scope of the site (though still related)
First of all you should not include source file into another source. You should create a header file and put declarations there (that allows main() to call functions from c_lib.cpp or use global variables if any)
When you run g++ you have to look into it's output, if operation succeed or not. In your case it failed so executable run was not created.
Usually you do not call compiler manually but write a makefile and let make utility to call g++.

Using G++ to compile multiple .cpp and .h files

I've just inherited some C++ code that was written poorly with one cpp file which contained the main and a bunch of other functions. There are also .h files that contain classes and their function definitions.
Until now the program was compiled using the command g++ main.cpp. Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the g++ main.cpp command?
list all the other cpp files after main.cpp.
ie
g++ main.cpp other.cpp etc.cpp
and so on.
Or you can compile them all individually. You then link all the resulting ".o" files together.
To compile separately without linking you need to add -c option:
g++ -c myclass.cpp
g++ -c main.cpp
g++ myclass.o main.o
./a.out
Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the "g++ main.cpp" command?
Compiling several files at once is a poor choice if you are going to put that into the Makefile.
Normally in a Makefile (for GNU/Make), it should suffice to write that:
# "all" is the name of the default target, running "make" without params would use it
all: executable1
# for C++, replace CC (c compiler) with CXX (c++ compiler) which is used as default linker
CC=$(CXX)
# tell which files should be used, .cpp -> .o make would do automatically
executable1: file1.o file2.o
That way make would be properly recompiling only what needs to be recompiled. One can also add few tweaks to generate the header file dependencies - so that make would also properly rebuild what's need to be rebuilt due to the header file changes.
.h files will nothing to do with compiling ... you only care about cpp files... so type g++ filename1.cpp filename2.cpp main.cpp -o myprogram
means you are compiling each cpp files and then linked them together into myprgram.
then run your program ./myprogram
I know this question has been asked years ago but still wanted to share how I usually compile multiple c++ files.
Let's say you have 5 cpp files, all you have to do is use the * instead of typing each cpp files name E.g g++ -c *.cpp -o myprogram.
This will generate "myprogram"
run the program ./myprogram
that's all!!
The reason I'm using * is that what if you have 30 cpp files would you type all of them? or just use the * sign and save time :)
p.s Use this method only if you don't care about makefile.
You can still use g++ directly if you want:
g++ f1.cpp f2.cpp main.cpp
where f1.cpp and f2.cpp are the files with the functions in them. For details of how to use make to do the build, see the excellent GNU make documentation.
As rebenvp said I used:
g++ *.cpp -o output
And then do this for output:
./output
But a better solution is to use make file. Read here to know more about make files.
Also make sure that you have added the required .h files in the .cpp files.
You can use several g++ commands and then link, but the easiest is to use a traditional Makefile or some other build system: like Scons (which are often easier to set up than Makefiles).
If you want to use #include <myheader.hpp> inside your cpp files you can use:
g++ *.cpp -I. -o out
I used to use a custom Makefile that compiled all the files in current directory, but I had to copy it in every directory I needed it, everytime.
So I created my own tool - Universal Compiler which made the process much easier when compile many files.
You can do that using a single command assuming all the needed .cpp and .h files are in the same folder.
g++ *.cpp *.h -Wall && ./a.out
It will compile and execute at the same time.
when using compiler in the command line, you should take of the following:
you need not compile a header file, since header file gets substituted in the script where include directive is used.
you will require to compile and link the implementation and the script file.
for example let cow.h be header file and cow.cpp be implementation file and cow.cc(c++ files can have extension .cpp, .cc, .cxx, .C, .CPP, .cp) be script file.
Since gcc compiler notation for c++ file is g++, we can compile and link the files using
$g++ -g -Wall cow.cpp cow.cc -o cow.out
options '-g' and '-Wall' are for debugging info and getting warning for errors. Here cow.out is the name of the executable binary file that we can execute to run the program. it is always good to name your executable file otherwise name will be automatically given which might be confusing at times.
you can also do the same by using makefiles, makefiles will detect, compile and link automatically the specified files.
There are great resources for compilation using command line
enter link description here
~/In_ProjectDirectory $ g++ coordin_main.cpp coordin_func.cpp coordin.h
~/In_ProjectDirectory $ ./a.out
... Worked!!
Using Linux Mint with Geany IDE
When I saved each file to the same directory, one file was not saved correctly within the directory; the coordin.h file. So, rechecked and it was saved there as coordin.h, and not incorrectly as -> coordin.h.gch. The little stuff.
Arg!!