简体   繁体   English

C Makefile未定义引用错误

[英]C makefile undefined-reference error

I am having slight problems with using makefile in C. Ive been following a tutorial in a textbook, but it doesnt seem to want to work. 我在C中使用makefile时遇到一些小问题。我一直在遵循教科书中的教程,但它似乎并不想工作。 I have three files, message_hider.c, encrypt.h and encrypt.c. 我有三个文件,message_hider.c,encrypt.h和encrypt.c。 When I create a makefile for these files it returns an error, but when I run each command individually it works just fine. 当我为这些文件创建一个makefile时,它返回一个错误,但是当我单独运行每个命令时,它就可以正常工作。 Here are my files. 这是我的文件。

encrypt.c 加密

#include "encrypt.h"

void encrypt(char *message) {
char c;
while (*message) {
*message = *message ^ 31;
message++;
}
}

message_hider.c message_hider.c

#include <stdio.h>
#include "encrypt.h"

int main() {
char msg[80];
while (fgets(msg, 80, stdin)) {
encrypt(msg);
printf("%s", msg);
}
}

encrypt.h 加密

void encrypt(char *message);

Makefile 生成文件

message_hider: message_hider.o encrypt.o
    gcc message_hider.o encrypt.o -o message_hider
message_hider.o: message_hider.c encrypt.h
    gcc -c message_hider.c
encrypt.o: encrypt.c encrypt.h
    gcc -c encrypt.c

Error message 错误信息

$ make message_hider
cc   message_hider.o   -o message_hider
message_hider.o:message_hider.c:(.text+0x17): undefined reference to `encrypt'
message_hider.o:message_hider.c:(.text+0x17): relocation truncated to fit: R_X86_64_PC32       against undefined symbol `encrypt'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../x86_64-pc-cygwin/bin/ld:     message_hider.o: bad reloc address 0x0 in section `.pdata'
/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../x86_64-pc-cygwin/bin/ld: final link    failed: Invalid operation
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'message_hider' failed
make: *** [message_hider] Error 1
$ make message_hider
cc   message_hider.o   -o message_hider

That is not the rule you've specified in your makefile. 不是您在makefile中指定的规则。 First off, it appears to be using cc rather than gcc . 首先,它似乎正在使用cc而不是gcc Second, there's no mention of encrypt.o in there which is why your link is failing. 其次,这里没有提到encrypt.o ,这就是链接失败的原因。

Try to explicitly use the makefile, such as with: 尝试显式使用makefile,例如:

make -f Makefile message_hider

It may be that it's picking up a different makefile, one that either has different rules or one that simply relies on the default rules like .co . 可能是因为它选择了一个不同的makefile,一个具有不同规则,或者仅依赖于.co这样的默认规则。


And, based on your update that: 并且,根据您的更新,您可以:

make -f Makefile message_hider

gives you: 给你:

$ make -f Makefile message_hider
make: Makefile: No such file or directory
make: *** No rule to make target 'Makefile'. Stop.

that's the error you get when Makefile does not actually exist. 那就是当Makefile实际上不存在时得到的错误。

So you need to check that it's in the current directory, and named exactly as you expect. 因此,您需要检查它是否在当前目录中,并按照您的期望正确命名。

That would explain the use of default rules as mentioned earlier, since your makefile isn't actually being picked up. 这将解释如前所述使用默认规则的原因,因为实际上并没有拾取您的makefile。

Another thing to check, though it's probably moot now that we've seen the error above, is that you're actually running the correct make program. 另一件事要检查,尽管现在我们已经看到上述错误,现在可能尚无定论,但实际上您正在运行正确的make程序。 Use which make to find out where it is (should be /usr/bin/make on CygWin) and make --version to check the version. 使用which make找出它的位置(在CygWin上应该是/usr/bin/make ),并make --version检查版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM