我正在尝试使用this C++ code在我的Linux
框中编译g++
但它失败并出现以下错误:
enigma/Enigma# g++ -I . main.cpp -o main
In file included from machine.h:14:0,
from tests.h:13,
from main.cpp:10:
plug.h:13:2: warning: #import is a deprecated GCC extension [-Wdeprecated]
#import "util.h"
^~~~~~
/tmp/ccxyoEC2.o: In function `main':
main.cpp:(.text+0x10): undefined reference to `test_machine_encode_decode()'
collect2: error: ld returned 1 exit status
该错误表示编译器找不到同一文件夹中存在的tests.h文件。 如何编译和运行此代码?
我现在明白我需要将目标文件链接在一起,我这样做是使用:
g++ -c *.cpp
g++ *.o -o enig
它仍然不起作用,生成的二进制文件与./enig
一起执行但是被破坏并且不能按预期运行:
Entire encoded message: TZQA
Decoding now...
Entire decoded message: AHOJ
Entire encoded message: HBIU
Decoding now...
Entire decoded message: AHOJ
Entire encoded message: ZSNE
Decoding now...
Entire decoded message: AHOJ
Entire encoded message: ICRH
它只是对这些随机文本进行编码和解码,而不是我上面分享的git页面上提到的功能。
我缺少什么?
该错误表示编译器找不到同一文件夹中存在的tests.h文件。
不,它没有。实际上,编译器成功编译了main.cpp
。
该错误表明链接器找不到test_machine_encode_decode
。这并不奇怪,因为test_machine_encode_decode
在test.cpp
中定义。您必须链接main.cpp
和test.cpp
的目标文件才能获得完整的可执行文件。
如果查看实际代码,您会看到main只调用test_machine_encode_decode()
单元测试。您必须自己实现自述文件中的功能,或者搜索git历史记录并尝试查找程序实际上是否有效。