如何在Linux上使用相关的头文件编译这个C ++代码?

问题描述 投票:-2回答:2

我正在尝试使用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页面上提到的功能。

我缺少什么?

c++ linux compiler-errors g++
2个回答
1
投票

该错误表示编译器找不到同一文件夹中存在的tests.h文件。

不,它没有。实际上,编译器成功编译了main.cpp

该错误表明链接器找不到test_machine_encode_decode。这并不奇怪,因为test_machine_encode_decodetest.cpp中定义。您必须链接main.cpptest.cpp的目标文件才能获得完整的可执行文件。


0
投票

如果查看实际代码,您会看到main只调用test_machine_encode_decode()单元测试。您必须自己实现自述文件中的功能,或者搜索git历史记录并尝试查找程序实际上是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.