0

I am trying to use two .c files together. I am lost at how to do this, I have a simple setup for each file but I get a undefined reference to format_lines error when I try to compile. Any help would be muchly appreciated;

formatter.h

#ifndef _FORMATTER_H_
#define _FORMATTER_H_

#include <stdio.h>

char **format_file(FILE *);
char **format_lines(char **, int);
void test();


#endif

formatter.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "formatter.h"


char **format_file(FILE *infile) {
    return NULL;
}


char **format_lines(char **lines, int num_lines) {
     char **result = NULL;

#ifdef DEBUG
    result = (char **)malloc(sizeof(char *) * 2);
    if (result == NULL) {
        return NULL;
    }

    result[0] = (char *)malloc(sizeof(char) * 80);
    if (result[0] == NULL) {
        return NULL;
     }
    strncpy(result[0], "(machine-like voice) EXTERMINATE THEM!", 79);

    result[1] = (char *)malloc(sizeof(char) * 2);
    if (result[1] == NULL) {
        return NULL;
    }
    result[1][0] = '\0';
 #endif
 }
 void test(){
      print("here");
 }

and sengfmt.c

 #include <stdio.h>
 #include <stdlib.h>
 #include "formatter.h"

 int main(int argc, char *argv[]) {
 test();

#ifdef DEBUG
    printf("%s does nothing right now.\n", argv[0]);
#endif
    exit(0);
}

When I try to compile, I just type this.

    $ gcc sengfmt3.c
/tmp/cc7Ttgne.o: In function `main':
sengfmt3.c:(.text+0x15): undefined reference to `test'
collect2: ld returned 1 exit status
  • 3
    show us the compile command(s) and the error – pm100 Jul 29 '14 at 23:35
  • 1
    How do you compile? Also note, that ‘_’, followed by an uppercase letter, is reserved. Use `#define FORMATTER_H_`. – mafso Jul 29 '14 at 23:35
  • [Don't cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Jul 29 '14 at 23:37
  • 1
    When posting a question about build errors, please always include the actual (in complete and unedited form) in the question. Please edit your question to include them. – Some programmer dude Jul 29 '14 at 23:40
  • 1
    Another tip about asking questions: Don't edit your question to add a comment. Regarding that comment, how did you get the build error you mention in the first place? Just repeat the process to get the error, and copy-paste it into your question. – Some programmer dude Jul 29 '14 at 23:57
  • no u dont have to use a makefile. We dont know what platform you are on or what compiler you are using; the 'how to compile' question has different answers depending on what computer you are using – pm100 Jul 30 '14 at 00:36
  • you said "but I get a undefined reference to format_lines error when I try to compile.". Do it again and show us what you did and what the exact message was - cut and paste it – pm100 Jul 30 '14 at 00:38

2 Answers2

0

I suspect that your main used to try to call format_lines

You need to do this

gcc formatter.c sendgfmt.c -o myprog

You must list all the c files that you want compiled together

pm100
  • 48,078
  • 23
  • 82
  • 145
0

If you have code in multiple source files, then you need to build with all the source files.

There are two ways of doing this:

  1. Compile and link all source files using using one command:

    $ gcc sengfmt3.c someOtherSourceFile.c someThirdSourceFile.c
    
  2. First make object files of all source files, and then link the object files together. This is more work, but if you have a makefile or other build-system it will be better since only the modified source files will be recompiled, and might save you some build-time:

    $ gcc -c sengfmt3.c
    $ gcc -c someOtherSourceFile.c
    $ gcc -c someThirdSourceFile.c
    $ gcc sengfmt.o someOtherSourceFile.o someThirdSorceFile.o
    

    Note the command-line option -c for the compilation, this tells GCC to generate object files. Also note that for the linking command (the last one) the file extensions have changed from .c to .o.

    The command in point 1 does this internally, using temporary files which are removed when done.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • If I make a makefile would this accomplish this same goal? – user3777576 Jul 30 '14 at 01:04
  • 1
    @user3777576 If you only have a couple of small source files, you might as well build them manually as in my first point. If you get more that a couple of source files, then having e.g. a makefile will probably help you. What you do in a makefile should be what I do in point 2, each source file is compiled separately to an object file, and then in a separate step linked together to form the final executable program. – Some programmer dude Jul 30 '14 at 01:14