Software Engineering
c naming project-structure
Updated Wed, 27 Jul 2022 12:58:48 GMT

C project avoiding naming conflicts


I'm struggling to find pragmatic real-world advice on function naming conventions for a medium sized C library project. My library project is separated into a few modules and submodules with their own headers, and loosely follows an OO style (all functions take a certain struct as first argument, no globals etc). It's laid our something like:

MyLib
  - Foo
    - foo.h
    - foo_internal.h
    - some_foo_action.c
    - another_foo_action.c
    - Baz
      - baz.h
      - some_baz_action.c
  - Bar
    - bar.h
    - bar_internal.h
    - some_bar_action.c

Generally the functions are far too big to (for example) stick some_foo_action and another_foo_action in one foo.c implementation file, make most functions static, and call it a day.

I can deal with stripping my internal ("module private") symbols when building the library to avoid conflicts for my users with their client programs, but the question is how to name symbols in my library? So far I've been doing:

struct MyLibFoo;
void MyLibFooSomeAction(MyLibFoo *foo, ...);
struct MyLibBar;
void MyLibBarAnAction(MyLibBar *bar, ...);
// Submodule
struct MyLibFooBaz;
void MyLibFooBazAnotherAction(MyLibFooBaz *baz, ...);

But I'm ending up with crazy long symbol names (much longer than the examples). If I don't prefix the names with a "fake namespace", modules' internal symbol names all clash.

Note: I don't care about camelcase/Pascal case etc, just the names themselves.




Solution

Prefixing (well, affixing) is really the only option. Some patterns you'll see are <library>_<name> (e.g., OpenGL, ObjC runtime), <module/class>_<name> (e.g. parts of Linux), <library>_<module/class>_<name> (e.g., GTK+). Your scheme is perfectly reasonable.

Long names aren't necessarily bad if they are predictable. The fact that you are ending up with crazy long names and functions that are too big to stick with related functions in a single source file raises different concerns. Do you have some more concrete examples?





Comments (5)

  • +1 – I see where you're coming from I have wondered whether I'm being too pedantic about splitting into separate files, but it helps a lot with readability, maintenance and git mergeing. As an example I have a module for drawing UI with OpenGL, and I have separate .c files for each element that I need (slider.c, indicator.c etc). These element implementations have a main drawing function maybe a few hundred lines long, and a fair number of static helpers within. They also call a few pure geometry functions from within the UI module. Does that sound fairly typical? — Jun 20, 2013 at 15:50  
  • +0 – A better example of the long names might be my audio module I have a hierarchy like Audio Module > Engines > Channels > Filters which means something like MyLibAudioEngines<EngineName>Channel<ActionName>. Or in my filters submodule: MyLibAudioFilters<FilterName><Type><Action> eg. MyLibAudioFiltersBigSoundingCompressorFloat32Process — Jun 20, 2013 at 15:58  
  • +0 – None of that seems unreasonable. A few hundred lines for a function seems a bit long but if what you are drawing is complicated, that can be difficult to avoid. Is it branch heavy or just a lot of instructions? — Jun 20, 2013 at 16:21  
  • +0 – Re: the audio module names, you could abbreviate AudioFilters/AudioEngines as I think it would be easy to tell if it's a filter or module based on the name. Datatype qualifiers like Float32 could also be abbreviated (e.g. 'd','f') as such abbreviations are common in C programming. — Jun 20, 2013 at 16:27  
  • +1 – Thanks for your answers sometimes it feels near impossible to get good info on C program architecture (especially compared to the higher level languages). Many C books I've read barely consider the idea of having multiple modules or even more than one file! Overall, I don't think I'll change much, except considering whether to live with abbreviations for some of the longer names. — Jun 20, 2013 at 17:10