0

The problem arises firstly from the "isfinite" function: (undefined reference to isfinite). From google search I find that I must include "math.h" and write three lines of code, like:

ifdef __linux__
define _finite(v) (__builtin_isfinite(v))
endif

But then, there comes the error: (Make:47 missing endif. Stop).

If I comment out those three lines of code, the error becomes: (<math.h> no such file or directory).

My system is OpenSUSE Leap 15.4; gcc version 7; gnu make version 4.2.1-7.3.2.

I think I have installed all the needed packages. However the errors persist. Any help?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Please edit your question and format it properly. We can't figure out at all what you're actually doing from this text. Just to be clear, you should add those lines **into your C source code**, _not_ into your makefile. – MadScientist Nov 21 '22 at 14:47
  • In fact, you *should not* add those lines to your source code, at least not verbatim. Somewhere along the line, leading `#` characters have been dropped from them, and those need to be restored when you put the lines into your C code. Though honestly, I suspect that they aren't going to help, and I'm sure that there are other, probably better, ways to achieve what you're after. – John Bollinger Nov 21 '22 at 15:03
  • I wasn't sure if the missing `#` were due to the formatting issues, or not. – MadScientist Nov 21 '22 at 17:44

1 Answers1

0

I primarily want to address this part of the question, as the underlying issue has been addressed elsewhere, multiple times:

The problem arises firstly from the "isfinite" function: (undefined reference to isfinite). From google search I find that I must include "math.h" and write three lines of code, like:

ifdef __linux__
define _finite(v) (__builtin_isfinite(v))
endif

I started to write "Google led you astray", but I think it's more likely that you seriously misunderstood what it led you to. And perhaps you happened to choose poor results.

The error message indicates that you put those lines in your makefile, but they are wholly inappropriate for that. Don't put them there.

The lines are C preprocessor directives that have been stripped of their leading # characters. You would use them by restoring the # characters ...

#ifdef __linux__
#define _finite(v) (__builtin_isfinite(v))
#endif

... and putting the resulting lines into one or more of your C source files. BUT DON'T! Those lines are unnecessary and at best unhelpful.

You do need to #include <math.h> in each C source file that contains a call to isfinite(), because that is the header that provides a declaration of the function. Since C99, functions must be declared before they are called (and it was good practice well before then).

Other than that, with GCC and many other traditional-style Unix C compilers, you need to explicitly include the math library in your link when you need any of the functions it provides. That would involve adding -lm to your makefile, at the end of the gcc command that builds the final executable. This is covered in gcc will not properly include math.h, and many other duplicates on this site.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • The .c file that has a call to isfinite() already includes . Also, the makefile has the -lm. The problem still persists. – user8386563 Nov 22 '22 at 14:14
  • @user8386563, this answer is about your misunderstanding of the nature and purpose of the three lines of code presented in the question. Again, they do not belong in your makefile, and their presence is the reason for the error you asked about in the latter part of the question. The issue with an "undefined reference" error is already thoroughly covered at the Q&A referenced by this answer, and a bit more generally at various other Q&As elsewhere on SO. If your makefile indeed does contain an `-lm` flag, then I can only assume that it is in the wrong place. – John Bollinger Nov 22 '22 at 14:20
  • Already removed the three lines of code and the makefile is in the directory where the .c files are. There shouldn't be an error! – user8386563 Nov 22 '22 at 14:25
  • @user8386563, if you're seeing the same "missing endif" error issued by `make` then you *have not* removed the lines, or you have another error of the same kind. Perhaps you have not saved the changes, or perhaps you edited a different file than `make` is using. Perhaps the modified file was overwritten by some automatic process. If there is an IDE involved then it is conceivable that there is other weird stuff going on, such as a cached version or an internal representation is being used instead of the file on disk. – John Bollinger Nov 22 '22 at 14:32