r/C_Programming • • 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

  1. Follow #include (except system includes) and add each file only once.
  2. Preserve the original order.
  3. 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.

5 Upvotes

18 comments sorted by

7

u/josk613 Sep 18 '26

What's the use case?

1

u/thradams Sep 18 '26

A programmer is creating a library using multiple source files. Following the general objective of an amalgamator, they may want to publish a single header/source pair for users who want to use the library by including just one file.

The amalgamator can do this by merging all the library's files into a single source file. The public header still needs to be create manually.

Why is making functions static, for example, interesting?

Because it effectively puts the library's internal functions into a private namespace, preventing them from competing with symbols in the code that uses the library.

The same idea applies to namespace prefixes they will prevent name collisions between the library and the application using it. (also can be done manually for public interface)

1

u/thradams Sep 18 '26

It also interesting that the amalgamated version can have better diagnostics. For instance, non used static functions. Or if the static analysis is checking the implementation, then everything is visible for better checking. And it can have better inlining.

2

u/pjl1967 Sep 18 '26

Not only better inlining, but better optimization overall (a superset of inlining).

2

u/pjl1967 Sep 18 '26 edited Sep 18 '26

It sounds like an interesting project. (I tend to like software development tools.)

10. Deduplicating system include #include <stdio.h> just once.

Related: removing includes that aren't actually necessary.

FYI, I just did a major release of include-tidy that 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-tidy in 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

Yes, but I think removing unused include files is also useful in non-amalgamator scenarios. In that case, it could be a separate tool.

Initially, I was planning to implement it with a full parser. But then I realized that I wanted to work with inactive blocks and incomplete files, so I decided to use pattern matching for the more advanced transformations, like adding static or prefixes.

I think SQLite was the first, or one of the first, to use this name and concept.

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/thradams Sep 18 '26

-E will expand all the macros and will not preserve inactive blocks.

For example, if you have #ifdef _linux and run -E on Windows, all of the code inside _linux block will disappear. If you lib has constants #define LIB_CONST1 they will also disappears.

The objective of amalgamator, is to preserve all the code, but in one file.

1

u/veloxVolpes Sep 18 '26

Genuinely just trying to understand, so I'm sorry if I come off as anything else but could you give me an example?

I am trying to walk through it where the amalgamation would be more useful specifically for development than the source files or a distributed amalgamation that does require these values in tact? Like, for a library you need different binaries per platform already so the amalgamation preserving _linux on Windows is unnecessary.

Again, I mean this as mostly a reflection of my own understanding, and I'd like to learn

1

u/thradams Sep 18 '26
  • it avoids name collision

lets say your lib uses print for a function. any other print in the user program will conflict with your print unless you add prefix or make it static.

The amalgamator makes possible you develop you code, without prefixing internal functions, just the public ones. and more...

1

u/thradams Sep 18 '26

Yes. Then a simple tool with 500 lines can do the job.

I give my static functions unique names, because then I don't have conflicts when creating the amalgamated version with the simple tool.

The competitor to the perfect amalgamator is the dumb amalgamator, implemented in 500 lines of C.

So, the bigger project needs to provide some compensation for it size.

1

u/thradams Sep 18 '26

I have another tool that collects unit tests from the source code and builds a function that calls all the unit tests.

#ifdef TEST
void test1(){}
void test2(){}
#endif

It builds

extern void test1();
extern void test2();

void run_tests()
{
  test1();
  test2();
}

I am thinking about merging it into the same tool.

2

u/pjl1967 Sep 18 '26

Also, -E will expand system headers that, in this case, isn't what you want since you want only local headers expanded.

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