5

I've tried more and more time, but I got the same error. When I use an external lib, for example I'm try to work on openssl and crypto++, I receive the error: "undefined reference to (function)".

I dunno why Eclipse do that neverthless the #include are all correct. I've tried even to add the path from Properties-C/C++ General-Path & Symbols but nothing.

Can anybody help me?

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • 2
    this is why I think newcomers in programming should start with a plain editor and the basic toolchain (compiler + build system). – akappa Oct 22 '11 at 16:07

2 Answers2

18

You must specify the name of the shared libraries which must be linked to the executable.

Assuming you are using the GNU toolchain, you can do it by following these steps:

  1. Right-click on the project, then select Properties
  2. Go under C/C++ Build -> Settings
  3. Select GCC C Linker -> Libraries
  4. In Libraries (-l), add the name of your libraries,
  5. If needed, put the directory where your libraries are hosted in Library search path (-L).
akappa
  • 10,220
  • 3
  • 39
  • 56
  • I've done: Right click on Properties-C/C++ Build-Settings-Tool Settings-Cross G++ Linker. And now where I have to put the -l command? –  Oct 22 '11 at 16:00
  • There is a small "+" icon near "Libraries (-l)". – akappa Oct 22 '11 at 16:03
  • Ok I did it, but I got this error: /usr/bin/ld: cannot find -lopenssl Why I'm wrong? –  Oct 22 '11 at 16:06
  • 2
    because the library isn't named that way. Try with -lssl – akappa Oct 22 '11 at 16:07
  • (if you are on an unix system, one easy way to understand what's the name of the library is to go under `/usr/lib` and search your library there. Libraries are names libX.so[.*], like libssl.so.0.9.8. The name of the library is X, ssl in this case. – akappa Oct 22 '11 at 16:10
  • Perfect it works! One more question. How can I find the right name for other libs? In this case openssl=ssl, but for other lib there is a fast way to know the right name? Thank you! –  Oct 22 '11 at 16:13
  • see my comment before yours ;) – akappa Oct 22 '11 at 16:14
  • 1
    I am using Eclipse Juno with Windows7 and facing same issue while using CURL library. How can I solve this problem? – AndroidDev Nov 19 '12 at 06:52
0

I have the same problem too. I was using Eclipse CDT and trying to build my source code with OpenSSL headers and I got the same "undefined reference" problem.

To those who may be also suffering from this type of errors, try these steps:

  1. Make sure you use the right compiler(for Ubuntu 18.04) right click your project->preference->C/C++ Build->tool chain editor->use CDT internal Builder / Linux gcc(you can try to run HelloWorld as a test)
  2. C/C++ Build->settings->gcc linker, see right there is a "+", click and type ssl and crypto
  3. Rebuild your project, done.

I strongly recommend you to learn gcc compiler command line. If you have compile problem, always use the terminal and command line to see if you can compile it successfully, then compare the succeeded command with the console log in Eclipse, at where you can see the actual gcc command that used to compile your code.

A good resource for gcc command: https://www.rapidtables.com/code/linux/gcc/gcc-l.html