如何使用相关标头文件在Linux上编译此C 代码

How to compile this C++ code on Linux with relevant header files?

本文关键字:Linux 编译 代码 文件 何使用      更新时间:2023-10-16

我试图使用g++在我的Linux框中编译此C 代码,但它在以下错误时失败:

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_decodetest.cpp中定义。您必须链接main.cpptest.cpp的对象文件才能获得完整的可执行文件。

如果您查看实际代码,您会看到MAIM仅调用test_machine_encode_decode()单位检验。您必须自己实现Readme的功能,或者您搜索GIT历史记录并尝试找出该程序是否过去是否有效。