Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么即使我定义了我正在使用的命令,我仍会得到对另一个文件的未定义引用?_C_Gcc_Makefile_Linker_Header Files - Fatal编程技术网

C 为什么即使我定义了我正在使用的命令,我仍会得到对另一个文件的未定义引用?

C 为什么即使我定义了我正在使用的命令,我仍会得到对另一个文件的未定义引用?,c,gcc,makefile,linker,header-files,C,Gcc,Makefile,Linker,Header Files,我正在用C写一个项目,虽然我已经有一年没有用C写代码了,我相信我犯的只是一个简单的错误,但我不确定我到底应该用谷歌帮我做什么,所以我决定来这里。我有一个名为driver.c的简单文件,如下所示: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <signal.h> #include "printHello.h" #define MAXLIN

我正在用C写一个项目,虽然我已经有一年没有用C写代码了,我相信我犯的只是一个简单的错误,但我不确定我到底应该用谷歌帮我做什么,所以我决定来这里。我有一个名为driver.c的简单文件,如下所示:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include "printHello.h"

#define MAXLINE 4096

int main(int argc, char **argv)
{
    char bfr[MAXLINE];
    fputs("$ ", stderr);

    while (fgets(bfr, MAXLINE, stdin) != NULL)
    {
        bfr[strlen(bfr) - 1] = '\0'; //replaces newline with NULL term. char
        if (bfr[0] != '\0')
        {

            printHello();
            //parseCommand(bfr); - this will be used eventually but tested with printHello()
        }
        fputs("$ ", stderr);
    }
    exit(0);
}
#Define the gcc options
CFLAGS = -Wall -std=c99

#Define the names of the object modules
OBJS = printHello.o driver.o

#Define a pattern for building an object file from a C file.  Note:  The
#variable $< expands to the name of the first prerequisite.
%.o : %.c
    gcc $(CFLAGS) -c -o $@ $<

#Define the default rule
all: driver

#Define a rule for building the executable.  Note:  Unlike $?, the $^ variable
#expands to all the prerequisites required to build the target.
driver: driver.o
    gcc -o $@ $^

#Define a rule to clean-up the mess.  Note:  Projects don't agree on whether
#to delete the library.  The rule below deletes it.
clean:
    rm -f *.o *.a driver *~

在red hat linux计算机上出现以下错误:

driver.o: In function `main':
driver.c:(.text+0x3c): undefined reference to `printHello'
collect2: error: ld returned 1 exit status
make: *** [driver] Error 1
现在,它看起来好像看到printHello不存在,但是它确实存在于这两个版本中

printHello.h:

void printHello();
和printHello.c:

#include <stdio.h>

void printHello(){
    printf("hello");
}
#包括
void printHello(){
printf(“你好”);
}
有人知道为什么会这样吗?我在这个项目上有很多事情要做,所有这些都相当容易,但是我不能开始,直到我可以简单地运行我的程序

注意:每当我使用make编译时,就会创建一个名为“-Wno objc signed char bool implicit int conversion”的文件

我的make文件如下所示:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include "printHello.h"

#define MAXLINE 4096

int main(int argc, char **argv)
{
    char bfr[MAXLINE];
    fputs("$ ", stderr);

    while (fgets(bfr, MAXLINE, stdin) != NULL)
    {
        bfr[strlen(bfr) - 1] = '\0'; //replaces newline with NULL term. char
        if (bfr[0] != '\0')
        {

            printHello();
            //parseCommand(bfr); - this will be used eventually but tested with printHello()
        }
        fputs("$ ", stderr);
    }
    exit(0);
}
#Define the gcc options
CFLAGS = -Wall -std=c99

#Define the names of the object modules
OBJS = printHello.o driver.o

#Define a pattern for building an object file from a C file.  Note:  The
#variable $< expands to the name of the first prerequisite.
%.o : %.c
    gcc $(CFLAGS) -c -o $@ $<

#Define the default rule
all: driver

#Define a rule for building the executable.  Note:  Unlike $?, the $^ variable
#expands to all the prerequisites required to build the target.
driver: driver.o
    gcc -o $@ $^

#Define a rule to clean-up the mess.  Note:  Projects don't agree on whether
#to delete the library.  The rule below deletes it.
clean:
    rm -f *.o *.a driver *~

#定义gcc选项
CFLAGS=-Wall-std=c99
#定义对象模块的名称
OBJS=printHello.o driver.o
#定义从C文件构建对象文件的模式。注:该
#变量$<扩展为第一个先决条件的名称。
%.o:%.c
gcc$(CFLAGS)-c-o$@$<
#定义默认规则
所有:司机
#定义用于构建可执行文件的规则。注意:$^变量不同于$
#扩展到构建目标所需的所有先决条件。
司机:司机
gcc-o$@$^
#定义一个规则来清理混乱。注:项目不同意是否
#删除该库。下面的规则将删除它。
清洁:
rm-f*.o*.a驱动程序*~

任何帮助将不胜感激,和往常一样,请让我知道如果你需要更多的信息

driver:driver.o
替换为makefile中的
driver:${OBJS}
为了使可执行文件依赖于所有对象文件(.o)

现在,makefile告诉我们只需要
driver.o

要构建
驱动程序
,因此链接中缺少一些函数。

驱动程序:$(OBJS)
?是@安蒂哈帕拉