如何使用在C头文件 [英] How to use a header file in c

查看:186
本文介绍了如何使用在C头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义的类MyStrLen.c的方法和实现它,我的头文件MyStrLen.h申报,什么我想是在另一个类MyStrCmp.c使用的方法从MyStrLen
但它显示在shell编译错误,当我试图使o文件。

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

MyStr.c

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

MyStrCmp.c

MyStrCmp.c

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

这就是编译错误

MyStrCmp.c :(文字+为0x18):未定义引用中InputLen
。MyStrCmp.c :(文字+ 0x29):未定义引用
中InputLen
。MyStrCmp.c :(文本将0x55 +):未定义引用中InputLen
。MyStrCmp.c :(文字+ 0x77):未定义引用
中InputLen

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'

推荐答案

右键,基本清单去如下:

Right, the basic checklist goes as follows:


  • 确实 MyStrCmp.c 包括 MyStr.h 文件:的#includemyStr的.H应该在文件的顶部(沿侧的#include<&stdio.h中GT; #包括<&stdlib.h中GT;

  • 确实 MyStr.c 做?我的意思是包括它自己的头文件(的#includeMyStr.h

  • 提到的3个文件( MyStrCmp.c MyStr.c myStr中.H )在同一目录?

  • 您传递两个 MyStrCmp.c 文件和 MyStr.c 文件与gcc?

  • 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?

如果回答这些问题的所有4个是肯定的,那么:

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

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

应该工作。因为这样,你已经写了中InputLen 功能(在 MyStr.c ),它写成一个文件可以在外部编译或separatly(的gcc -o MyStr.c ,以产生一个O形文件)。其结果是,连接已被明确地进行,由双方的源文件传递给编译器。顺便说一下,更多的细节可以在<一个中找到href=\"http://stackoverflow.com/questions/5559250/c-error-undefined-reference-to-function-but-it-is-defined\">this重复的问题

基本上,打开一个终端窗口,输入以下命令:

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

我使用Vim,你可以用你的preferred编辑器,但这是除了点。

MyStr.h 文件,键入:

int inputLen(char* myStr);

保存并关闭它,然后编辑 MyStr.c 文件,并定义你的实际功能:

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);
}

保存和放大器;关闭,然后编辑 MyStrCmp.c 文件,写类似:

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

这篇关于如何使用在C头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆