General Question

Thammuz's avatar

I need C troubleshooting: When i include a library do i have to do something more in order to have its functions work in my script?

Asked by Thammuz (9287points) October 23rd, 2009

I’m writing some code for my “programming fundamentals” class, just for a bonus point with the examiner, i need to use some functions from the math.h library so i added the ”#include <math.h>” string at the beginning, along with the others i needed.

Then i looked up the functions i needed on the net and i wrote the code. the compiler tells me there is an “implicit declaration of function ‘div’” and an “implicit declaration of function ‘fmod’” (which are the only two functions from math.h i’m using)

The problem is that i did use other libraries before and i never had this kind of problems.

If it’s needed i will post the code, i didn’t post it right now because i have to pass it from my laptop and i’m too lazy to do that without good reason…

Cheers and thanks in advance!

Observing members: 0 Composing members: 0

17 Answers

grumpyfish's avatar

Are you getting any other errors, like “math.h” not found?

Actually, IIRC, not finding an include is just a warning…

Thammuz's avatar

Nope, no problem with that, that’sthe odd part.

Besides, i’m using ubuntu, so the libraries are there for sure, they’re part of the system.

Just in case: i’m programming in C, not C++

Thammuz's avatar

The part in which i use div and fmod is this:

while(quotient != 0){
output[counter] = fmod(quotient, 2);
quotient = div(quotient, 2);
counter++;
}

It’s one of the sub-cycles for a base 10 to base 2 converter.
The variables are all declared as integers.

Zaku's avatar

No, that should be fine. I think the compiler is just giving you helpful announcements.

(You might want to refer to your code as code rather than script. Script usually implies a scripting language as opposed to a programming language.)

Thammuz's avatar

Problem is that it then refuses to createthe executable. It says “undefined reference to fmod”.

Zaku's avatar

I’m guessing you probably need to specify the linkage for the linker.

Thammuz's avatar

how do i do that?

By the way i’m using Geany if it’s any help.

ratboy's avatar

You must use a compiler switch to let the linker know to use the math library. For example: -lm if you are using gcc.

Thammuz's avatar

hm, do i have to simply add it to the command? and on what stage? compilation or creation of the executable?

Ok, i added on both, just to be sure, it compiles and generates but when it should use that command it gives me a segmentation error

ratboy's avatar

Segmentation error usually means that you’ve written beyond the end of an array—check your indices for an “off by one” error.

Thammuz's avatar

Still gives me a “warning: implicit declaration of function div”

And it still gives me a segmentation error no matter what i do.

The whole part that gives the error is this:

case 102: printf(“Insert the number you want to convert: ”);
scanf(”%d”, &input);
quotient = input;
while(quotient != 0){
output[counter] = fmod(quotient, 2);
quotient = div(quotient, 2);
counter++;
}
printf(”%d base 10 in base 2 equals ”, input);
while (counter > 0) {
printf(”%d”, output[counter]);
counter—;
}
break;

By the way why does the compiler still give me the warning for div but not for fmod?

ratboy's avatar

div is in stdlib.

Zaku's avatar

oh that would also do it :-)

Thammuz's avatar

F. U. C. K.

Ok, thanks i’ll include the library and see if it works…

Thammuz's avatar

Done, it works now. Thanks a lot guys!

Zaku's avatar

I’m glad you got it to link.

grumpyfish's avatar

Interesting!

Most of my experience in C of late has been using avr-gcc—which links its libraries automatically if you include them (you just have to manually include any libraries you’re using outside of it). Doubt it’d be of much use to use, since it’s for AVR microprocessors =)

Glad you got it to work, however!

Answer this question

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther