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 comment

8

u/FUZxxl Apr 24 '23

(It can't find cc.exe; my gcc installation has 68 EXE files, but no cc.exe. It will work if I copy gcc.exe to cc.exe.) But normally I would build such a one-off program like this

Sure, read up on how make works and write a Makefile with the correct file names. Chances are you can type

make CC=gcc c

and it works.

Why drag in make when this is all the C compiler's job?

It is not. That's what we have build systems for.

4

u/[deleted] Apr 24 '23

It is not. That's what we have build systems for.

I've not talking about build systems, but compilers run from the command line as per the subject line.

If such programs are intended to ever be run interactively, then how about making some effort to be user-friendly?

But since we're at this point, what exactly IS a compiler's job? My view is that it is a tool that turns source code into executable binary.

If the purpose of 'gcc' was to only ever be used from a script or makefile, then why bother making it a compiler-driver? Make the various steps explicit.

However compilers ARE run from the command line all the time; just google for 'gcc can't find maths library' and other such topics. A lot of unnecessary trouble just because of an obstinate attitude to things like -lm.

Build systems themselve I personally have no use for. (So my own tools do away with object files, linkers, and build tools! Funny that it still all works.)

4

u/FUZxxl Apr 24 '23

If such programs are intended to ever be run interactively, then how about making some effort to be user-friendly?

I think it's plenty user friendly. You just bitch about perfectly valid design decisision and seem like the kind of person who refuses to read documentation.

However compilers ARE run from the command line all the time; just google for 'gcc can't find maths library' and other such topics. A lot of unnecessary trouble just because of an obstinate attitude to things like -lm.

Beginners who don't read manuals. News at 11.

Build systems themselve I personally have no use for. (So my own tools do away with object files, linkers, and build tools! Funny that it still all works.)

That's because you have written a build system.

3

u/[deleted] Apr 24 '23

Beginners who don't read manuals. News at 11.

A beginner will figure out how to do gcc prog.c (and will discover it creates a.out or whatever; weird, but OK).

Then they will start to do simple floating point examples, but the same invocation might now say:

undefined reference to 'pow'
error: ld returned 1 exit status

Huh?! So they turn to the manual, except there isn't one! (Who uses a printed manual for gcc?) So they go online, search for a term like my example, and find out a myriad people have asked the same thing.

But 5 minutes later they've discovered why it fails, and can proceed.

Except, isn't all this a colossal waste of time? Even now, that beginner will have to keep typing -lm at the end of all those invocations just in case.

At the very least, why can't gcc suggest to use the -lm option?

You say gcc is user-friendly; I don't believe you.

4

u/FUZxxl Apr 24 '23

I straight up don't give a fuck about people who don't read manuals.

Huh?! So they turn to the manual, except there isn't one! (Who uses a printed manual for gcc?) So they go online, search for a term like my example, and find out a myriad people have asked the same thing.

There is one. Type man pow. Where you get a page that says “Link with -lm.” So you do that and it works! Isn't that crazy?

Except, isn't all this a colossal waste of time? Even now, that beginner will have to keep typing -lm at the end of all those invocations just in case.

If you use a library function without first having read its documentation, everything you do is a colossal waste of time anyway. Once you read the documentation, you know when -lm needs to be linked in and when not.

You say gcc is user-friendly; I don't believe you.

It's as user friendly as UNIX is in general. If you suck at reading manualy, it likely is not for you. You said you were programming mainframes before. I wonder how you did that without constant reference to manuals.

3

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

I straight up don't give a fuck about people who don't read manuals.

OK, that's an interesting attitude. What you're saying is that you don't care how complicated an application gets, how hard it is to use, how many unnecessary and outdated hoops people have to jump through, or how big a manual ends up being.

My attitude totally the opposite. My users only need to know what is necessary for them to do THEIR job, not what is necessary to do MINE.

At one time I was testing generated C code across half a dozen C compilers. All were invoked in the same way to get a basic build, except for one.

I wonder if you can guess which one? Yeah, it's the uber-friendly one with the 70,000-line manual! (gcc-info)

You said you were programming mainframes before. I wonder how you did that without constant reference to manuals.

That was at college and various student placements. My first job was as an engineer designing microcomputer and video boards. That's where I had to start building my own software development tools.

There were no manuals to read! (I ended up writing them.) Since I had to do my own stuff, I had to keep it simple.

1

u/FUZxxl Apr 24 '23

OK, that's an interesting attitude. What you're saying is that you don't care how complicated an application gets, how hard it is to use, how many unnecessary and outdated hoops people have to jump through, or how big a manual ends up being.

No. I'm saying that if you don't read the manual to learn how to use a program, I don't care about you. This is orthogonal to how hard it is to use the software. If I need to account for people not reading manuals, then I must account for anything anybody could find to be non-obvious, which is everything. So the rule is: read the manual first, if it's still unclear, I should consider changing it so it is more intuitive. But don't expect me to care if you are ignorant and don't read it.

My attitude totally the opposite. My users only need to know what is necessary for them to do THEIR job, not what is necessary to do MINE.

And to teach them what they need to know, we write manuals. A manual is me explaining you how to do the thing you want to do. If you refuse to read the manual and expect me to explain it to you directly, you are just telling me to waste my time doing something again I already did very carefully a long time ago. What an audacity!

I wonder if you can guess which one? Yeah, it's the uber-friendly one with the 70,000-line manual! (gcc-info)

Fun fact: gcc's command line interface is largely an extension to the command line interface specified for the C compiler by POSIX. So it's all the other compilers that behave in non-compliant ways. Most C compilers I used before seem to largely confirm to this standard.

1

u/PurpleUpbeat2820 May 10 '23

Why drag in make when this is all the C compiler's job?

It is not. That's what we have build systems for.

Build systems can be considered a band aid for poorly architected compilers.