I cannot get my programs to link...I need help!

This is a discussion on I cannot get my programs to link...I need help! within the C Programming forums, part of the General Programming Boards category; I've copied and pasted my code. The main program, the calculateTaxes.cpp function code and my makefile. I am using the ...

  1. #1
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108

    Unhappy I cannot get my programs to link...I need help!

    I've copied and pasted my code. The main program, the calculateTaxes.cpp function code and my makefile. I am using the makefile to link these two codes together but I get an error when I type 'make' in the command line.
    I receive the error code:
    assign2c.cpp.text+0x169): undefined reference to 'calculateTaxes(float, float, float*, float*, float*)'
    collect: ld returned 1 exit status
    make: *** [main.exe] error 1
    I have no idea what this means!

    Code:
    /*   Name: Arturo
         Date: 03/22/13
         Purpose: To learn
    */
    extern void calculateTaxes(float gross,float deferred, float *ft, float *st, float ........i);
    void ovtHrs(float *hrs_wrk, float *ovt_hrs, float hrs, float *gross, float payrate);
    
    
    
    #include<stdio.h>
    #define BLANKLINES    "                                                                                                \n\n"
    #define REPORTHEADER  "Employee               Pay             Reg Hrs          Gross              Fed            SSI        Net\n"
    #define REPORTHEADER1 "Name                   Rate            Ovt Hrs          Pay                State          Defr       Pay\n"
    #define REPORTFORMAT  "%2s %2s       %7.2f %16.2f %17.2f %16.2f%15.2f%11.2f\n"
    #define REPORTFORMAT1 "                    %24.2f %34.2f   %12.2f\n"
    
    
    
    int main(void)
    {
      char lastname[15], firstname[15];
      float payrate=0, hrs_wrk=0, ovt_hrs=0, gross=0, ft=0, st=0, ssi=0, deferred=0, net=0, hrs=0;
      printf("Exxxxxnter the last name of the employee\n");
      scanf("%s", lastname);
      printf("Enter the first name of the employee\n");
      scanf("%s", firstname);
      printf("Enter the the pay of the employee\n");
      scanf("%f", &payrate);
      printf("Enter the amount of hours worked\n");
      scanf("%f", &hrs);
      printf("Enter the deferred amount\n");
      scanf("%f", &deferred);
      ovtHrs(&hrs_wrk, &ovt_hrs, hrs, &gross, payrate);
      calculateTaxes(gross, deferred, &ft, &st, &ssi);
      printf(BLANKLINES);
      printf(REPORTHEADER);
      printf(REPORTHEADER1);
      printf(REPORTFORMAT, lastname, firstname, payrate, hrs_wrk, gross, ft,  ssi, net);
      printf(REPORTFORMAT1, ovt_hrs,st, deferred);
      while(getchar()!= '\n');
      getchar;
      return 0;
    }
    
    void ovtHrs(float *hrs_wrk, float *ovt_hrs, float hrs, float *gross, float payrate)
    {
     if(hrs<=40)
     {
       *ovt_hrs=0;
       *hrs_wrk=hrs;
       *gross=hrs*payrate;
     }
     else
     {
       *hrs_wrk=40;
       *ovt_hrs=hrs-40;
       *gross=payrate*40+(hrs-40)*1.5*payrate;
     }
    }
    Code:
    #define FEDTAX 0.15
    #define STATE  0.07
    #define SSITAX 0.0775
    
    void calculatetaxes(float gross, float deferred, float *ft, 
                        float *st, float ........i);
    float calcfed(float gross, float deferred);
    float calcstate(float ft);
    float calcssitax(float gross, float deferred);
    
    void calculatetaxes(float gross, float deferred, float *ft, float *st, float ........i)
    {
     *ft=calcfed(gross, deferred);
     *st=calcstate(*ft);
     ........i=calcssitax(gross, deferred);
    }
    
    float calcfed(float gross, float deferred)
    {
     return(gross-deferred)*FEDTAX;
    }
    
    float calcstate(float ft)
    {
     return ft*STATE;
    }
    
    float calcssitax(float gross, float deferred)
    {
     return SSITAX*(gross-deferred);
    }
    Code:
    main.exe: assign2c.cpp taxes.o
    	g++ assign2c.cpp taxes.o -o main.exe
    
    taxes.o: taxes.cpp
    	g++ -c taxes.cpp -o taxes.o -c
    Last edited by arti; 03-23-2013 at 01:12 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,169
    It means that the linker is not able to establish a link between the statement that calls calculatetaxes() and the entry point for that function.

    As is, the code should not even compile since this type of thing (note the bits I have highlighted in red) are not valid code.
    Code:
    extern void calculateTaxes(float gross,float deferred, float *ft, float *st, float ........i);
    If code does not compile, the link will also fail. If your build script is proceeding to do a link even if compilation of one of the source files fails, that would explain the error message.

    The fact that the function, as called in main(), is named calculateTaxes(), and the definition (in the other source file) is calculatetaxes() would also explain the link error, even if the source files were successfully compiled. C++ is case sensitive, so an identifier abc is distinct from identifier Abc.


    More generally, you need to ensure that ALL of the .cpp files successfully compile, that there is one and only one implementation of calculateTaxes() in only one source files, and then link all the resultant object files together.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User arti's Avatar
    Join Date
    May 2010
    Posts
    108
    I don't know what happened there when i pasted the code. The actual code reads like so....
    [code]
    extern void calculateTaxes(float gross, float deferred, float *ft, float *st, float ssi);

    I didn't notice that the name was spelled wrong! Shoot I'm dumb.....I don't know how I could have overlooked that. Thank you so much! This time when I typed 'make' in the command line it compiled!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me link to a .so
    By thealmightyone in forum C Programming
    Replies: 5
    Last Post: 11-18-2010, 12:33 PM
  2. Replies: 1
    Last Post: 11-09-2009, 06:03 AM
  3. Programs opening programs
    By LinuxPLC in forum C Programming
    Replies: 1
    Last Post: 11-21-2002, 11:50 AM
  4. unable to link NCB programs
    By rohit in forum Windows Programming
    Replies: 0
    Last Post: 05-12-2002, 09:47 AM
  5. Link
    By CFRK in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 09:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21