News for hackers. Stuff that matters.
Re:Nuklear: A single-header ANSI C GUI library by zevv on Saturday February 10, 2018 @11:07AM (#16347856)
I really do not understand why 'single header' is considered a good thing, but I see this more and more often on libraries. What is the reason all the code is put in the header file?
Re:Nuklear: A single-header ANSI C GUI library by svdree on Saturday February 10, 2018 @12:02PM (#16348153)
Sean Barrett (who I think popularized the idea) has a FAQ on this (https://github.com/nothings/stb) where he justifies it by pointing at difficulties with deploying libraries on Windows. Which is a fair point, but by going straight to header-only he skips the step where you can also just distribute a bunch of headers and .C files. The convenience of only having to include a single header is nice for quick weekend projects, but for anything bigger you're dealing with dependencies and build issues anyway.

I get some of the reasons that you would initially start out with a header-only implementation, but when your library grows, you probably want to split it at some point. For me personally, that point would be some time before the header reached 25k (!!) lines.

Re:Nuklear: A single-header ANSI C GUI library by flohofwoe on Saturday February 10, 2018 @12:22PM (#16348291)
Some advantages of header-only vs .h/.c pair:

- you can build simple tools contained in a single .c file, and you dont't need a build system for this (e.g. just call "cc tool.c -o tool" instead of mucking around with Makefiles or cmake)

- the library can be configured with macros before including the implementation (e.g. provide your own malloc, assert, etc...), with the implementation in a .c file, these config macros must be provided via command line args, which implies a build system

- you can put the implementations for all header-only libs used in a project into a single .c file instead of compiling the multiple .c implementation files, this might speed up compilation

Single-header libs have some of the advantages that a proper module system would bring to C and C++ (e.g. extremely simple integration into own project and increased compile speed), but without all the complexity of implanting a module system into C or C++.

Re:Nuklear: A single-header ANSI C GUI library by badsectoracula on Saturday February 10, 2018 @01:05PM (#16348563)
For #1: you already have multiple files in your source so nothing stops you from including the .c file if you feel like compiling a single c file from the command line without a build tool is needed (although when it comes to make usually you can have a single generic makefile that does the same job)

For #3: the #1 applies here too (although beware for static stuff conflicts) but in practice C code compiles fast enough for this to not be a problem

I can see #2 being an advantage, but TBH i think the case where you both need a custom malloc, assert, etc and not need a build tool where you can pass the configuration macros is kinda rare.

Re:Nuklear: A single-header ANSI C GUI library by rixed on Saturday February 10, 2018 @06:51PM (#16350323)
Make is so simple that it can actually build you single file project without a makefile with less characters to type:

make tool

Et voilà.

Re:Nuklear: A single-header ANSI C GUI library by rlabrecque on Saturday February 10, 2018 @09:45PM (#16350898)
Errr, I actually have to spend 5 minutes googling how to install make on Windows, then getting mingw and hating myself. So no, no it's really not that simple in general.
Re:Nuklear: A single-header ANSI C GUI library by to3m on Saturday February 10, 2018 @10:38PM (#16351042)
https://github.com/tom-seddon/b2/blob/master/snmake.exe - standalone build of GNU Make 3.80. Has a couple of hacks in it to better support Windows-style paths with colons in it. I think this came from the SN Systems Playstation2 SDK. Thanks to the GPL, you can have it too.

(I have no idea whether this will successfully run a C compiler for you on Windows, though. I mostly use it to run Makefiles with phony targets as a cross-platform replacement for shell scripts.)

Re:Nuklear: A single-header ANSI C GUI library by angelsl on Saturday February 10, 2018 @11:14PM (#16351141)
Oh no, Make 3.80...

Why not build a more recent version? This thing is 15 years old!

Re:Nuklear: A single-header ANSI C GUI library by kqr on Sunday February 11, 2018 @06:05AM (#16352178)
This sounds like something one should ask people who do not write portable makefiles, rather than porters of Make, which has been standardised for quite a while now.
Re:Nuklear: A single-header ANSI C GUI library by to3m on Sunday February 11, 2018 @06:08PM (#16355358)
They added some useful stuff in subsequent versions... my recollection (though this is going back a while now) is that even only 3.81 was a worthwhile upgrade over 3.80!

However I have better things to do than try to figure out how to build GNUstuff myself on Windows.

Re:Nuklear: A single-header ANSI C GUI library by to3m on Saturday February 10, 2018 @11:48PM (#16351253)
Good idea! Let me know when you’ve done it... I could do with an updated copy.
Re:Nuklear: A single-header ANSI C GUI library by dTal on Sunday February 11, 2018 @07:13AM (#16352360)
Not really fair to blame make for your daft OS.
Re:Nuklear: A single-header ANSI C GUI library by MaulingMonkey on Sunday February 11, 2018 @02:03PM (#16354159)
1) A new dependency to install on every build server and dev's machine unless you're a 'nix only shop (e.g. not in gamedev) and sometimes even then (yes, I've had to apt-get install make) - at the very least this is a boatload of new setup/install instructions, in practice this also involves coordinating with IT, and heaven help you if you let your less programmery coworkers build from source too.

2) Which version of make? I have detailed instructions at work about which make to use with which makefiles such that all the stars align and the OS vars and other gnu tools line up such that our upstream Makefiles work without modifications (well, usually.)

3) Given the complexity of 2, what I'm actually going to do is automatically invoke make from whatever build system we're using internally.

4) Now that I have two sets of build configuration, I have a continual maintenance burden as conflicting dependencies and incompatible build settings need to be resolved. Since I deal with C++ libs and tools, sneezing too hard will cause incompatible libs. In practice, the Makefile will hardcode CCFLAGs, sometimes CC itself, and things like building with/without RTTI will cause incompatible libs I can't link without more Makefile tweaks.

5) When I get sufficiently fed up with the state of affairs in point 4, I'll integrate the tool into our own build system such that we have a single unified build system again and I don't have to make the same change in 5 places (our codebase + a measly 4 dependencies in this example.)

At this point I'm no longer using make and wondering why I didn't just skip directly to step 5 in the first place. Make is simple - so simple it doesn't address my needs nor solve my problems.

Re:Nuklear: A single-header ANSI C GUI library by TeMPOraL on Saturday February 10, 2018 @12:19PM (#16348270)
> he justifies it by pointing at difficulties with deploying libraries on Windows. Which is a fair point, but by going straight to header-only he skips the step where you can also just distribute a bunch of headers and .C files

That part was justified by deploying libraries for Windows. Going for one file only was justified by this:

"You don't need to zip or tar the files up, you don't have to remember to attach two files, etc."

--

Unrelated, and probably colored by the fact I first learned to program on Windows, but I don't get the problem. Windows applications usually bundle DLLs with them and keep them locally, unlike Linux applications which typically install dependencies globally through a package manager. I don't think I've ever had a big DLL problem developing on Windows, whereas on Linux I've been occasionally bit by the "oh this software requires X <= v2.1 but you can't have that since something else is already using X v2.3 and that would be downgrade".

Re:Nuklear: A single-header ANSI C GUI library by v_lisivka on Saturday February 10, 2018 @05:11PM (#16349937)
For some reason, problems with DLL libraries are called as "DLL hell", not "SO hell". ;-)

If your software needs library foo.so.x while other application needs library foo.so.y, just put both into /usr/lib. Problem solved.

Re:Nuklear: A single-header ANSI C GUI library by pjmlp on Sunday February 11, 2018 @03:26AM (#16351771)
Easy, Windows already had dynamic loading on Windows 3.x, which was the same model used by OS/2, while UNIX was still trying to figure out how to implemente dynamic libraries.

The first versions of dynamic linking on UNIX basically required patching a.out files, before ELF was designed.

So of course the expressetion regarding compatibily issues with dynamic libraries came to be "DLL hell", there weren't .so to talk about.

Re:Nuklear: A single-header ANSI C GUI library by chubot on Saturday February 10, 2018 @12:12PM (#16348215)
Thanks for the link.

Why not two files, one a header and one an implementation? The difference between 10 files and 9 files is not a big deal, but the difference between 2 files and 1 file is a big deal. You don't need to zip or tar the files up, you don't have to remember to attach two files, etc.

I'm still not convinced. I am convinced about a .c and .h -- that's how sqlite does it. Going to just a .h seems to provide negligible benefit and confuses the implementation and interface. It probably confuses a lot of tools too, e.g. source navigation tools, code coverage, code instrumentation, etc.

Re:Nuklear: A single-header ANSI C GUI library by gameswithgo on Saturday February 10, 2018 @08:04PM (#16350558)
I don't understand why anyone cares. you grab the .h file and call "load a jpeg"/"draw a button" or you grab two files and call "load a jpeg"/"draw a button"

Are we bikeshedding about this?

Re:Nuklear: A single-header ANSI C GUI library by Koshkin on Saturday February 10, 2018 @09:56PM (#16350936)
This is a matter of adhering to a sound engineering principle, and the approach in question has not been generally considered acceptable. To explain why, one of the ideas behind header files was that they could be freely reused in more than one part of a project; therefore, for example, any executable code appearing in a header file might end up existing in multiple copies throughout the executable (perhaps, depending on the linker).
Re:Nuklear: A single-header ANSI C GUI library by jcelerier on Sunday February 11, 2018 @01:30PM (#16353960)
> any executable code appearing in a header file might end up existing in multiple copies throughout the executable

that's why the keyword "inline" exists.

Re:Nuklear: A single-header ANSI C GUI library by Koshkin on Sunday February 11, 2018 @02:22PM (#16354244)
'Inline' does not prevent duplication of the generated code (in fact, it forces it).
Re:Nuklear: A single-header ANSI C GUI library by dllthomas on Sunday February 11, 2018 @03:05PM (#16354461)
... mostly. Except in the case where the inlined version can be optimized away, which is the best time to use inline but not entirely germane.
Re:Nuklear: A single-header ANSI C GUI library by flohofwoe on Saturday February 10, 2018 @12:25PM (#16348311)
> and confuses the implementation and interface

Normally you have the implementation inside an "#ifdef IMPLEMENTATION" block, and the API interface (public structs and functions) outside of the implementation block, and all private functions inside the implementation block are defined as 'static' so they are not visible outside the special implementation source file. In the places where you include the header for normal use, only the public interface is accessible.

Re:Nuklear: A single-header ANSI C GUI library by ocornut on Sunday February 11, 2018 @11:41AM (#16353361)
I'm also not convinced by the value of single file especially for semi large libraries like Nuklear. Especially as you could #include the .c file just as well as the .h files if you need. That's as many lines as #define IMPLEMENTATION.

Nuklear was developed as multiple files and hastily merged at the last minute by its author before being tagged 1.0. I think it was a mistake especially as he copied the entire stb_xxx files inside. And unlike libraries like stb_image.h, if you want to use Nuklear you need to setup or copy a non-trivial backend anyway, so it's not a matter of include and ready-to-use.

(Dear ImGui which Nuklear is based on is 7 files including 4 headers as you know.)

To me the core value of those libs is that they are highly portable, designed to compile everywhere without any specific build system, and designed to be compiled from source code. Because they are designed as such, problems (such as error/warnings on some setup) are caught easily and fast.

Whereas for bigger libraries you either get the headache of binaries, either get the headache of figuring out their build systems and building from source which frequently fails. And as people don't frequently build themselves, portability problems aren't caught as often.

Re:Nuklear: A single-header ANSI C GUI library by chubot on Saturday February 10, 2018 @12:43PM (#16348433)
OK I get that the big .h file is still logically separated into an .h and .c, like you say.

But what about preprocessing times? If you're including a library from many of your source files, then even if it always hits the #if 0 case, the preprocessor still has to parse the implementation. It matters for distributed compilation too -- more preprocessed bytes have to be sent over the network.

I'm sure there are cases where this overhead is negligible. But I'm just as sure there are some where it's not. Not caring about how much text is in your headers seems like a bad habit to get into. Build times are the main reason I don't use C and C++ more.

Re:Nuklear: A single-header ANSI C GUI library by kaslai on Saturday February 10, 2018 @03:39PM (#16349448)
The C preprocessor is the least of your worries with regard to compile times. Parsing a #if 0 can be done almost as quickly as a memcmp operation. Even my naive implementation can process an #if 0 at around 600 MBps. Even if you had a gigabyte of text in an #if 0, that's only about 2 more seconds on the compile, provided your disk can manage the throughput.
Re:Nuklear: A single-header ANSI C GUI library by moefh on Saturday February 10, 2018 @04:06PM (#16349590)
> Even if you had a gigabyte of text in an #if 0, that's only about 2 more seconds on the compile

Are you sure your implementation is correct? How does it handle this:

    #if 0
    "\
    #endif \
    "
    #endif
I'm not saying gcc's and clang's preprocessors are not really fast, but preprocessing is trickier than most people expect. In particular (as you can see from my example), while skipping over an "#if 0" you still have to split the source into tokens and discard the tokens.
Re:Nuklear: A single-header ANSI C GUI library by chubot on Saturday February 10, 2018 @06:00PM (#16350132)
Absolutely, great example! I have looked at quite a few implementations of preprocessors, and they're not simple.

I don't trust anyone who thinks it's simple without actually having implemented it -- and tested it on real code.

One older thread: https://news.ycombinator.com/item?id=10945552

Re:Nuklear: A single-header ANSI C GUI library by a_t48 on Sunday February 11, 2018 @04:13AM (#16351894)
I agree with the sentiment, but it should be easy enough to smash line continuations together while you're searching for that #endif (famous last words)
Re:Nuklear: A single-header ANSI C GUI library by naivete on Saturday February 10, 2018 @06:35PM (#16350265)
Is your naive preprocessor implementation complete and standards compatible C99? Or just some bastardization of it? If not the 600 MBps bench is as good as useless.

What's the C preprocessor speed of GCC/Clang/MSVC? Any benches?

Re:Nuklear: A single-header ANSI C GUI library by jcelerier on Sunday February 11, 2018 @02:05PM (#16354167)
Some tests on my system: including everything at the root of /usr/include: (intel 4770HQ)

    $ for file in /usr/include/*.{h,hpp} ; do echo $file | awk '{ print "#include \"" $1 "\"" }' >> /tmp/test.cpp ; done
    $ (laborious step consisting in removing headers that don't play nice with others... in the end there are still more than 1425 base headers, which end up including most of Qt4 & Qt5, boost, etc)
    $ time g++ -E /tmp/test.cpp -fPIC -std=c++1z -I/usr/include/glib-2.0 -I/usr/include/qt -I/usr/include/glibmm-2.4 -I/usr/include/glib-2.0/glib/  -I/usr/lib/glib-2.0/include -I/usr/include/gdkmm-2.4 -I/usr/include/gdkmm-3.0 -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/atk-1.0 -I/usr/include/qt4/ -I/usr/include/qt/QtCore -I/tmp -I/usr/include/qt/QtWidgets -I/usr/include/qt/QtGui -I/usr/include/KDE -D_FILE_OFFSET_BITS=64 -DPACKAGE=1 -DPACKAGE_VERSION=1 -I /usr/include/raptor2 -w -I/usr/include/rasqal -I/usr/include/lirc/include -Wno-fatal-errors -I/usr/include/lirc -I/usr/include/gegl-0.3 -I/usr/include/libavcodec -I/usr/include/freetype2 -DPCRE2_CODE_UNIT_WIDTH=8 -I/usr/include/python3.6m -I/usr/include/libusb-1.0 > out.txt

    1,82s user 0,18s system 99% cpu 2,013 total
out.txt (the whole preprocessed output) is 710kloc and 23 megabytes.

With clang:

    0,72s user 0,08s system 91% cpu 0,875 total 

so I'd say that in general, preprocessing time is fairly negligible. A few template instantiations will take much more time to compile.
Re:Nuklear: A single-header ANSI C GUI library by chubot on Saturday February 10, 2018 @05:58PM (#16350124)
Well, you have to multiply it by the number of translation units.

Also, I like the point made in the sibling comment by moefh. Are you sure you don't need to tokenize?

Can I see your implementation? I have looked at a few implementations of C preprocessors, and they're not simple.

Re:Nuklear: A single-header ANSI C GUI library by kzrdude on Saturday February 10, 2018 @11:13AM (#16347888)
because dependency management and build systems are hard to get right and not standardized. It's a simple distribution model that works everywhere.
Re:Nuklear: A single-header ANSI C GUI library by badsectoracula on Saturday February 10, 2018 @11:55AM (#16348106)
Yes, but why a single header file and not a pair of .c and .h files (personally this is what i do for my small libs)? With a single header file you'd need the user to put it in a specially designated "this is the implementation" .c file anyway.

I can see it for C++ which supports placing code in a header as a language, but C requires jumping over awkward preprocessor hoops that can be avoided by using a .c file.

Re:Nuklear: A single-header ANSI C GUI library by c-smile on Saturday February 10, 2018 @12:27PM (#16348324)
With .h only files, when you need to include needed functionality, you modify only your own source file.

But with .c files, when you have multiplatform project, you need to put it in other "3rd party" tools. Think about the whole zoo: Visual Studio, XCode, Code::Blocks, make, NMake, etc.

Re:Nuklear: A single-header ANSI C GUI library by loup-vaillant on Saturday February 10, 2018 @04:31PM (#16349723)
> But with .c files, when you have multiplatform project, you need to put it in other "3rd party" tools.

No you don't: just act as if you've written the damn thing yourself. Your tools don't have to know. How being multiplatform changes anything?

Re:Nuklear: A single-header ANSI C GUI library by gmueckl on Sunday February 11, 2018 @03:29AM (#16351776)
It changes everything. Each platform brings its own preferred build system with it, which needs a copy of a definitive and explicit lost of all source files to compile the project. This is cumbersome to maintain. Header files on the other hand are licked up be the compiler automatically after seeing the include statement in the source. So this is actually much less time consuming to maintain.

Also, it shows that building C and C++ is a sad affair.

Re:Nuklear: A single-header ANSI C GUI library by badsectoracula on Saturday February 10, 2018 @12:57PM (#16348513)
Why you need to do that with .c files? You can just drop the .c/.h pair into the same place as you would put the single .h file and the rest of your C/C++ files and use it like that.
Re:Nuklear: A single-header ANSI C GUI library by c-smile on Saturday February 10, 2018 @10:52PM (#16351081)
Let's say you need zip lib functionality in your project. If it will be available as set of .h files you would include it by one line `#include zip.h` in your .c file.

But libzip is made of .c files. How you would include it in your multiplatform project ? Visual Studio, XCode, etc...

In general it would be great if C has `#include source "some.c"` feature.

So you would add single file zip-lib.c that will contain `#include source ...` references to all needed .c files.

But C/C++ has no modules as other languages so we need palliatives like make files, etc.

Re:Nuklear: A single-header ANSI C GUI library by badsectoracula on Saturday February 10, 2018 @11:56PM (#16351270)
I am not comparing many-c-files (like your libzip example) with single header, i am comparing the single header with a pair of c/h files.
Re:Nuklear: A single-header ANSI C GUI library by pjmlp on Sunday February 11, 2018 @03:28AM (#16351773)
Easy, one links to the binary library.

It really feels strange that young devs find it so hard to use a linker.

Re:Nuklear: A single-header ANSI C GUI library by yoklov on Sunday February 11, 2018 @06:07AM (#16352186)
It feels strange that you're unfamiliar with the headaches that brings for cross platform projects.
Re:Nuklear: A single-header ANSI C GUI library by pjmlp on Sunday February 11, 2018 @06:39AM (#16352279)
Those headers have code that most likely require linking with other libraries, many of each are not available in source code that one can dump into an header file.

So it appears like a little convenience just for not having to deal with installing binary libraries, at the expense of wasting everyone's time.

The time I waste installing a binary library?

Once per platform for the longevity of the project.

The time I waste building software with header only library?

Every single time I do make all.

Re:Nuklear: A single-header ANSI C GUI library by ocornut on Sunday February 11, 2018 @11:34AM (#16353326)
Dealing with pre-compiled libs in the Visual Studio ecosystem is more complicated that you make it sound like.

For a start there are 4 incompatible standard libraries (dll vs static, debug vs optimized) and that's already a big headache. Also to a mix, a bunch of Visual Studio versions with enough difference to make it frequent that one compiled for e.g. 2010 doesn't work with 2015 or vice-versa.

To me the nature of those small libraries (regardless of the number of source files) is that they are highly portable and designed to always be compiled from source code to prevent those issues.

Re:Nuklear: A single-header ANSI C GUI library by merlish on Sunday February 11, 2018 @09:18AM (#16352764)
The point of single-file header libraries is that generally they are relatively small, and do not rely on linking with other libraries (outside the C runtime).
Re:Nuklear: A single-header ANSI C GUI library by pjmlp on Sunday February 11, 2018 @10:27AM (#16353031)
That might be the point, but it doesn't mirror many use cases.
Re:Nuklear: A single-header ANSI C GUI library by satysin on Saturday February 10, 2018 @02:53PM (#16349210)
You are free to split it into .h and .c if you want, every single header library I have used has an IMPLEMENTATION block to make doing so very simple.
Re:Nuklear: A single-header ANSI C GUI library by IshKebab on Sunday February 11, 2018 @06:26AM (#16352248)
Header only means you don't have to modify your build system at all, and C++ build systems nearly all suck. A lot. (Only exceptions I've found are Meson and QBS.)

Though I agree a single header and single .c/cpp file is another good option and solves the compile time issues of throwing everything in the header.

Re:Nuklear: A single-header ANSI C GUI library by FRex on Saturday February 10, 2018 @05:34PM (#16350035)
Some libraries do go with a pair because they find the single header too spartan or awkward. Either way works, the C preprocessor and compiler should chew through either style effortlessly and it's trivial to spit the header into a pair or merge a pair into a header by hand if you wish.
Re:Nuklear: A single-header ANSI C GUI library by kilpikaarna on Saturday February 10, 2018 @11:42AM (#16348030)
Especially when that single header is over 13k lines and reimplements half the standard library. This looks pretty cool and I have an idea for an OpenGL project that needs a GUI, so I'll probably try it out at some point, but it doesn't really seem as lean and straightforward as the blurb implies...
Re:Nuklear: A single-header ANSI C GUI library by naivete on Saturday February 10, 2018 @07:04PM (#16350368)
There isn't really that much unnecessary stuff in there. It's more like 20k LoC but half of it is comments. And if included as a header most of it will get stripped away by the preprocessor.

Just for context, in C++ including std::string (after preprocessing) will pull in 25k LoC, std::vector around 20k.

Programming in C++ by the book will average around 35k LoC per every compilation unit. And it will compile significantly slower than C code.

Having that in mind, and considering that Nuklear doesn't pull in any dependencies, not even C stdlib parts. I think author has done a good job at being lean and fairly straightforward.

UI libs are usually a huge dependency and a hairy mess. This is not really the case. It's just one ~1mb 20k line header file.

There is some overhead on preprocessing over having a .c/.h combo, but that's it.

Re:Nuklear: A single-header ANSI C GUI library by dangerbird2 on Saturday February 10, 2018 @12:04PM (#16348162)
Reimplementing parts of the standard library is important for applications where you don't neccessarily have access to a standards-compliant libc (read Windows), or if you have need custom malloc/free implementations. Similar techniques are used in automake and similar build generators to provide non-standard libc function implementations. precompiled headers can help with the fact that good portions of the header will be #ifdef'ed away.
Re:Nuklear: A single-header ANSI C GUI library by asveikau on Saturday February 10, 2018 @12:17PM (#16348253)
I think you completely missed the point of what you're replying to.

You can put those reimplentations in a different file. You don't have to expose the consumer of the library to them by jamming the whole thing, both interface and implementation, in a single header.

This is like library design by the folks who mistakenly say that what they do with '#include <stdio.h>' is they are "including a library". Eg. Compile and link are the same thing in a lot of people's minds, I think increasingly these days as C expertise is diminishing.

Re:Nuklear: A single-header ANSI C GUI library by pjmlp on Sunday February 11, 2018 @03:30AM (#16351779)
Yeah it looks like they are afraid of touching a linker.
Re:Nuklear: A single-header ANSI C GUI library by banachtarski on Saturday February 10, 2018 @01:16PM (#16348620)
I develop on linux primarily but occasionally windows also. The stdlib is totally fine! When did you last compile on Windows? 2011?
Re:Nuklear: A single-header ANSI C GUI library by pjmlp on Monday February 12, 2018 @03:28AM (#16357314)
All Windows compilers have ANSI C compliant libraries.
Re:Nuklear: A single-header ANSI C GUI library by FraKtus on Sunday February 11, 2018 @04:17AM (#16351908)
once you grasp the concept of immediate mode it is straight forward. You need to parse system events and feed them to Nuklear, then you make a single function that will draw your interface and update the application state variables you need. I like a lot the fact that within just a few lines of code I can have a GUI and focus on the main work...
Re:Nuklear: A single-header ANSI C GUI library by sam0x17 on Saturday February 10, 2018 @12:40PM (#16348410)
I've actually seen some projects where it's organized into a bunch of files, but then they export/"compile" to a single header file which is what you include in your project.
Re:Nuklear: A single-header ANSI C GUI library by printf_kek0 on Saturday February 10, 2018 @11:20AM (#16347917)
I would also like an explanation. The author(s) of Musl (libc alternative) mention on this page that it can potentially invoke undefined behavior: https://wiki.musl-libc.org/alternatives.html

However, having used a few of these "single-header" libraries my main concern is navigating a 9000 sloc header file as opposed to a neatly re-factored version...

An explanation of how undefined behaviour is possible would be welcome.

Re:Nuklear: A single-header ANSI C GUI library by CamperBob2 on Saturday February 10, 2018 @09:25PM (#16350822)
An explanation of how undefined behaviour is possible would be welcome.

Short summary: compilers that enable strict aliasing assumptions by default can introduce bugs into otherwise-working code by assuming that a pointer to one type will never refer to a variable of another type. This assumption is perilous but considered worthwhile by many, since it allows the compiler to take advantage of early-out optimizations and CPU pipeline scheduling in ways that would otherwise be unavailable at compile time.

More specifically, compilers may make different decisions about the appropriateness of aliasing optimizations depending on the information they have available. If a function that accepts potentially-aliased pointers exists in its own C file, its visibility to the compiler is limited to what the linker can see. So the compiler may not be as aggressive about its aliasing-safety assumptions as it would be if the function and all of its callers were all present in the same file. Under these conditions, a single-header library can result in "riskier" optimizations that the compiler wouldn't attempt if the same code resided in its own translation unit.

My understanding of Sean's position is that this breaches an implicit contract between the programmer and the compiler of a systems-level language. I agree, and I think the standard should have included a keyword -- or the compiler authors a flag -- to allow people to opt in to these sorts of optimizations rather than requiring them to opt out. It is way too late to change the way C works by default by doing stuff like this.

This is an oversimplification but I think it's what the Musl author is getting at. My guess is that if he knew Sean, he'd be a lot slower to accuse him of being ignorant of any particular aspect of what he's doing. Still, while his dismissal amounts to FUD in the absence of any specific examples of "undefined behavior," there are some good points on both sides of the argument.

My own take, which is unfortunately all too easy to back up with specific historical examples, is that it's inappropriate to do anything that makes C/C++ programming more difficult, more error-prone, or less secure than it already is.

Re:Nuklear: A single-header ANSI C GUI library by printf_kek0 on Sunday February 11, 2018 @04:07AM (#16351875)
I agree with the last part; should ask the Musl author(s) to substantiate this assertion.

> [...] the standard should have included a keyword -- or the compiler authors a flag -- to allow people to opt in to these sorts of optimizations rather than requiring them to opt out.

As an amateur Linux kernel hacker, I see hacks in the kernel that circumvent compiler bugs and unexpected behavior because of compiler defiance of standards. The rants on lkml seem to assign most of blame to the compiler authors of gcc. Here is one of Linus' (many) denunciations of gcc:

https://lkml.org/lkml/2003/2/26/158

But also Andrew T: https://stackoverflow.com/a/2771041 who claims - if I understand correctly - that strict-aliasing was already part of the C89/C90 standard but that compiler authors didn't implement the standard correctly.

> It is way too late to change the way C works by default by doing stuff like this.

One thing that I am confused about in your explanation is this:

> Under these conditions, a single-header library can result in "riskier" optimizations that the compiler wouldn't attempt if the same code resided in its own translation unit.

How exactly does a compiler generate "riskier" optimizations from a single-header as opposed to separate translation units? I fail to understand how after the pre-processing phase, this would be less safe.

Re:Nuklear: A single-header ANSI C GUI library by CamperBob2 on Sunday February 11, 2018 @04:10AM (#16351884)
How exactly does a compiler generate "riskier" optimizations from a single-header as opposed to separate translation units? I fail to understand how after the pre-processing phase, this would be less safe.

For instance, the optimizer might conclude that it's safe to either elide an inline function call or compile it very differently if it sees that you're referring to the same object in two separate parameters.

If the function is implemented in a separate file, the optimizer has to assume that the function does something it doesn't know about.

Re:Nuklear: A single-header ANSI C GUI library by printf_kek0 on Sunday February 11, 2018 @04:19AM (#16351919)
Thank you. Please indulge me if you have time with two more questions:

1. In some libraries (SQLLite for instance, and libev too I think) the authors have a script that "amalgamates" all sources into a single translation unit.Their reasoning being that a compiler with full-visibility of the source can do global / interprodecural optimization that would not be possible otherwise. Is there any sense in this if it practical to do so for a small to moderate size library?

2. Please tell me what I should read so I can reach the same level of understanding that you have. <not a question>

Re:Nuklear: A single-header ANSI C GUI library by CamperBob2 on Sunday February 11, 2018 @06:31AM (#16352258)
1. It's almost certain that the speed increase you'd get from intentionally merging a lot of source files into one is less than what you could achieve with a more intelligent profile-based refactoring approach. I wouldn't have a very high opinion of the approach you describe, not knowing any more about those libraries or their authors' motivations.

2. Read a lot of C code written by people like Sean Barrett. You would just pick up a lot of bad habits from mine. :)

Re:Nuklear: A single-header ANSI C GUI library by to3m on Saturday February 10, 2018 @01:01PM (#16348539)
Perhaps the musl author is referring to Sean Barrett's comments about strict aliasing on his web page: http://nothings.org/

Fun rant by the owner of the company Sean Barrett works for: http://web.archive.org/web/20160309163927/http://robertoconc... (and while we're on the subject, additional UB links: https://news.ycombinator.com/item?id=14170585)

As for how you navigate these files, you use a text editor that understands that you're working with code and not just a list of chars. Example post about this from last time Nuklear was discussed: https://news.ycombinator.com/item?id=11532001

Personally, for Visual Studio I use DPack (http://www.usysware.com/dpack/CodeBrowser.aspx); in Xcode, I use its methods dropdown (Ctrl+6); in VSCode, I use its methods dropdown (Ctrl+2); and in Emacs I use a thing I wrote ages ago that grabs the imenu names list and presents it in an ido menu in the minibuffer. And there are other options for other editors. The key thing is just to unshackle yourself from PgUp/PgDn/Find/mouse wheel.

Re:Nuklear: A single-header ANSI C GUI library by CamperBob2 on Saturday February 10, 2018 @08:43PM (#16350693)
I think the real reason for this stance is that it imposes a sense of discipline that wouldn't otherwise be there. When you write a library like this one, you're always tempted to overengineer it... maybe it needs to render .PDFs (or render to them)? Wouldn't it be nice if it supported various OS clipboards? How about supporting both OpenGL and DirectWhatever and that homebrew software renderer that you wrote a few years ago? And wow, it sucks to build complex interfaces by aggregating C function calls. Lua support would only take a few more KLOCs, right? ...

And before you know it, you're reinvented Qt. NTTAWWT, I guess, but if you set out to build a lightweight set of GUI controls for a specific purpose, you probably had a different goal in mind, and that's not the way to accomplish it.

In my own work I frequently include stb libraries in separate .c(pp) files that provide a somewhat higher-level C or C++ interface to the features I need, which are then linked in the traditional way with .obj files and minimal headers. This approach works well, maintaining encapsulation and keeping compile times down while avoiding dragging in a ton of random dependencies.

Re:Nuklear: A single-header ANSI C GUI library by JepZ on Sunday February 11, 2018 @06:28AM (#16352255)
As Ken Thompson [1] (the one from Ken Thompson and Dennis Ritchie (Unix & C)) was one of the original authors of Go, I think it is safe to use the Golang Origins FAQ [2] as a source to answer this question.

> Dependency management is a big part of software development today but the “header files” of languages in the C tradition are antithetical to clean dependency analysis—and fast compilation.

That might be a reason to limit the use of header files to single file. In addition, I think that it is meant to make the adoption of the library easier.

[1]: https://en.wikipedia.org/wiki/Ken_Thompson [2]: https://golang.org/doc/faq#Origins

Re:Nuklear: A single-header ANSI C GUI library by sam0x17 on Saturday February 10, 2018 @12:39PM (#16348403)
It's really a godsend. include it and you're done.
Re:Nuklear: A single-header ANSI C GUI library by Koshkin on Saturday February 10, 2018 @10:06PM (#16350964)
... Include it in two files, and you are really done!
Re:Nuklear: A single-header ANSI C GUI library by sam0x17 on Monday February 12, 2018 @01:26AM (#16356946)
assuming proper guard macros are included in the header, this shouldn't be a problem
Re:Nuklear: A single-header ANSI C GUI library by wott on Saturday February 10, 2018 @04:01PM (#16349558)
I have mostly seen it done by (young) people who come from web dev. They bring their bad habits together with them. And they usually don't get a very warm welcome for that.

I mean, we've been nicely organising our sources in files, modules and libraries for 30 years, and that's been fine: very easy, logical and giving us many benefits (such as quick partial builds, splitting different operations, isolating problems for a much easier resolution, and so on) but suddenly they show up and it's too difficult for them and they prefer to dump thousands and thousands of lines in a single compilation unit and have all things interfere with each other...

Re:Nuklear: A single-header ANSI C GUI library by dang on Sunday February 11, 2018 @02:07PM (#16354175)
On HN, could you please not conduct arguments like this in the flamewar style?

Making this about "young" people who come from "web dev" is flamebait, which predictably takes the discussion into inner circles of hell. That's just what we don't want on HN.

You've done this kind of thing more than once before. That's bad. If you'd (re-)read https://news.ycombinator.com/newsguidelines.html and take the spirit of this site to heart—intellectual curiosity and thoughtful conversation—we'd appreciate it.

Re:Nuklear: A single-header ANSI C GUI library by _dcwr on Saturday February 10, 2018 @05:30PM (#16350016)
I have not heard this 'criticism' ever before and it's absolutely ridiculous on so many levels to me as a person tangentially interested in gamedev, C, C++, runtimes, system programming, retro stuff, history of gaemdev, etc.

1. Why would a web dev suddenly decide to write in such a spartan way an algorithm heavy library in C or C++? This makes no sense except to somehow tie modern web dev failures like left pad with this style of libraries to portray them in negative light...

2. In gamedev neat organization is/was not some golden long standing standard, i.e. in comments under[0] you can find information that with 3D graphics it was customary to use a single file to let the early compilers do a better job since they didn't optimize across translation units and many old codebases have no problem with thousands of lines of spaghetti per file.

3. These libraries are meant to be compiled once ever per project (except when you clean out all your object files) with a define in a single source file (which any modern compiler should handle), otherwise most of their text is thrown away in a single ifdef which should be trivial and speedy on a modern compiler. The resulting header shouldn't be a problem for any modern compiler and is as light or lighter than an entire include tree would be. Most of these libraries are also single purpose and have very small API and C is light compared to template heavy C++ anyway. If you develop such a library you can keep it split up and ship merged files, i.e. SQLite does that. If anything this might speed up the execution (optimization in single TU), compilation (a single TU for entire library) and linking (no dynamic nor static linking but a single object) but to me that tangential compared to convenience itself.

4. I've never seen such libraries to have been badly received - quite the contrary, SQLite might be the most widely deployed piece of software actually and it's recommended consumable form is a single pair of files (plus an ext file, for a total or 3 in the official 'amalgamation').

5. I've personally yet to see a 'young web dev' create such a library. If anything it's the grizzled veterans of gamedev, runtimes, compression, middelware, cross platformness, systems programming, people with university education that includes CS heavy stuff, etc.

Libraries I use or like that use this (single file or single header + source) style are:

- very popular PD libraries by Sean T Barret, thanked in this linked repo's README. He arguably popularized this style, maintains a list of such libraries and has a FAQ and a guide about this style of libraries. He is over 50 (or maybe over 60, he was a teen in the 80s), is a gamedev veteran with software and hardware graphics and runtimes - Thief 1, Iggy (Flash runtime) at RAD, etc. His website is also hardly something that a modern web dev would make[1].

- pugixml by Arseny Kapoulkine - gamedev veteran: PS3, physics, rendering, etc. not a web dev, not young (definitely over 30, unless you happen to redefine young to 'under 40' which makes 0 sense in industry as young as ours but considering your other claims is totally likely).

- SQLite which is shipped and recommended to be used like that (and I think Python embedds it like that) - D. Richard Hipp, Tcl, over 50 now, total veteran of world class between Fossil, Tcl, SQLite, etc., educated and programming since 90s, his personal website is also hardly modern web dev.

- ClipperLib by Angus Johnson, a hardcore clipping 2D library, it has single/two file versions in C#, Delphi and C++ (are these the languages young web devs know well now too?) - another person who was a programme rin the 90s and is from very Enterprise-y background (Delphi Object Pascal, yikes), not a web dev, nor with a web dev worthy web page[3] (I don't mind it, I even find it nice to look at, but it's another hint of the lack of supposed web dev blood in him).

Honorable mentions are the two people related to imgui concepts and also thanked in this linked repo's README:

- Casey Muratori - programmer in the 90s already, over 30 or 40 years old, another (along STB) RAD tools related person, worked on Granny (animation for games) and Bink (video compression for games), total veteran in low level stuff, not a young web dev.

- Omar Cornut - maker of Dear IMGUI, was an intern in the 90s, has a very non web dev website[4].

All in all I'm truly baffled about where you got the 'mostly young people who come from web dev' bit, especially since it's no secret many people are inspired to write them by STB himself (and mention that in their READMEs, as seen here) who is none of that. Your comment seems very hearsay, dishonest disingenuous.

If anything I feel like this is a set up of a Yerevan (communist, post communist, Eastern Europe, Eastern Bloc, [5], etc.) joke I happen to know in Polish (translation mine): "Dear Radio Yerevan, is it true they give out free cars on the Red Square in Moscow?" - "That's mostly true but it's bikes, not cars, they are stolen, not given out, and not in the Red Square in Moscow but in the Prague district of Warsaw".

[0] - http://fabiensanglard.net/duke3d/

[1] - http://nothings.org

[2] - http://www.hwaci.com/drh/

[3] - http://www.angusj.com/delphi/clipper.php

[4] - http://www.miracleworld.net/

[5] - https://en.wikipedia.org/wiki/Radio_Yerevan_jokes

Re:Nuklear: A single-header ANSI C GUI library by wott on Sunday February 11, 2018 @10:41AM (#16353104)
That's all fine and dandy. You can quote all the shiny names you want from your domain and write a novel about them as it seems you are in the mood for doing that, it will not change anything to what I witness on each C forum I follow: it is always:

1. young people

2. coming from Javascript

3. coming from the land of IDEs which blur the difference in nature between files, between external and internal files, between copy inside the project and inclusion/linking from outside, etc.

4. who take a try at C, because they have heard C is hardcore or something like that and they want to taste low-level to become real programmers, not just web devs

5. who offer or request single header libraries because they have no idea it could be otherwise, or because they think linking or writing a basic makefile is an issue an enormous amount of work, because they come from the land of language-dependant automatic package managers.

That's somehow the same horrible movement that ends up giving us projects which ship with all the dependencies inside the package. Well, not all dependencies, just almost all, because there will always be a random couple of libraries you'll have to install yourself, without knowing why... And then those 'internalised' libraries will never get patched, never get updated, never get fixed, that's wonderful.

End of story. I do not even know why, along your text wall, you go on fighting things you pretend I have said, when I haven't. I haven't spoken of this library in particular, I haven't spoken of gamedev (I would not touch that with a 10 feet pole), I have spoken of what I witnessed. And that's a true phenomenon that started only 2 or 3 years ago.

Re:Nuklear: A single-header ANSI C GUI library by _dcwr on Sunday February 11, 2018 @12:34PM (#16353631)
You have answered to:

> I really do not understand why 'single header' is considered a good thing, but I see this more and more often on libraries. What is the reason all the code is put in the header file?

With:

> I have mostly seen it done by (young) people who come from web dev. They bring their bad habits together with them. And they usually don't get a very warm welcome for that. (...)

And now you ask why I've listed all these libraries to prove otherwise? This is what I'm disproving with this list of old, experienced, non web devs writing good libraries that are well received and reliable.

You can't make a sweeping statement about an entire paradigm of writing libraries and then claim you just happened to ignore all of the facts that contradicted it like existence of SQLite, STB and the entire gamedev industry. Your logic is on the level of saying that "most nuclear power plants go boom" and claiming you just don't care about any except Chernobyl personally.

Where are your examples of these bad libraries made by young web devs that are not well received? Between few examples I gave you should have at least 10 by now but instead you tell me that some people learn C because it's 'hardcore' and that new C programmers struggle with C linking (both of which are obvious facts to any C or C++ programmer).

And there's nothing special with single file libraries about avoiding building or lack of updates, people can (and do) dump entire source trees into their projects to avoid having to build a separate library and it works just as well most of the time. People also do things like ship prebuilt binaries (especially C ones, which have a very stable ABI even on Windows, unlike C++) with their C or C++ project which is just as bad from the update standpoint as embedded source is or it's even worse since it tasks anyone trying to replace these with finding out how to build that library (which between CMake, premake, Scons, even changing compiler defaults - which once bit me when building a project with GCC - and God knows what else might be quite an adventure compared to just dumping the source into the project itself and rebuilding that).

Re:Nuklear: A single-header ANSI C GUI library by naivete on Saturday February 10, 2018 @07:13PM (#16350396)
What are you talking about, C doesn't have a concept of a module or a library in the first place.

Nothing about C is easy or logical. C only makes sense in 1965 on PDP7. All these hacks just work around the inherent shittyness of C/C++.

Re:Nuklear: A single-header ANSI C GUI library by wott on Sunday February 11, 2018 @10:45AM (#16353122)
Not sure why you felt necessary to create an account to post this message.

It is not funny and yet it is wrong on all accounts.

Do I even need to discuss that C doesn't have libraries???

And a module == a compilation unit.

Re:Nuklear: A single-header ANSI C GUI library by pjmlp on Sunday February 11, 2018 @03:21AM (#16351760)
Developers that don't want to bother how to properly use a linker.
Re:Nuklear: A single-header ANSI C GUI library by wott on Sunday February 11, 2018 @10:46AM (#16353130)
Apparently, passing "-lmylib" to a compiler/linker has become a terrible effort indeed.