gpt4 book ai didi

c - 制作头文件,编译它们并在C中运行

转载 作者:行者123 更新时间:2023-12-02 10:50:56 27 4
gpt4 key购买 nike

我有任务我创建了八个功能,这些功能可以完美地工作。我对4.步骤有疑问

在说明中我有4个步骤
1.我需要创建头文件,在其中必须将所有功能的声明放入:(已编辑)
头文件的名称为 textFunc.h


#define _RNM_HPP

int _strlen (char *tab);

int writeText(FILE *wp, char text[]);
int readText(FILE *wp, char text[], int max);
int copyText(char skad[],char dokad[],int max);

int allSigns(char text[]);
int notWhiteSigns(char text[]);
int words(char text[]);
int lines(char text[]);

#endif
  • 然后,我必须创建 textFunc.c ,其中我具有这些函数的定义。 (您不需要阅读整个程序)。
  • #include <stdio.h>
    #include <stdlib.h>

    int _strlen (char *tab)
    {
    int i;
    for (i = 0; tab[i] != '\0'; ++i);
    return i;
    }

    int writeText(FILE *wp, char text[])
    {
    int len = _strlen(text);
    for (int i = 0; i < len; i++) {
    fputc (text[i], wp);
    }
    return _strlen(text);
    }

    int readText(FILE *wp, char text[], int max)
    {
    int length = _strlen(text);
    int c, i;
    max = 1000;
    while( (c = fgetc(wp)) != EOF && i <= max )
    {
    fputc (text[i], wp);
    i++;
    }
    text[i+1] = '\0';
    if (i < max)
    printf("Array is too big.");
    return _strlen(text);
    }

    int copyText(char skad[],char dokad[],int max)
    {
    if (_strlen(skad) <= max)
    {
    int i;
    for (i = 0; skad[i] != '\0'; ++i)
    {
    dokad[i] = skad[i];
    }
    dokad[i] = '\0';
    }
    return _strlen(dokad);
    }

    int allSigns(char text[])
    {

    int i;
    for (i = 0; text[i] != '\0'; ++i);
    return i;
    }

    int notWhiteSigns(char text[])
    {
    int sum = 0;
    for (int i = 0; text[i] != '\0'; ++i)
    {
    if ((text[i] != ' ') && (text[i] != '\n') && (text[i] != '\t') && (text[i] != '\v'))
    sum++;
    }
    return sum;
    }

    int words(char text[])
    {
    int i;
    int sum = 0;
    for (int i = 1; text[i] != '\0'; ++i)
    {
    if (text[i] == ' ' && text[i-1] != ' ' && (text[i] != '\n') && (text[i] != '\t') && (text[i] != '\v') && (text[i-1] != '\n') && (text[i-1] != '\t') && (text[i-1] != '\v'))
    sum++;
    }
    if (text[i-1] != ' ');
    sum++;
    return sum;
    }

    int lines(char text[])
    {
    int i;
    int sum = 1;
    for (int i = 0; text[i] != '\0'; ++i)
    {
    if (text[i] == '\n')
    sum++;
    }
    return sum;
    }


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

    char sentence[] = "C is \n a \n programming \t language";

    printf("Show array: %s \n", sentence);

    printf("All signs: %i\n not white signs: %i\n words: %i\n lines: %i\n", allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));


    return 0;
    }

    然后,我必须使用主要功能 mainProgram.c 创建程序
    #include <stdio.h>
    #include <stdlib.h>
    #include "funTabTekst.h"

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

    char sentence[] = "C is \n a \n programming \t language";

    printf("Show array: %s \n", sentence);

    printf("All signs: %i\n not white signs: %i\n words: %i\n lines: %i\n", allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));


    return 0;
    }
  • 然后他们说:“编译每个源文件(带有.c扩展名),然后将它们链接在一起,例如。”
  • gcc -o mainProgram.x mainProgram.o textFunc.o

    我对4.步骤有疑问。我无法编译我的源文件
    我尝试
    gcc -o textFunc.o textFunc.c

    我得到一个错误
    /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
    (.text+0x20): undefined reference to `main'
    collect2: error: ld returned 1 exit status

    我没有主要功能,所以我将它们添加到textFunc.c
    int main()
    {
    return 0;
    }

    编译没有错误,我有textFunc.o,但是我总是必须在每个程序中都放置main函数吗?

    好的,我现在有textFunc.o我需要mainProgram.o

    所以我尝试
    gcc -o mainProgram.o mainProgram.c
    但我得到一个错误
    gcc -o  mainProgram.o mainProgram.c 
    mainProgram.c: In function ‘main’:
    mainProgram.c:12:86: warning: passing argument 1 of ‘allSigns’ makes integer from pointer without a cast [-Wint-conversion]
    igns: %i\n not white signs: %i\n words: %i\n lines: %i\n", allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));
    ^~~~~~~~
    In file included from mainProgram.c:3:0:
    textFunc.h:8:5: note: expected ‘char’ but argument is of type ‘char *’
    int allSigns(char);
    ^~~~~~~~
    mainProgram.c:12:111: warning: passing argument 1 of ‘notWhiteSigns’ makes integer from pointer without a cast [-Wint-conversion]
    s: %i\n words: %i\n lines: %i\n", allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));
    ^~~~~~~~
    In file included from mainProgram.c:3:0:
    textFunc.h:9:5: note: expected ‘char’ but argument is of type ‘char *’
    int notWhiteSigns(char);
    ^~~~~~~~~~~~~
    mainProgram.c:12:128: warning: passing argument 1 of ‘words’ makes integer from pointer without a cast [-Wint-conversion]
    \n lines: %i\n", allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));
    ^~~~~~~~
    In file included from mainProgram.c:3:0:
    textFunc.h:10:5: note: expected ‘char’ but argument is of type ‘char *’
    int words(char);
    ^~~~~
    mainProgram.c:12:145: warning: passing argument 1 of ‘lines’ makes integer from pointer without a cast [-Wint-conversion]
    allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));
    ^~~~~~~~
    In file included from mainProgram.c:3:0:
    textFunc.h:11:5: note: expected ‘char’ but argument is of type ‘char *’
    int lines(char);
    ^~~~~
    /tmp/cc2xAejC.o: In function `main':
    mainProgram.c:(.text+0x83): undefined reference to `lines'
    mainProgram.c:(.text+0x94): undefined reference to `words'
    mainProgram.c:(.text+0xa5): undefined reference to `notWhiteSigns'
    mainProgram.c:(.text+0xb5): undefined reference to `allSigns'
    collect2: error: ld returned 1 exit status

    编辑2

    我尝试在我的终端上写 gcc mainProgram.c textFunc.c -o my_program我得到一个错误
    mainProgram.c: In function ‘main’:
    mainProgram.c:12:77: warning: implicit declaration of function ‘allSigns’ [-Wimplicit-function-declaration]
    tf("All signs: %i\n not white signs: %i\n words: %i\n lines: %i\n", allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));
    ^~~~~~~~
    mainProgram.c:12:97: warning: implicit declaration of function ‘notWhiteSigns’ [-Wimplicit-function-declaration]
    not white signs: %i\n words: %i\n lines: %i\n", allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));
    ^~~~~~~~~~~~~
    mainProgram.c:12:122: warning: implicit declaration of function ‘words’ [-Wimplicit-function-declaration]
    ds: %i\n lines: %i\n", allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));
    ^~~~~
    mainProgram.c:12:139: warning: implicit declaration of function ‘lines’; did you mean ‘linie’? [-Wimplicit-function-declaration]
    i\n", allSigns(sentence), notWhiteSigns(sentence), words(sentence), lines(sentence));
    ^~~~~
    linie
    /tmp/ccQteDqR.o: In function `main':
    textFunc.c:(.text+0x3b6): multiple definition of `main'
    /tmp/cc0VFNnF.o:mainProgram.c:(.text+0x0): first defined here
    collect2: error: ld returned 1 exit status

    然后我尝试写另一种方法,创建makefile newMakeFile
    my_program: textFunc.o mainProgram.o
    gcc -o my_program textFunc.o mainProgram.o

    textFunc.o: textFunc.c
    gcc -o textFunc.o -c textFunc.c

    mainProgram.o: mainProgram.c textFunc.h
    gcc -o mainProgram.o -c mainProgram.c

    然后我得到“make:在'newMakeFile'中不做任何事情。

    最佳答案

    编译代码的最简单方法是:
    gcc mainProgram.c textFunc.c -o my_program
    您还必须更新textFunc.h中的函数定义,使其与textFunc.c中的相同。

    #ifndef _RNM_HPP

    #define _RNM_HPP

    int _strlen (char *tab);

    int writeText(FILE *wp, char text[]);
    int readText(FILE *wp, char text[], int max);
    int copyText(char skad[],char dokad[],int max);

    int allSigns(char text[]);
    int notWhiteSigns(char text[]);
    int words(char text[]);
    int lines(char text[]);

    #endif

    如果要先编译到file.o。请参阅下面的简单Makefile:
    my_program: textFunc.o mainProgram.o
    gcc -o my_program textFunc.o mainProgram.o

    textFunc.o: textFunc.c
    gcc -o textFunc.o -c textFunc.c

    mainProgram.o: mainProgram.c textFunc.h
    gcc -o mainProgram.o -c mainProgram.c

    关于c - 制作头文件,编译它们并在C中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61016926/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com