Thread: Linking library problem

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Linking library problem

    Hi
    I have copied the following C program (err.c) from a book and attempted to compile it.

    I use the following command line protocol:

    gcc –lm err.c –o err

    When I do I get the following error:

    /usr/lib/gcc/i386-redhat-linux/4.1.1/../../../crt1.o: In function `_start':
    (.text+0x18): undefined reference to `main'
    /tmp/cckvrV7Z.o: In function `rtnewt':
    err.c.text+0x6e): undefined reference to `nrerror'
    err.c.text+0x9d): undefined reference to `nrerror'
    collect2: ld returned 1 exit status

    It appears to be a “linking” problem, but I am linking the math libraries. I have no problems with sin, cos etc. It is only when the `nrerror` shows up in the script that I start getting problems with compilations.

    Thanks, Steve

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you post what you are actually compiling?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    Hi
    Here is the code:
    Code:
    /*The Newton Raphson C program*/
    
    #include <math.h> 
    #define JMAX 20
    
    float rtnewt(void(*funcd)(float,float *, float * ), float x1, float x2, float xacc)
    {
      void nrerror (char error_text[]);
      int j;
      float df,dx,f,rtn;
    
      rtn=0.5*(x1+x2);
      for (j=1;j<=JMAX;j++) {
          (*funcd)(rtn, &f,&df);
          dx=f/df;
          rtn -=dx;
          if((x1-rtn)*(rtn-x2) < 0.0)
            nrerror("Jumped out of brackets in rtnewt");
          if (fabs(dx) < xacc) return rtn;
         }
         nrerror("Maximum number of interations exceeded in rtnewt");
         return 0.0;
    }
    Any help would be greatly appreciated!! I have looked online at FAQ but so, far there has been no help.

    Thanks
    Steve

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Every C program must have a main function of type int main().
    That is, unless you aren't posting the whole code.

    Also, you have declared nrerror but never actually defined it (where is the function's source code?!?).
    I also think your indentation could be improved.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    4
    I found that the function nrerror() is part of a utility program in nrutil.c but I don't know how to implement it in compilation and add to script. Can you inform me how to do this?

    Thanks for your response!!

    Steve

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Include the .c file in your compilation command line. Basically compile all the .c files then link them together.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Wink

    Thanks Elysia.
    Your comments were very helpful!!
    Cheers,
    Steve

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LNK2019 when linking against a static library.
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2010, 01:39 PM
  2. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  3. help solve linking problem, thanks
    By Raison in forum Windows Programming
    Replies: 8
    Last Post: 05-29-2004, 11:14 AM
  4. Linking problem...
    By BrianK in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2002, 04:13 PM
  5. Library Linking problem
    By Lynchie in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 08:49 AM