Problem with linking files

This is a discussion on Problem with linking files within the C Programming forums, part of the General Programming Boards category; I'm having trouble with these files: Code: /* File peptide.c */ #include <stdio.h> #include <stdlib.h> #include "residues.c" /* Simulate a ...

  1. #1
    Caution: Wet Floor
    Join Date
    May 2006
    Posts
    55

    Problem with linking files

    I'm having trouble with these files:

    Code:
    /* File peptide.c */
    #include <stdio.h>
    #include <stdlib.h>
    #include "residues.c"
    
    /* Simulate a peptide sequence and report its hydropathy index */
    
    int main(void) {
    
      puts("Your peptide sequence is\n");
      puts(strcat(sequence(), " ."));
    
      puts("\n\n");
    
      printf("Cumulative hydropathy: %f;\n"
             "average hydropathy: %2.1f.\n", hp, hp/20.);
    }
    
    /* --------------- */
    
    /* File residues.c */
    
    #include "peptide.h"
    
    char* sequence(void) {
    
      int aaLeft = 20;
      char* seq = "NH2-"; /* Build from the N-terminus of the chain. */
    
      seq = malloc(120 * sizeof(char));
    
      while(aaLeft--) {
        int aa;
    
        aa = random() % 20; /* Randomly choose an amino acid from the list */
    
        seq = strcat(seq, residue[aa]);
        hp += hpi[aa];
      }
    
      seq = strcat(seq, "COOH"); /* We're done */
    
      return seq;
    }
    
    /* --------------- */
    
    /* File peptide.h */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    char* residue[20] = { "Ile-", "Val-", "Leu-", "Phe-", "Cys-", "Met-",
                           "Ala-", "Gly-", "Thr-", "Ser-", "Trp-", "Tyr-",
                           "Pro-", "Gln-", "Asn-", "Asp-", "Glu-", "His-",
                           "Lys-", "Arg-" };
    
    float      hpi[20] = { 4.5, 4.2, 3.8, 2.8, 2.5, 1.9,
                           1.8, -0.4, -0.7, -0.8, -0.9, -1.3,
                           -1.6, -3.5, -3.5, -3.5, -3.5, -3.2,
                           -3.9, -4.5 };
    
    extern float   hp; /* Running count of the hydropathy. */
    
    
    /* That's all, folks... */
    Here's what happens when I try to compile and link:

    Code:
    127.0.0.1> gcc -o peptide residues.c peptide.c
    
    /tmp/cclu2H5r.o(.data+0x0): multiple definition of `residue'
    /tmp/ccU47JJi.o(.data+0x0): first defined here
    /tmp/cclu2H5r.o(.data+0x60): multiple definition of `hpi'
    /tmp/ccU47JJi.o(.data+0x60): first defined here
    /tmp/cclu2H5r.o(.text+0x0): In function `sequence':
    : multiple definition of `sequence'
    /tmp/ccU47JJi.o(.text+0x0): first defined here
    /tmp/ccU47JJi.o(.text+0x65): In function `sequence':
    : undefined reference to `hp'
    /tmp/ccU47JJi.o(.text+0x72): In function `sequence':
    : undefined reference to `hp'
    /tmp/cclu2H5r.o(.text+0x65): In function `sequence':
    : undefined reference to `hp'
    /tmp/cclu2H5r.o(.text+0x72): In function `sequence':
    : undefined reference to `hp'
    /tmp/cclu2H5r.o(.text+0x100): In function `main':
    : undefined reference to `hp'
    /tmp/cclu2H5r.o(.text+0x115): more undefined references to `hp' follow
    collect2: ld returned 1 exit status
    Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    1. You've got one source file including another.
    2. You've got a header file with definitions, not declarations.
    3. Your header files don't have include guards.

    4. char* seq = "NH2-"
    This assignment is lost when you do malloc

    5. seq = strcat(seq, residue[aa]);
    malloc doesn't initialise the memory, so who knows where the first append to string will go.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    you might new some more header files including in your header file such as string.h and perhaps Salem said you need a ifndef enclosing the header content so that they don't get initialized every time.

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Linking Files
    By MT_Pockets in forum C++ Programming
    Replies: 8
    Last Post: 02-24-2006, 07:50 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Problem linking files in C++
    By Feenaboccles in forum Linux Programming
    Replies: 3
    Last Post: 04-05-2002, 07:48 AM

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