-2

I am trying to compile this C++ code in my Linux box using g++ but it fails with the following error:

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

The error indicates that the compiler cannot find the tests.h file present in the same folder. How can I compile and run this code?

I now understand that I needed to link the object files together, I did so using:

g++ -c *.cpp
g++ *.o -o enig

It still does not work though, the resulting binary executes with ./enig but is broken and does not function as intended:

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

It just keeps encoding and decoding those random texts as opposed to the functionality mentioned on the git page I shared above.

Anything I'm missing?

learnerX
  • 1,022
  • 1
  • 18
  • 44
  • "_The error indicates that the compiler cannot find the tests.h_" No it doesn't. The **warning** indicates that `#import` is a deprecated extension. The **error** indicates that the **linker** can't find the **definition** of the function. And judging from command line you are using to compile your program - you aren't linking with the file containing the definition. – Algirdas Preidžius Dec 16 '17 at 12:36
  • 3
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Algirdas Preidžius Dec 16 '17 at 12:37
  • You've edited your question to ask something completely different, making the current answers useless. Please revert it and ask a separate question if you need to. – mbrig Dec 18 '17 at 17:27

2 Answers2

1

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

No, it doesn't. In fact, the compiler successfully compiled main.cpp.

The error indicates that the linker cannot find test_machine_encode_decode. This is hardly surprising, since test_machine_encode_decode is defined in test.cpp. You have to link the object files of main.cpp and test.cpp to get a complete executable.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

If you look at the actual code, you'll see that main only calls the test_machine_encode_decode() Unit-test. You'll have to implement the functionality from the readme yourself or you search through the git history and try to find out, if the program actually worked in the past.

Nj0rd
  • 62
  • 1
  • 3