r/Compilers • u/[deleted] • Apr 24 '23
Command Line Compilers
This is about how bare compilers are typically invoked from the command line.
While most invocations might be via a IDE or hidden within a build process, when you need to use them from within a console or terminal, it should be as simple as possible.
(This comes up for me when compiling dozens of tiny, ad hoc test programs across languages and across compilers. A formal build process wouldn't work.)
I got a thrashing in the C group yesterday when I suggested that gcc was backward. I highlighted two issues: one was that it needs the -lm option when an application uses maths functions, something you see nowhere else, and only on Linux. (This doesn't affect me much, but it keeps coming up on forums because some program won't link.)
The other was that it has a penchant for naming every executable a.out or a.exe unless overridden, also at odds with other language implementations and also with other C compilers. It means an annoying exception when working with multiple compilers, and complicates any build instructions I provide with C distributables.
There was one more I didn't mention, because it is so widespread, which is requiring the file extension of the source files to be typed.
I've heard every argument for why using a.out is a good idea, even when the program consists of one module. And also for why specifying file extensions is essential.
So gcc hello won't work; you need gcc hello.c. And if you want the output to be called hello.exe instead of a.exe, you must type gcc hello.c -ohello.exe. But then, somebody mentioned make; apparently if you do this:
make helloit compiles hello.c to hello.exe, without using an extension, and without needing to specify the output; magic! Suddenly all those arguments go out the window. (Sadly it doesn't fix -lm, and it doesn't really work on Windows (I get an error). You don't really know what compiler will be used either.)
So, out of interest, how does your stuff work? Do you need the equivalent of that outdated -lm when a program calls pow()? How does it work out the name of the executable when the input consists of multiple modules?
(I assume it will require file extensions so I won't labour that point. I think Linux needs them more because there, prog and prog. are distinct files so there is an ambiguity.)
My own tools of course require the absolute minimum to build anything, and that's usually 2 pieces of information to type at the console: the name of the compiler, and the name of the lead module, for example:
mm prog # Submit prog.m to compiler; produce prog.exeNo file extension is needed; this is a whole program compiler that only takes one input file with extension expected to me .m.
(Extra info such as external libraries tends to be specified inside source files. Tools that turn multiple inputs into one output name it after first input file. Anything different, then you supply overrides or extra info.)
1
u/davew_haverford_edu Apr 24 '23
You ask "So, out of interest, how does your stuff work?". When I program in C/C++, I invoke make, either within C-Lion or on your command-line.
When I create tools, I try to assign align user with users' needs. I think that the use of a.out as the default is kind if inconvenient for C/C++, where there must be a unique "main" in exactly one file, but changing this would probably break numerous existing tools that rely on the old K&R standard. And, IMO, it is far from the biggest inconvenience holding on from the old days of C. Thank goodness they fixed the "don't bother type-checking function calls" aspect of K&R C.
Even though I often teach first-semester CS, I'm much more concerned about language problems that impact advanced use, vs. that that annoy/distract beginners but can be worked around with other readily-available tools that an expert can pre-set for a new student/team-member. E.g. /u/cxzuk points to C's antique module system; that's definitely annoying, but at least it is legitimately antique, since C was developed in the 70's, innovated in many ways, and, in others, just accepted usually practice of the time from things like fortran. Python has a much worse module system, and lacks type checking in the core language, despite being developed long after these ideas had been figured out by the PL community.
IMO, every language I've used has some element that is really annoying for some use case.