No linking required with some functions [duplicate] - c

This question already has answers here:
undefined reference to "only some math.h" functions
(4 answers)
Closed 5 years ago.
I just encountered a strange thing. While testing math.h I tried to use pow() and compile it. I did not have to link math.h .
But when I try the same with something like fmod() I have to link math.h while compiling the programm.
Why do I have to link the library in the second case but not in the first?

Your compiler may be replacing some usages of pow with a constant. For example, it could replace pow(2.0, 3.0) with 8.0. This is a good optimisation and means you no longer need the pow in math.h.
But your compiler probably can't replace fmod, or all usages of math functions, so it still needs to link to the math library.

Related

Intercepting Arithmetic Operations in C program [duplicate]

This question already has answers here:
Operator overloading in C
(7 answers)
How do you get assembler output from C/C++ source in GCC?
(17 answers)
Is it possible to overload operators in C?
(4 answers)
Closed 4 years ago.
Is there a way that we can call user defined function when we are calling arithmetic operator in a C program just like operator overloading in C++. using GNU GCC Compiler?
Simply,
I have a function add(), and in my C program I have arithmetic Operation
c = a + b;
when I compile the program, it should call my add() function internally for + operator.
and Is there a way we can see what is the code that gcc compiler is calling when it encounters + operator?
No.
C does not work that way, you cannot overload/override the basic built-in operators.
Seeing the code is of course possible, either by making gcc emit it directly using -S, or by disassembling the resulting binary. The related binutils tool is objdump.
These days much such exploration can also be made "online" using the fantastic Compiler Explorer tools at godbolt.org, of course.

How to find definition of library functions in C [duplicate]

This question already has answers here:
Where are the functions in the C standard library defined?
(5 answers)
Closed 7 years ago.
Functions like printf() , scanf() , memset() , puts() etc have their declaration in header files but is there any mechanism to see the definition of these function..?
This might not be a new question but i could not find the appropriate solution for this.
Find your compilers include path (e.g. GCC solution)
Search for the header you are interested in (e.g. printf should be in stdio.h or more likely another header included by stdio.h)
Correctly configured, some IDEs will help you with that, e.g. Eclipse
The method has its limits though, because at some point the include files will get less and less Standard-C, but more and more compiler dependent. The C-standard does not prescribe the contents of standard headers. It merely states that if you write #include <stdio.h>, you can use printf(). That does not necessarily mean that stdio.h has some form you might expect.

<stdio.h> vs <math.h> - Why do you have to link one and not the other? [duplicate]

This question already has answers here:
Why do you have to link the math library in C?
(14 answers)
Closed 9 years ago.
I'm confused why you have to type -lm to properly link math to your code, but don't have to do the same for stdio. I've only just started using C, so I apologize if this is a stupid quesiton or I'm missing something obvious.
In short, because of historical reasons,
The functions in stdio.h are in libc, while the functions in math.h are in libm. libc is linked by default but libm isn't.
There are two different things:
header files (stdio.h and math.h) - they contain only function prototypes and some definitions and data; they are #included in your source code
libraries (libm.so) - they contain binary code which will be linked back into your application (binary code). Also, for a library named libname.so the linker flag is -lname - for libm.so the flag is -lm.
Take also in consideration that there are libc.so and libstdc.so which are always linked into your application. Code for functions in stdio.h and stdlib.h and several others is found on those libraries - thus, it is always included.
PS: I'm assuming Linux/UNIX here, thus the names are very specific. On Windows things are similar but with other names (DLLs instead of .so files, etc.)

Math.h library functions in assembly x86? [duplicate]

This question already has answers here:
How does C compute sin() and other math functions?
(22 answers)
Closed 3 years ago.
I tried to convert C code that is written under Linux (fedora 9) to assembly x86 code, however, I have problem in a Math.h functions. The functions in this library such as ceil, floor, log, log10, pow are undefined in the assembly x86. Can you please help me to solve this problem?
Thanks.
Most library functions won't be defined in assembly language, at least not in the sense of the addition operator directly mapping to the ADD instruction. If you want to re-write the library in assembly, you'll have to implement the function using whatever capabilities that your processor has available. Most library functions will require a separate assembly language subroutine, not just a single operation. The easiest way to approach this is to get the individual library subroutines working in isolation, then incorporate them into the larger program.
You can compile the C code and examine the disassembled output, but beware of compiler optimizations that can make the output hard for a human to follow.
May I ask what the purpose is behind this task? Since a compiler is essentially a C to assembly-language translator, there's rarely a need to do this by hand. Is this homework?
The best way to find out what these functions do is to take a look at their implementation in glibc's source. It should give you clear enough insight. Another way would be to take a look at the disassembly of lm.so found in /usr/lib/.

Where to find the implementation of stdio.h? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Where to find stdio.h functions implementations ?
Hi, I am trying to find the function definitions of the functions defined in stdio.h header file, I want to learn how functions like printf() is achieved, but I can't find any preprocessor directives link in stdio.h to the implementation file elsewhere. How can a C Compiler know where to find the implementations when there are no direct references to the function definition file? (I learned that .h file may accompany with a same name .c implementation file from an objective-c book.) Could you help me? Thanks! I am using GCC on Mac OS X.
FreeBSD's libc is pretty well laid out in its src repository.
http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libc/
e.g. for printf(3):
http://svnweb.freebsd.org/base/head/lib/libc/stdio/printf.c?view=markup
http://svnweb.freebsd.org/base/head/lib/libc/stdio/vfprintf.c?view=markup
Try downloading source code for GLIBC library project. That's where definitions for standard functions are when using GCC compiler (and derivates).

Resources