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.)
13
u/JMBourguet Apr 24 '23
I've some sympathy for your point of view, but I think it lacks historical understanding and I'm not sure when in the time-line of gcc development you want the design choices you prefer had been made. gcc has been designed on Unix, as a drop-in replacement for existing c compilers available on those systems. All the choices you complain about have been made before gcc development started.
Removing the need of -lm is probably the only one which could be made now with the less of impact. Although it can be argued that's not a decision of gcc but of the libc used on Linux and that the change has be be made there, g++ doesn't need it and gcc could easily provide it as well. The biggest issue would be for an important part of the market of gcc which is less important for g++: the embedded market where linking by default the math library could still be an inacceptable overhead, just as it was on the early Unix systems where the tradition started.
Changing the argument parsing brings out the backward compatibility issue, and the point that gcc has been designed from the start as a drop in replacement for platform specific C compiler (it even accepts arguments it never used because they weren't meaningful in its context to be such a drop-in replacement), just as clang interface is a drop-in replacement for gcc (and the clang project has, IIRC, another driver which is a drop-in replacement for Microsoft C++ compiler).
Your point on extension forgets that gcc is not a C compiler. It is a driver which dispatches source to the appropriate compiler. .c to the C compiler. .s and .S to the assembler. Those two from the earliest versions. Others to the C++, ObjectiveC, Ada, Fortran, Pascal, go, modula, Java... depending on the version and what has been installed. Allowing now to elude the extension is just not possible without breaking with the design of the gcc executable as a master of ceremony. Convenience programs like g++, gfortran... are mostly just providing other defaults for the linked in libraries.
There is a cost of improving systems having an installed base in a backward compatible ways just as there is a cost of keeping the old ways available. The issue is that the cost is not paid by the same people and those impacted by one firstly tend to underestimate the true cost for those impacted by the other and secondly they see few reasons to pay anything for reducing it.
1
u/cxzuk Apr 24 '23
Hi Till-one,
I think there are two general points with C (and GCC) that don't align with your desires/expectations of a build and build system.
A C compiler is one tool within a set of tools (a toolchain). Arguably its core use-case was not to be run directly, but within a build system - like make. In today's world this would be like running a function call in a library directly. In fact, taking multiple files and producing an exe is somewhat of a luxury feature in this mindset.
The other thing is what it means to be a module, package etc. Cs ideas on these things probably don't align with what you want from your PL.
The key to this point is that -L is pointing to an external dependency. This makes perfect sense when you're meant to invoke the compiler with a build system. But not great directly.
To resolve this, dependencies have to be written down somewhere. Some keep it in a build config file, some have it in the source file itself, and GCC has them on the command line.
If you choose one of the other options, you start to need to define package, module etc.
Kind regards M ✌️
1
Apr 24 '23
Dependencies can just be listed in a file. Such a file could be directly submitted to a compiler using
cc @filename, and it can include any necessary options. But it's also simple enough for people to extract the info and devise their own solutions.Ideally the necessary files can be tidily present in one place too: there's a difference between a messy and sprawling development environment, where you need dependency info to avoid unnecessary compilations, and the situation where someone just wants to build a finished working program froms source. Then all that messy structure is irrelevant.
(When distributing programs as C source, I used to provide a single, amalgamated file. The ideas was for it to be as easy to build as hello.c.)
3
u/JoJoModding Apr 24 '23 edited Apr 24 '23
Your use case is uncommonly simple. Be reminded that programs usually consist of more than one file and use >0 libraries.
Let's say you have files main.c, foo.c, bar.c and want to additionally use your library baz. The whole program is of course to be called hello Then the command line simply is cc -o hello main.c foo.c bar.c -lbaz. This is very concise and the longest you would feasibly type in manually. For anything larger, you will want a separate build system that calls the compiler for you.
4
u/FUZxxl Apr 24 '23
Note that the
-ooption goes before operands. It's argument needs to be a separate command line argument. Other use may work with gcc, but is not portable:cc -o hello main.c foo.c bar.c -lbaz1
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.
1
1
u/umlcat Apr 24 '23
Took a long read after I found the " How does your stuff work."
I'm working to implement my compiler alike tools as both command line and GUI apps.
Both ways are useful, both ways have Issues.
I designing them, using an A.P.I. programming design approach, a function receives parameters and returns a result, including a potential error.
The user interface could be a command line or a GUI form, that receives the parameters and executes an A.P.I. style function, and get the results, and display it's either a console or a form.
I found a command line approach very useful with very few parameters, but when it requires too many options, it's a sign you may need to break that program into several programs, or use a single script file as options instead...
2
Apr 25 '23
[removed] — view removed comment
3
Apr 25 '23
The compiler does not produce the executable by itself.
Mine do!
It first produces the assembly, then invokes the assembler, then invokes the linker.
That just an implementation detail for C compilers like gcc and clang. With smaller ones like tcc and bcc (my product), there is no linker.
Ones like gcc are anachronistic. Although if it was to be brought more up-to-date, making gcc conform to more modern and safer standards by default would be a priority. As it is, it allows poor coding habits to be perpetuated because it will say nothing unless you tell it too, and beginners won't know how to do that.
What if the user actually implemented its own version of "pow" in a static library of his own doing?
What if the user implemented their own version of
fopen? What if they wanted to use the suppliedsin()(which needs-lm) but use their ownpow()?Having to add this explicit
-lmflag for 99.99% of the programs that use those functions is just a plain nuisance. Make this common case the default, and require overrides for the minority case. Doesn't that make sense? (I expect it wouldn't in the C subreddit where there would be a deluge of downvotes for such a suggestion.)To tell them something that you would find in literally 5 minutes of Google search?
Suppose that every single invocation of gcc required you to add a
-lcrtflag to pull in the library needed forprintfand so on.Sure you can find out in a few minutes that that's what you need to do. But that would get tedious very quickly.
BTW here's a funny thing; I ran this on rextester.com:
#include <stdio.h> #include <math.h> int main(void) { double x=3,y=4; printf("%f\n", pow(x,y)); return 0; }With
-O2, it runs fine without using-lm. With-O0, it needs it. For those saying thatpowisn't special, here it obviously is! gcc and clang know perfectly well whatpowis and use an internal version for constant reduction.However, C++ versions of gcc and clang don't need
-lmeven with-O0; they've dropped the requirement, and the world hasn't stopped turning.2
u/FUZxxl Apr 26 '23
What if the user implemented their own version of fopen? What if they wanted to use the supplied sin() (which needs -lm) but use their own pow()?
These cases are not nearly as uncommon as you think. A large part of the complexity of the glibc is ensuring that everything still works fine if users do this sort of thing.
With -O2, it runs fine without using -lm. With -O0, it needs it. For those saying that pow isn't special, here it obviously is! gcc and clang know perfectly well what pow is and use an internal version for constant reduction.
The compiler knows the behaviour of
powas it is specified in the standard. However, the user is free to provide his or her own implementation; it is just assumed to match the standard behaviour. You can turn this optimisation off with-fno-builtin-powif you desire. It's very helpful, e.g. if you want to instrument the number of calls to transcendental functions.
13
u/FUZxxl Apr 24 '23
That's not up to the compiler. The math functions are in a different library. There's nothing outdated about that. What do you expect the compiler to do? Link in every library on your system just in case any of them is used?
Just pass a
-ooption to select the binary name. Having a fixed default file name in case no-ooption is given is reasonable. What else behaviour do you expect?Never say “I get an error.” That is a useless sentence that doesn't assist in diagnosis. Instead say what specific error you get. The reason make can do this is because it has a specific builtin rule to generate a binary from one source file. The C compiler is a program to generate a binary from an arbitrary number of source files, so there is no obvious way to pick a name (just picking the first source file is a possibility, but can lead to surprising results).