Live data from Hacker News

C Preprocessor Hell

jacquesmattheij.com

11–20 of 30 posts

Re: C Preprocessor Hell

#11
post #6

I don't know why people don't seem to think of this when confronted with the limitations of the standard C preprocessor, but Perl or Lisp both make excellent C preprocessors. You aren't required to only use the C source code transformation tools that a default install of GCC provides.

True but this also creates extra dependencies for any project doing this. If you need to be able to compile on various platforms and/or machines doing so can introduce unnecessary complications as a result.

Re: C Preprocessor Hell

#12
post #6

I don't know why people don't seem to think of this when confronted with the limitations of the standard C preprocessor, but Perl or Lisp both make excellent C preprocessors. You aren't required to only use the C source code transformation tools that a default install of GCC provides.

Using external programs to generate code nontrivially increases the required complexity of your build system, and unless you're targeting Linux only, it isn't really given that users will already have perl installed, much less a lisp interpreter.

nontrivially increases the required complexity of your build system.

No it doesn't. It only requires a single rule in your Makefile. Something like:

    %.c: %.cpre preprocess.pl; ./preprocess.pl $@

Re: C Preprocessor Hell

#13
post #12

Earlier quoted context omitted.

Using external programs to generate code nontrivially increases the required complexity of your build system, and unless you're targeting Linux only, it isn't really given that users will already have perl installed, much less a lisp interpreter.

nontrivially increases the required complexity of your build system. No it doesn't. It only requires a single rule in your Makefile. Something like: %.c: %.cpre preprocess.pl; ./preprocess.pl $@

As was already stated: it isn't really given that users will already have perl installed.

The usual workaround for such situations is to add the preprocessed files into source control as well, so they are available when a user is building the code. However, this ends up even more ugly.

Re: C Preprocessor Hell

#14
post #7

"Lisp programmers should stop reading right now because they'll likely suffer severe injury of the jaw muscles as they laugh themselves silly at how hard it is to do some things in C." Naive question: in Lisp, how would you set the byte at address 0xDEADBEEF to 0x42?

There are some who would suggest that allowing you to commit segment violations is not generally a desirable language feature.

Re: C Preprocessor Hell

#15
post #12

Earlier quoted context omitted.

Using external programs to generate code nontrivially increases the required complexity of your build system, and unless you're targeting Linux only, it isn't really given that users will already have perl installed, much less a lisp interpreter.

nontrivially increases the required complexity of your build system. No it doesn't. It only requires a single rule in your Makefile. Something like: %.c: %.cpre preprocess.pl; ./preprocess.pl $@

Which works great if you use makefiles, but for those using Visual Studio for build management, it can be a bit more tricky. First you have to make sure you have perl, then you have to make sure you've got the appropriate build rules in place, then you have to make sure things get built at the right time, etc. It really is nontrivial.

Re: C Preprocessor Hell

#16
post #7

"Lisp programmers should stop reading right now because they'll likely suffer severe injury of the jaw muscles as they laugh themselves silly at how hard it is to do some things in C." Naive question: in Lisp, how would you set the byte at address 0xDEADBEEF to 0x42?

There are some who would suggest that allowing you to commit segment violations is not generally a desirable language feature.

Except when 0xDEADBEEF happens to be the memory address overlaying the custom hardware registers, and setting it to 0x42 turns the blinky light on. There are some cases where manipulating static memory locations is not only a good thing, but the only way to do something (at least for embedded programming). Not saying that your point is invalid, btw.

Re: C Preprocessor Hell

#17
post #7

"Lisp programmers should stop reading right now because they'll likely suffer severe injury of the jaw muscles as they laugh themselves silly at how hard it is to do some things in C." Naive question: in Lisp, how would you set the byte at address 0xDEADBEEF to 0x42?

There are some who would suggest that allowing you to commit segment violations is not generally a desirable language feature.

The ability to commit segment violations is a consequence of the broader abilities you get with pointers as a language feature. In many cases, neither of those things are desirable, but there are cases (e.g., systems programming) where they are very desirable.

Re: C Preprocessor Hell

#18
post #9
post #6

I don't know why people don't seem to think of this when confronted with the limitations of the standard C preprocessor, but Perl or Lisp both make excellent C preprocessors. You aren't required to only use the C source code transformation tools that a default install of GCC provides.

Yep. Why try to force a tool into a role it was never designed for? You use m4 or just write a preprocessor in any language. If you really want to do complicated lisp-style meta-programming stuff with C, just write another C program to pre-process your C program. It's more work, but it gives you complete control over the syntax.

...or just use Python, Ruby, or Lisp directly?

Doing weird things to a C file (while occasionally useful) is just increasing the technical debt of your teammates later.

(though, we do have a closure-ish macro in our codebase that's pretty nifty, so do as I say not as I do etc.)

Re: C Preprocessor Hell

#19

Earlier quoted context omitted.

There are some who would suggest that allowing you to commit segment violations is not generally a desirable language feature.

The ability to commit segment violations is a consequence of the broader abilities you get with pointers as a language feature. In many cases, neither of those things are desirable, but there are cases (e.g., systems programming) where they are very desirable.

Sure, but LISP was never intended as a systems or embedded programming language. Criticizing LISP for not being good at doing low-level tasks is akin to criticizing Harley-Davidson's motorcycles because they don't float.

Re: C Preprocessor Hell

#20
post #13
post #12

Earlier quoted context omitted.

nontrivially increases the required complexity of your build system. No it doesn't. It only requires a single rule in your Makefile. Something like: %.c: %.cpre preprocess.pl; ./preprocess.pl $@

As was already stated: it isn't really given that users will already have perl installed . The usual workaround for such situations is to add the preprocessed files into source control as well, so they are available when a user is building the code. However, this ends up even more ugly.

None of that has to do with build system complexity.
Post reply on HN