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

13

u/FUZxxl Apr 24 '23

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

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?

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.

Just pass a -o option to select the binary name. Having a fixed default file name in case no -o option is given is reasonable. What else behaviour do you expect?

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

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

4

u/[deleted] Apr 24 '23

What do you expect the compiler to do? Link in every library on your system just in case any of them is used?

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

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.

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

Actually, on Windows gcc invokes ld with these library options out of 56 in all:

-lmingw32
-lgcc
-lgcc_eh
-lmoldname
-lmingwex
-lmsvcrt
-lkernel32
-lpthread
-ladvapi32
-lshell32
-luser32
-lkernel32
-lmingw32
-lgcc
-lgcc_eh
-lmoldname
-lmingwex
-lmsvcrt
-lkernel32

It looks like it does include everything, and sometimes twice just in case! Compared to this it seems churlish to leave out a few maths ops. (Here, maths functions can be found inside msvcrt.dll.)

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

I'm telling you, it is outdated. I had my own floating point library in the 1980s, mainly concerned with emulating floating point in software, which was an extra module that needed to be included, a consideration when working with 64KB of total memory. But that was a very long time ago.

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.

2

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

Just pass a -o option to select the binary name. Having a fixed default file name in case no -o option is given is reasonable.

No it isn't. I might expect that in a beginner's effort, or a throwaway program, or in the early stages of developing an application. But a major piece of software that has been developed and refined over 35 years?

What else behaviour do you expect?

I want gcc prog.c to produce prog.exe not a.exe, the same way that bcc prog.c or tcc prog.c or lcc prog.c do. Because I'd be switching constantly between compilers, and it's easier to tweak the compiler name and/or program name when recalling the last command line. I don't want to have to specify the program name twice.

Never say “I get an error.” That is a useless sentence that doesn't assist in diagnosis. Instead say what specific error you get. [About 'make']

It's different every time. Sometimes make works on Windows, usually it doesn't, generally failing at some arbitray point. In this case it fails like this:

c:\xxx>make c
cc     c.c   -o c
process_begin: CreateProcess(NULL, cc c.c -o c, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [<builtin>: c] Error 2

(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:

c:\xxx>bcc c
Compiling c.c to c.exe

No -o option needed; not even the .c extension (being a C compiler, bcc makes a wild guess as to my intentions); and no maths options (c.c contains a call to pow).

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

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.

7

u/DonaldPShimoda Apr 24 '23

But a major piece of software that has been developed and refined over 35 years?

Ironically, this is exactly why gcc still does the same thing: it needs to be dependable and consistent with the behavior of the versions of the past few decades. Whether you like the a.out default now, it was chosen way back when, and so gcc needs to maintain backwards compatibility with that choice. Why? Because gcc is among the most important components of a ton of software these days, and making a breaking change like that could have significant impact on many real-world systems.

Building a language isn't just about choosing the shiniest new features that seem reasonable. It's also about being a steward for your users. I'm not saying C has succeeded in all regards, but it has consistently been steered with user interests in mind.

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

u/[deleted] 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 -o option 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 -lbaz

1

u/JoJoModding Apr 24 '23

thanks, fixed :)

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

u/chibuku_chauya Apr 26 '23

Out of curiosity, in what way is Python's module system worse?

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

u/[deleted] Apr 25 '23

[removed] — view removed comment

3

u/[deleted] 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 supplied sin() (which needs -lm) but use their own pow()?

Having to add this explicit -lm flag 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 -lcrt flag to pull in the library needed for printf and 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 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.

However, C++ versions of gcc and clang don't need -lm even 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 pow as 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-pow if you desire. It's very helpful, e.g. if you want to instrument the number of calls to transcendental functions.