vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

Missing necessary flag when building in C with gcc

Open sthavens opened this issue 6 years ago • 19 comments

Type: General When building with gcc (I am using WSL2 but I don't think that it matters), gcc requires the "-lm" flag to link some headers. My issue was with "math.h".

Describe the bug

  • OS and Version: Windows 10 20.04, WSL2, gcc 9.3.0
  • VS Code Version: 1.49.1
  • C/C++ Extension Version: 1.0.0
  • Other extensions you installed (and if the issue persists after disabling them): Issue specific to build configuration in this extension.
  • A clear and concise description of what the bug is.

To Reproduce Create a build that needs the math.h header (not sure which other headers need this flag).

Expected behavior The base build configuration should be able to follow the #include <math.h> path and actually include the contents of the header.

Screenshots Error

Additional context Following is a basic call to math.pow that produces the error in the build. This and any related issues can be fixed by adding the "-lm" flag to the build when compiling a C file in gcc.

`#include <stdio.h> #include <stdlib.h> #include <math.h>

int main(void) { int base; int expo; scanf("%d", &base); scanf("%d", &expo); printf("%f", pow((double)base, (double)expo)); printf("\n"); }`

sthavens avatar Sep 20 '20 20:09 sthavens

@sthavens

Does the build work when the flag "-lm" is added to the args in tasks.json? The extension cannot determine any specific flags for building when the base build template is created in tasks.json. Any specific args/flags and additional include path must be specified manually.

michelleangela avatar Sep 21 '20 17:09 michelleangela

The build does work when you add the flag. The reason that I suggest this update is that, unless if your file(s) to be compiled are either completely self-referencing or only need the most basic of header files (stdio.h and stdlib.h), your build will fail as it cannot compile the other headers unless if the flag is used.

sthavens avatar Sep 21 '20 18:09 sthavens

bump

W1F3B34T3R avatar Sep 30 '20 05:09 W1F3B34T3R

any updates @sean-mcmanus ?

W1F3B34T3R avatar Oct 02 '20 14:10 W1F3B34T3R

@W1F3B34T3R No updates.

sean-mcmanus avatar Oct 02 '20 18:10 sean-mcmanus

@sean-mcmanus can u at least tell us how long will it take?

W1F3B34T3R avatar Oct 07 '20 20:10 W1F3B34T3R

We are currently working on issues tagged for the 1.1.0 milestone. When an issue is assigned a milestone, then you will know that it is nearer to being addressed. Until then, we cannot provide an estimate for when this will be addressed. In the meantime, the solution is to add the missing flag to your tasks.json file.

bobbrow avatar Oct 07 '20 21:10 bobbrow

There are many flags that alter the behavior of gcc - what it links to, what it defines, what language extensions it supports, etc.. It may not be desirable for the C/C++ Extension to automatically add args that modify the effective default behavior of the compiler. IMHO, it would be best to require these be specified by the user, as desired.

Colengms avatar Oct 07 '20 21:10 Colengms

Colen, while in most cases, I would agree with you, I do not in this case. I think that the number of people who build a C project (this feature has no bearing on C++) with libraries outside of standard library (which is rather small in C) far outnumber those who are building toy "hello world" projects. If you read the original request, you will see that even the math.h file cannot be included into your project without this flag. Further, all that the flag does is tell the compiler to search the usual places for the correct file.

sthavens avatar Oct 07 '20 22:10 sthavens

even the math.h file cannot be included into your project without this flag.

My understanding was that this is limited to math.h. Are there any other features that this arg or other similar args are required for? Users of gcc who require math.h are likely aware that an additional arg needs to be added to their gcc command line in order for that to work as expected.

Colengms avatar Oct 07 '20 22:10 Colengms

From my understanding, you can literally not use any header file outside of the standard library (or likely something local) without this flag. Math.h is a simple example of a file that is heavily used that cannot be included without the flag. The compiler will not search anywhere except for the C standard library without it.

This is basic functionality and, although I was easily able to google how to fix it and I'm no more than an intermediate user of gcc at best. I think that it's a convenience for my skill level and above as well as necessary for those transitioning from basic programs to more advanced.

I know that I faced this problem when I first tried cpptools when I was really new to C... I fixed it by moving to Code::Blocks (terrible IDE) which, I just found out, set this flag by default.

sthavens avatar Oct 07 '20 22:10 sthavens

Math.h is a simple example of a file that is heavily used that cannot be included without the flag

The -l argument indicates a library to link against. The "m" refers to gcc's math library libm.lib/so. So, this is specific to gcc's C math library. By definition, I don't believe there is anything other than the math library that would be addressed by specifying -lm. (Let me know if you have an example to the contrary). My concern is that this same reasoning could be applied to other libraries that require passing additional args to gcc. If there is a general issue here, it should be addressed for all instances within that category of issue. Perhaps the same should be done for -lpthread and -lrt?

Colengms avatar Oct 07 '20 23:10 Colengms

I found a comment here: http://autotoolset.sourceforge.net/tutorial.html

The catch is that there are many functions that you may consider standard features of C that are not included in the libc.a library itself. For example, all the math functions that are declared in math.h are defined in a library called libm.a which is not linked by default. So if your program is using math functions and including math.h, then you need to explicitly link the math library by passing the ‘-lm’ flag. The reason for this particular separation is that mathematicians are very picky about the way their math is being computed and they may want to use their own implementation of the math functions instead of the standard implementation. If the math functions were lumped into libc.a it wouldn't be possible to do that.

While we can work around this behavior, there is also value is not changing this decision for our users of gcc, and directing users to provide feedback to GNU.

Colengms avatar Oct 07 '20 23:10 Colengms

The Build and Debug Active File is targeted to new users, so adding -lm by default for gcc (clang?) compilers seems fine to me.

sean-mcmanus avatar Oct 08 '20 18:10 sean-mcmanus

Colen, I don't think that you understand that this has nothing to do with math.h other than it was used as an example. In the case that you pointed out with the mathematicians, they simply would not #include the math.h. The point is that if you use any header outside of the standard library, you need the flag. While there are a bunch of headers in the standard library, there are many many more outside of it and you can't use any of them without the flag or a bunch of extra code. I could be wrong but this is what all of the research that I've done tells me.

sthavens avatar Oct 08 '20 21:10 sthavens

Can you give an example of code that doesn't use math.h that requires -lm to link?

sean-mcmanus avatar Oct 09 '20 00:10 sean-mcmanus

I did find a reference to the fact that it is actually a shortcut that adds the "libm" library to the search path. I stand corrected on the issue (as I re-read Colen's explanation, he was correct) as the math.h header encompasses everything in that particular library.

It is still probably a good flag to have as a standard as it adds in a large bit of core functionality and is only used when math.h is included. The reason that I posted it in here is that when I googled how to get the header in, the consensus was that it was a flag that people just added onto their compiler as a standard.

It is ultimately up to you all whether or not to add it.

sthavens avatar Oct 09 '20 01:10 sthavens

@sthavens not sure if it might help you or not but i use code runner extension and you can edit cli arguments in settings.json file, i'll post a video if it helps.https://gofile.io/d/YSldb2

W1F3B34T3R avatar Oct 18 '20 08:10 W1F3B34T3R

This feature request is being closed due to insufficient upvotes. When enough upvotes are received, this issue will be eligible for our backlog.

github-actions[bot] avatar Dec 17 '20 11:12 github-actions[bot]