5 ms·
The thing that was always interesting to me is that #include <filename> does not even need to include a _file_ - it could grab the needed definitions from a dat
by phone8675309 3y ago
The thing that was always interesting to me is that #include <filename> does not even need to include a _file_ - it could grab the needed definitions from a database or something else entirely. It's only the #include "filename" that looks for a source file according to the standard.
- sshine 3y agoTIL. I can’t be the only one who likes random facts like these. I’m sure 10-20% of my university experience is listening to people’s monologues of random facts outside of lectures.
- camel-cdr 3y agoConsider also what including a file means. Ok, so you read from a file on the filesystem, but what about custom filesystems? I played around with this idea a while ago and build a user space filesystem that executes the file names with the shell [0] (it needs a bit of a custom syntax to express characters that aren't allowed in filenames) $ ./execfs mnt # or make mount $ cat 'mnt/(uname)' Linux $ cat "mnt/unique text(seq 10 | awk '{c+=\$1}END{print(c)}')unique text" 55 $ fusermount -u mnt # or make umount This allows you to basically do anything you want at compiletime with any C compiler: $ cat examples.h char seed[8] = { #include "mnt/(head -c 8 /dev/urandom | xxd -i)" }; double pi_squared = #include "mnt/(python -c @;import math;print(math.pi * math.pi);@;)" ; char *kernel = #include "mnt/(uname -sr | sed @,s/^/@;/;s/$/@;/@,)" ; $ cpp -w -P examples.h char seed[8] = { 0xf9, 0x17, 0x93, 0x83, 0xf6, 0x90, 0xca, 0xa7 }; double pi_squared = 9.86960440109 ; char *kernel = "Linux 5.16.0-1-amd64" ; [0] http://github.com/camel-cdr/execfs http://github.com/camel-cdr/execfs
- smegsicle 3y agowhat a great idea this is just what i needed to simplify my build process
- phone8675309 3y agoThis is incredibly cool.
- rramadass 3y agoVery neat and innovative!
- yjftsjthsd-h 3y ago> #include <filename> does not even need to include a _file_ - it could grab the needed definitions from a database or something else entirely. Has anyone actually done that, or is this a purely hypothetical "the standard doesn't say you can't" like converting undefined behavior in C into replacing the program with tetris?
- rramadass 3y agohttps://news.ycombinator.com/item?id=37938444 https://news.ycombinator.com/item?id=37938444
- rramadass 3y ago>#include <filename> does not even need to include a _file_ One of the stackoverflow answers (from Adrian McCarthy) states this; The VAX/VMS C compiler kept all the C runtime library headers in a single textual library file (similar to a unix archive), and used the string between the < and > as the key to index into the library.