gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:42:21 25 4
gpt4 key购买 nike

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

我缺少什么吗?

最佳答案

The error indicates that the compiler cannot find the tests.h file present in the same folder.

不,事实并非如此。事实上,编译器成功编译了main.cpp

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

关于c++ - 如何在Linux上使用相关头文件编译这段C++代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47845721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com