LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Error "Undefined reference" while compiling a c program in linux (http://www.linuxquestions.org/questions/linux-general-1/error-undefined-reference-while-compiling-a-c-program-in-linux-893227/)

acharyaa 07-22-2011 06:31 PM

Error "Undefined reference" while compiling a c program in linux
 
3 Attachment(s)
Hi
I am trying to compile a built up code for a model. While compiling, I got the error of "undefined Reference" as below.I tried to compile with

gcc rout_version.c



/tmp/cclgEVdE.o: In function `MakeConvolution':
rout_version.c:(.text+0x1455): undefined reference to `sin'
rout_version.c:(.text+0x14a9): undefined reference to `sin'
/tmp/cclgEVdE.o: In function `MakeUH':
rout_version.c:(.text+0x2573): undefined reference to `sqrt'
rout_version.c:(.text+0x25d8): undefined reference to `pow'
rout_version.c:(.text+0x2613): undefined reference to `exp'
collect2: ld returned 1 exit status



I looked at the similar post in linux questions.org and tried to see if I can link like that. Since the error function "MakeConvolution" is a different file than the rout_version.c file, I was not able to fix it. I have attached related files with this post that might be helpful to fix this error in "rout_version.c". The file extensions are "F file for make convolution" ".c for rout_version.c". I don't know if it makes a difference but I had to change the extension since I can upload only txt file extension. The same code worked in "cygwin" (linux interface in windows) while it do not run in Linux. I can get object file (.O) in cygwin but get error in Linux. I need to run the program in actual Linux.

If anyone can help me out, it will be a great relief for me.


Sham

shambler 07-22-2011 11:12 PM

You need to explicitly link the math library. Like so:
Code:

$ gcc rout_version.c -lm
$ ls
a.out  rout_version.c
$


acharyaa 07-25-2011 10:59 AM

Thank You Shambler, it worked. I appreciate it. It would have taken me weeks to find what's going on. You guys are really helpful.

BTW why we need to link it to math library? What it does? Just curious since I am new and it's a built up code.

Sham

chrism01 07-25-2011 08:06 PM

... err because that's where the (code for) maths fns like sin(), pow(), exp() & so on lives ??
;)

acharyaa 07-25-2011 11:16 PM

Thank You. Appreciate it.


All times are GMT -5. The time now is 12:07 AM.