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...