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

Make header file, compile them and run in C

我有一个任务。我创建了八个功能,这些功能完美无缺。我对 4.step

有疑问

在说明中我有 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
  1. 然后我必须创建 textFunc.c,其中我有这些函数的 定义。 (您不需要阅读整个程序)。
#include <stdio.h>
#include <stdlib.h>

int _strlen (char *tab)
{
    int i;
    for (i = 0; tab[i] != '[=12=]'; ++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] = '[=12=]';
    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] != '[=12=]'; ++i)
        {
            dokad[i] = skad[i];
        }
        dokad[i] = '[=12=]';
    }
    return _strlen(dokad);
}

int allSigns(char text[])
{

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

int notWhiteSigns(char text[])
{
    int sum = 0;
    for (int i = 0; text[i] != '[=12=]'; ++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] != '[=12=]'; ++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] != '[=12=]'; ++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;
}

然后我必须用 main 函数创建程序 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;
}
  1. 然后他们说 "Compile each source file (with a .c extension) and then link them together, e.g"
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,但我总是必须在每个程序中放入主函数?

好的,我有 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: The is nothing to do in '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