im in new to c, i did this is some program and i used the math library, but i was wondering how is the math library available within the the system? and how can we find the names of the files that correspond to that library?

Also, i read something about "linking against the math library"

what is " linking against the math library " and what are the possible ways to do it ?

thank you :)

The math library is just compiled code. It is in the ~lib/ directory.

When you #include <math.h> you are only telling the compiler what the things (functions) in the math library look like. But that doesn't automatically link those functions into your program. You have to do it with the appropriate command. For example, if I were using the GCC, I'd say: gcc myprogram.c -lm where that "-lm" is the compiler directive for Link Math library.

Hope this helps.

intresting :)

and what are names of the files that correspond to that library?

intresting :)

and what are names of the files that correspond to that library?

libm.a

intresting :)

and what are names of the files that correspond to that library?

depends on your compiler because there is no standard naming convention. *nix it's normally in libm.a as Duoas reported. MS-Windows Microsoft compilers the math functions are in one of the c runtime libraries.

how can i get to the ~lib/ directory?

its installed with your compiler. search your file system for libm.a and you will find it. On *nix computers:

cd / <Enter> -- cd to root
find . -name libm.a -print <Enter>

im trying it now, the computer is still searching for the file.

whats the diffrence btw

gcc -o myprogram myprogram.c -lm

and gcc myprogram.c -lm


is can this be used in both linux and windows to compile a c program ?

Read the manual.

You don't usually have to care where your compiler keeps lib files. If you link with the -l switch it will find it. -lm math library -lcurses terminal library -lglut -lglut32 -lglaux -lopengl32 various OpenGL libraries -lwinmm Windows multimedia library

etc.

thank you for the manual link ! its very useful.

so the same commands will work on both linux and windows ?

Yes. The GCC is a wonderfully well-ported compiler system. (One of the reasons I like it so much.)

You can also get IDEs that use the GCC as the backend compiler.

Thanks man
I'll copy paste it.

so the same commands will work on both linux and windows ?

rarely, if ever, does windows and linux have the same commands for anything.

for compiling C on windows, you might typically use the MSVC compiler distributed for free from Microsoft, either as a standalone or as part of the Visual Studio. the basic compiler is invoked by "cl", and has totally different arguments than other compilers, such as gcc.

on linux you'll typically use "gcc" which is, imo, superior. windows uses a lot of non-standard libraries that do not port, and is the cause of much frustration.

whats the diffrence btw

gcc -o myprogram myprogram.c -lm

and gcc myprogram.c -lm

the -o argument allow you to name the "output" of the compiled binary. if you dont use -o, it will always name it "a.out"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.