r/C_Programming • u/thradams • Sep 18 '26
Perfect c amalgamator tool requirements
Help me create a list of requirements for the perfect amalgamator tool. (I want to improve my existing one then share.)
I will write down some items that I think are necessary.
Basic stuff
- Follow
#include(except system includes) and add each file only once. - Preserve the original order.
- Allow exceptions for files that should not be appended.
More...
4. Remove #pragma once (since it does not make sense in the amalgamated source file). preserve other pragmas.
5. #undef macros that were defined inside the source file before appending the next source file. Keep definitions from headers.
More advanced...
6. Detect when a static function/variable is appended more than once. Auto-Rename?
7. I was thinking about adding static in front of functions that are not extern. This could be an alternative to the workaround of using PUBLIC/PRIVATE macros, where programmers define PRIVATE as static in the amalgamated file. Then let's say the function you want to be public, you write extern in front of it.
8. A header file that is the public interface for the amalgamator could be read before.This also could help to define what should be make static. It also could be interesting to add (optionally) a prefix in all public functions, etc. This would work as "namespace it"
9. Add optionally #line to the original source?
Did I forget anything?
Edit 1:
10. Deduplicating system include #include <stdio.h> just once..
11. Header macro collision. I think this is uncommon but two headers could define the same macro name.
12. Maybe put all system includes at the top?
Edit 2:
Maybe add a -ignore block to ignore #ifdef block that should not be included? For instance, lets say lib has #ifdef TEST and this code does not need to be in the published amalgamated version.
7
u/josk613 Sep 18 '26
What's the use case?
2
u/pjl1967 Sep 18 '26 edited Sep 18 '26
It sounds like an interesting project. (I tend to like software development tools.)
Related: removing includes that aren't actually necessary.
FYI, I just did a major release of
include-tidythat reports unnecessary include files.How do you plan on implementing your program? I used the C API of Libclang since (a) it's intended to be a stable API (unlike the C++ API) and (b) I wanted to write
include-tidyin C, not C++.FYI in case you didn't know, SQLite does the same type of amalgamation for their source files for distribution. You might check out what they do (though I expect they use home-grown tools).
1
u/thradams Sep 18 '26
There is one problem that an amalgamator does not solve?
It works perfectly for small, self-contained libraries. The problem appears with larger libraries. For example, an HTTP server library may depend on many smaller libraries, such as JSON, URL parsing, and others.
Having a single amalgamated file for the HTTP server library makes it much easier to use.
However, the problem is that a lot of code may be duplicated because the user's application may already depend on the same JSON, URL, or other libraries for different reasons. Then the same json functionality is added everywhere for each lib.
The solution is this case, would be a packet manager to find all the dependency libs that can be each one amalgamated.
So, I think one alternative is also amalgamated full, or in parts.
1
u/veloxVolpes Sep 18 '26
Sorry, can I specifically ask what the goal is beyond what the -E flag can do?
2
u/smtp_pro Sep 18 '26
I usually just write a script and write my source files carefully
2
u/Cats_and_Shit Sep 18 '26
I don't think it's possible to do this perfectly, there are some things that can't effectively be transformed in this way. Two static functions with the same name, for example.
So I would want to see good error handing for those cases. The tool should fail, and should provide a useful error message that points to where things went wrong.
1
u/timrprobocom Sep 18 '26
This seems to me the antithesis of good practice. More difficult maintenance, less opportunity for reuse