Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool to create an amalgamation/combine all source files of a library into one for C/C++?

Tags:

c++

c

build

SQLite and googletest come with a very easy-to-use, single-file version which makes it a breeze to use it in other projects, as you just need to add a single source file. Both of them use home-brew tools to create the combined source file, so I wonder if there is a more generic tool for this? It should take a list of implementation/header files and spit out a combined header/source, and fix up the local includes. I'm fine if it doesn't handle conditional includes/includes with different #defines before them like Boost.Tuple/MPL uses them. A typical target library would be something like ICU.

like image 908
Anteru Avatar asked Apr 27 '10 07:04

Anteru


2 Answers

try this https://github.com/vinniefalco/Amalgamate/ may be it can help...

like image 66
Pushkoff Avatar answered Oct 10 '22 12:10

Pushkoff


If your includes are correctly defined (that is, there are guards in all header files, and each header/code unit contains all includes that it requires) then you can do it 'half-manually'. Locate system header includes and comment them out, then create a header that just includes everything in any random order and preprocess the header (in gcc that would be gcc -E) and then operate similarly with the code units.

This manual approach can be cumbersome, but if you only need to do it once it will be fine. Then again, even if merging the header files might make sense I prefer not to do it. I would actually leave the files separate, and if you feel like you need to simplify access to it, provide bundling headers that just include the others. This is the approach that some boost libraries take, where you can include the details of what you want or a single header that includes everything else. The code can be compiled/linked into an static lib and used as if it was a single element.

like image 21
David Rodríguez - dribeas Avatar answered Oct 10 '22 12:10

David Rodríguez - dribeas