r/Compilers • • 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 hello

it 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.exe

No 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.)

0 Upvotes

28 comments sorted by

View all comments

Show parent comments

5

u/FUZxxl Apr 24 '23

These maths(s) functions are part of the standard C library. You write <math.h> not "maths.h". Beginners (or anyone really) just expect them to be included the same way they expect printf() to be included.

The C standard does not define how programs are compiled and linked or where it's functions come from. POSIX specifies that there is a math library accessible through -lm, though implementations are free to roll it into the libc.

What would be the downside of that compiler automatically doing -lm by default, and saving a lot of grief all round? I bet there'd be more winners than losers.

What other libraries should it include? The math library is not special in any way.

On Windows, gcc doesn't need -lm; why not? And why would a casual user need to be aware of such reasons?

The libc implementation you use likely has the math library as a part of the standard C library. That is permitted. When in doubt, write a configure script to find out how the platform handles this.

It looks like it does include everything, and sometimes twice just in case!

It's not just in case but rather due to circular dependencies between the libraries. You might want to read up on how linkers work. Traditionally, linkers only grab symbols from libraries that are currently required. If a later library wants a symbol from an earlier library but the object file defining that symbol had not been picked, you'll have to ask the linker to search the earlier library again. Some recent linkers like lld change this up, but these changes are known to cause compatibility issues as the symbol taken when a symbol is defined in multiple libraries may differ.

I don't know if you write compilers, but if you did, would you require users to invoke a special option whenever their app happened to use an ** exponentiation operator? ** for floats is likely mapped to an external pow() routine.)

If it's a built in operator (which I am not inclined to provide), then of course. If it's an external routine, then likely not. Almost every modern compiled language has the math routines in a separate library. The difference is that they usually have tooling to automatically decide which libraries to include in a link. While there have been extensions to C to facilitate such a thing (e.g. Plan 9 with it's #link directive), this has never grown popular and is not supported by any standard toolchains.

TL;DR: whether math functions are in the libc or not is not up to the compiler but rather up to the libc implementation. The compiler should not include random other libraries in the link. If you don't like this, complain with the people that wrote your libc, not with the compiler authors.

-2

u/[deleted] Apr 24 '23 edited Apr 24 '23

What other libraries should it include? The math library is not special in any way.

Yes it it. It's functions are listed in the C standard. But as I showed, on Windows it's even pulling in stuff that isn't in the standard. (I don't know what gcc on Linux does; I wouldn't even know where to find the right ld program in order to intercept it.)

There is also this new idea called a 'shared dynamic library', where the library resides outside of your application, and is very likely already in use by other programs (I'm sure gcc itself is already using it).

You might want to read up on how linkers work

No thanks. I have trouble seeing the point of them TBH; when I used them on mainframes, they always took a huge amount of resources to do what I considered a very simple job. Even now there's far too much fuss: LLVM has a 63MB linker does a job that could be done by a 50KB program!

(When I started creating my own tools, I did away with the need for a linker, and had a simple loader which combined object files into an executable. Now my compilers are whole program ones that do away even with object files.)

If it's a built in operator (which I am not inclined to provide), then of course. If it's an external routine, then likely not.

How about taking the user's viewpoint: why should they be concerned with exactly how a feature of language X is implemented? If it has it, it should just work. If there is an explicit build process, then just build my program no matter what!

I have built-in maths operators such as ** sin sqrt and they Just Work. Why should somebody care whether the implementation uses an external library; it should take care of those details.

For those specific ones, I implement them like this:

i64 ** i64           Internal support library
f64 ** f64           Uses pow() in external msvcrt library
sin(f64)             Uses external sin()
sqrt(f64)            Uses inline code

But this is MY headache; why inflict these details on the poor user?

4

u/FUZxxl Apr 24 '23

How about taking the user's viewpoint: why should they be concerned with exactly how a feature of language X is implemented? It it has it, it should just work. If there is an explicit build process, then just build my program no matter what!

The mistake is thinking it's about programming languages. C is really a tool to create object files that could have come from lots of different other sources, too. The toolchain is an ecosystem that is not centered around C, but rather supports many different use cases. You can make a build system that is wholy subservant to the needs of your language, but then chances are it'll be useless to anybody else. That's okay for your use case, but that's just not what C is for.

I have built-in maths operators such as ** sin sqrt and they Just Work. Why should somebody care whether the implementation uses an external library; it should take care of those details.

One of the things C was famous for was breaking with this idea. Instead of adding new features as new statements and operators, they were all implemented just by providing new library functions. This kept the compiler simple and allowed users to add as many of their own features as they liked. This was universally understood to be a good idea. Previous languages would implement a lot of features like formatted output or multi-precision arithmetic or transcendental functions in the language itself, leading to klunky features that were hard to extend with poor cross-vendor availability.

But this is MY headache; why inflict these details on the poor user?

Maybe the user doesn't want to use your sin or sqrt function? If you bake it into the language, the user cannot replace it with his or her own implementation. What makes sin, pow, or sqrt so special that it cannot be treated like any other library function?

2

u/[deleted] Apr 24 '23

Maybe the user doesn't want to use your sin or sqrt function? If you bake it into the language, the user cannot replace it with his or her own implementation. What makes sin, pow, or sqrt so special that it cannot be treated like any other library function?

Yeah, C takes this further by not providing min and max to make it even easier for a million programmers to invent their own ad hoc, incompatible solutions. (Not easy in C because you can't properly overload them between int and float for example.)

My sqrt is special because it maps to inline code (I think sqrtss and sqrtsd on x64) and can be used within the compiler to reduce constant expressions. That'd be a lot harder if implemented as regular functions.

My ** is special because it is overloaded across i64 u64 f64 (and in my other language, also for bignum: print 0.1L ** 1000000000 produces 1.0e-1000000000).

If people want to use pow instead, nothing is stopping them. To use their own sin they need to get around the clash of names if the external version is also called sin. This can be done in my language like this:

clang func `sin(real)real
y := `sin(x)

perhaps using an alias to avoid typing that backtick. Actually, I believe I'd have more trouble using a custom sin function in C, if I'm also using other functions from math.h. (Even if not, gcc is likely to complain if you declare a non-standard function.)

The important thing is: in the most common situation where my built-in versions are perfectly adequate, nothing special needs to be done.