0

I defined a method in class MyStrLen.c and implemented it and i declared for it in the head file MyStrLen.h, what i wanted is to use a method from MyStrLen in another class MyStrCmp.c but it shows a compilation error in the shell when i try to make the o file.

MyStr.h

  int inputLen(char* myStr);

MyStr.c

int inputLen(char* myStr)
{
  ....
  ....
}

MyStrCmp.c

 #include "MyStr"
void method()
{
 inputLen(someinput)
}

and this is the compilation error

MyStrCmp.c:(.text+0x18): undefined reference to inputLen' MyStrCmp.c:(.text+0x29): undefined reference toinputLen' MyStrCmp.c:(.text+0x55): undefined reference to inputLen' MyStrCmp.c:(.text+0x77): undefined reference toinputLen'

2
  • And the compilation error is... ? Commented Nov 12, 2013 at 11:36
  • it looks like your error has more to do with the fact that you aren't linking one of the files. Commented Nov 13, 2013 at 7:27

4 Answers 4

2

Right, the basic checklist goes as follows:

  • Does MyStrCmp.c include the MyStr.h file: #include "MyStr.h" should be at the top of the file (along side #include <stdio.h> and #include <stdlib.h>)
  • Does MyStr.c do the same? By that I mean include its own header file (#include "MyStr.h")
  • Are the 3 files mentioned (MyStrCmp.c, MyStr.c and MyStr.h) in the same directory?
  • Are you passing both the MyStrCmp.c file and the MyStr.c file to gcc?

If the answer to all 4 of these questions is yes, then:

$ gcc -o MyStrCmp -Wall MyStrCmp.c MyStr.c -std=c99

Should work. Because of the way you've written the inputLen function (in MyStr.c), it's written as a file that can be compiled externally, or separatly (gcc -o MyStr.c, to produce an o-file). As a result, the linking has to be done explicitly, by passing both source files to the compiler. By the way, more details can be found in this duplicate question
Basically, open a terminal window, and enter the following commands:

$ mkdir test
$ cd test/
$ touch MyStr.c && touch MyStr.h && touch MyStrCmp.c
$ vim MyStr.c MyStr.h -O

I use Vim, you can use your preferred editor, but that's besides the point.
In the MyStr.h file, you type:

int inputLen(char* myStr);

Save and close it, then edit the MyStr.c file, and define your actual function:

#include "MyStr.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int inputLen(char *myStr)
{
    printf("%s\n", myStr);
    return strlen(myStr);
}

Save & close, then edit the MyStrCmp.c file, and write something like:

#include <stdio.h>
#include <stdlib.h>
#include "MyStr.h"
int main(int argc, char **argv )
{
    const char *test = "Some test-string";
    int l = 0;
    l = inputLen(test);
    printf("The printed string is %d long\n", l);
    return EXIT_SUCCESS;
}

Then compile with the command I provided above. This worked just fine for me...

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for showing the correct way to link files.
1

In MyStrCmp.c, put this at the top:

#include "MyStr.h"

4 Comments

i did that but still didn't work
If those are the only files you've got: gcc -Wall MyStr.c MyStrCmp.c -o programoutput. It looks like you didn't compile the MyStr.c file yet.
@somebody You did what? #include "MyStr" or #include "MyStr.h"? Also, are you developing on an OS where file names are case-sensitive?
@somebody: Where are the files in respect to your MyStrCmp.c file? are both the MyStr.h and MyStr.c file in the same directory?
1

Your "MyStr.h" should have this: extern int inputLen(char* myStr);

and your "MyStr.c" should have #include<MyStr.h> and your MyStrCmp.c should also have #include<MyStr.h>

provided that, all the headers and sources are in the same directory!

To avoid multiple inclusion confusions: use header guards

#ifndef MYSTR_H
#define MYSTR_H

extern int inputLen(char* myStr);

#endif

Comments

0

The answer may be that you have not used the right compilation command.

You must use the -c flag (if you are using gcc) to create a .o file, otherwise gcc will try to link the executable and without knowing about MyStr.c it will fail to find the inputLen function.

Try compiling Mystr.c and MyStrCmp.c separately using -c and then linking the .o files.

You mention in a comment that you "did compile it", but you must make sure the two files get combined by the linker.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.