0

When i build my project (gnu g cc, -std=c++11) in Code::BLocks 16.01 on a Win10 x64 Machine, header files are included in the project, i get the following errors:

path..\src\Main.cpp|77|undefined reference to `Snmp_pp::UdpAddress::UdpAddress(char const*)'|

path..\snmp_pp\address.h|574|undefined reference to `vtable for Snmp_pp::UdpAddress'|

And i get a lot of other undefined reference errors.

Here are the parts of my code, there are a lot of commented lines, i skipped them.

main.cpp:

#include <stdio.h>
#include "libsnmp.h"
#include "snmp_pp/snmp_pp.h"
using namespace Snmp_pp;

int main (){
  long rc;
  char buf [256];
  char const* ip_address;
  ip_address = "192.168.127.250";


  Snmp socket_startup();


  //Socket Informationen
   //Here comes line 77***************************
   UdpAddress udp_address(ipaddr);
   snmp_version version = version1;
   int retries = 1;
   int timeout = 100;
   u_short port = 161;
   OctetStr community ("public");

   //SNMP Session öffnen
   int status;

   Snmp snmp(status, 0,(udp_address.get_ip_version()==Address::version_ipv4));
     //SNMP Header Variablen ASN.1 encoding
     Pdu pdu;
     Vb  vb;

      //Erstelle OID Objekte
     Oid oid("1.3.6.1.2.1.1.1.0"); //sysDescr
     vb.set_oid(oid);
     pdu+= vb;
     **Here comes Line 100**
     udp_address.set_port(port);
     **Here comes Line 102**
     CTarget ctarget(udp_address);
     ctartget.set_version(version);
     ctartget.set_retry(retries);
     ctartget.set_timeout(timeout);
     ctartget.set_readcommunity(community);

     SnmpTarget *target;

     target = &ctartget;

     status = snmp.get(pdu, *target);

address.h Here is the UdpAddress Class defined, this is a part of the code

 //------------------------------------------------------------------------
 //---------[ UDP Address Class ]------------------------------------------
 //------------------------------------------------------------------------
 class DLLOPT UdpAddress : public IpAddress
 {
  public:
   /**
    * Construct an empty invalid UDP address.
    */
   UdpAddress();

   /**
    * Construct an UDP address from a string.
    *
    * The following formats can be used additional to those recognized by
    * IpAdress:
    * - Port added to IPv4 address with '/' or ':'
    *   ("192.168.17.1:161", "192.168.17.1/161", "printsrv/161")
    * - Port added to IPv6 address with '/' or using '[...]:'
    *   ("::1/162", "[::1]/162", "[::1]:162")
    *
    * @param inaddr - Hostname or IP address
    */
   UdpAddress(const char *inaddr);

   /**
    * Construct an UDP address from another UDP address.
    *
    * @param udpaddr - address to copy
    */
   UdpAddress(const UdpAddress &udpaddr);

   /**
    * Construct an UDP address from a GenAddress.
    *
    * @param genaddr - address to copy
    */
   UdpAddress(const GenAddress &genaddr);

   /**
    * Construct an UDP address from a IP address.
    * The port will be set to 0.
    *
   * @param ipaddr - address to copy
    */
   UdpAddress(const IpAddress &ipaddr);

 /**
   * Return the IP version of the address.
   *
   * @return one of Address::version_type
    */
   virtual version_type get_ip_version() const { return ip_version; }

   /**
    * Construct an UDP address from a GenAddress.
    *
    * @param genaddr - address to copy
    */
     UdpAddress(const GenAddress &genaddr);

   /**
    * Construct an UDP address from a IP address.
    * The port will be set to 0.
    *
    * @param ipaddr - address to copy
    */
   UdpAddress(const IpAddress &ipaddr);

   /**
    * Destructor (ensure that SnmpSyntax::~SnmpSyntax() is overridden).
    */
   ~UdpAddress() {}

The included header files are from the company HP from the SNMP++3.3.7 project
Link to Page
My folder structure is:

main_dir\src\main.cpp   
main_dir\libsnmp.h   
main_dir\snmp_pp\all other header files  

Heres my build output:

  g++.exe -Wall -std=c++11 -g -std=c++11 -I"C:\Users\Kneringer Georg\Documents\CodeBlocks\SNMP_ZIM" -I"C:\Users\Kneringer Georg\Documents\CodeBlocks\SNMP_ZIM\snmp_pp" -c "C:\Users\Kneringer Georg\Documents\CodeBlocks\SNMP_ZIM\src\Main.cpp" -o obj\Debug\src\Main.o

I need help with understanding what i did wrong. I guess that it is a Linker Error.

Eggi
  • 170
  • 1
  • 15
  • [This question](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix?rq=1), found on the right side of the very page you'e looking at now, likely describes what you're seeing and tactics for addressing it. – WhozCraig Sep 08 '16 at 07:20
  • I had a look into this, do you know if it makes a difference when the -L command is before the -o commands? Currently it looks like this: mingw32-g++.exe -L"" -o bin\Debug\.exe obj\Debug\src\Main.o -L -Lsnmp_pp\ "full paths to header files" – Eggi Sep 08 '16 at 07:29
  • and as far as i can read the Main.o there is an entry with UdpAddress. – Eggi Sep 08 '16 at 07:33
  • First, your exact, and *complete* link line should be part of your question. Second, it shouldn't matter, but ideally you at least want all your `-Lpath-to-libs` ganged together. Third, from what I can see by what you provided, you're telling the linker where the header files are; not where the libsnmp_pp.lib file (or whatever it is called) is. Finally, you're not actually providing the `-lsnmp_pp` (again, whatever it is called, but lower-case `-l` is important) on your link line regardless, so even if you told the linker where to look for libs, you never told it the lib to look for. – WhozCraig Sep 08 '16 at 07:35
  • And the "entry" you see in `Main.o` is likely an external reference that needs resolving at link time (which is precisely what is *not* happening, and thus why you're here). – WhozCraig Sep 08 '16 at 07:40
  • Ah guys, i am sorry, i mixed up libs and header files. I added header files to library search path... I removed them now, still no reference found. – Eggi Sep 08 '16 at 07:55
  • And *again* we still don't know the *full, complete, and **exact** link line* being executed on your final build step. It should be in the console right above the failure messages. I will all-but-guarantee the library for snmp++ isn't being linked with your project. That's the cure The linker needs to know (a) what it is, and (b) where to look to find it. Both must be provided on your link line. Can't say it much simpler than that. – WhozCraig Sep 08 '16 at 07:59
  • g++.exe -Wall -std=c++11 -g -std=c++11 -I"C:\Users\Kneringer Georg\Documents\CodeBlocks\SNMP_ZIM" -I"C:\Users\Kneringer Georg\Documents\CodeBlocks\SNMP_ZIM\snmp_pp" -c "C:\Users\Kneringer Georg\Documents\CodeBlocks\SNMP_ZIM\src\Main.cpp" -o obj\Debug\src\Main.o – Eggi Sep 08 '16 at 08:04
  • That isn't the link line, its a single-file build line for your `Main.cpp` (the `-c` flag is a dead giveaway for that). Anyway, hope you figure it out. off to bed. – WhozCraig Sep 08 '16 at 08:05

1 Answers1

2

In every library (almost), we have two main components:

  • Header files containing the declaration of the public functions within the library
  • Implementation files where the functions are defined

By including the header files, you tell the compiler, I have these functions at my disposal, you can use them. During the linking phase, the linker will try to find the implementation for these functions, but it doesn't find them that's why you have that error.

In order to fix the error, you need to configure the linker path in your IDE to tell him this is the lib containing the functions.

Mansuro
  • 4,558
  • 4
  • 36
  • 76
  • 1
    @Eggi if you have those errors, it means the linker is not finding the required implementation file/lib, so you need to recheck your linker settings – Mansuro Sep 08 '16 at 07:24
  • 1
    "In **every** library" is slightly too strong, since you can write header-only libraries as well. Of course, such libraries are unlikely to fail to link because of undefined functions, so *this* library probably isn't header-only. – eerorika Sep 08 '16 at 07:36
  • Sorry messed up with libraries and header files. I don't use lib's, just header files, removed them from the Linker search paths. – Eggi Sep 08 '16 at 07:57
  • @Mansuro No, this is the first time i get such an error, i am playing with the settings of the IDE right now. – Eggi Sep 08 '16 at 08:16
  • @Eggi you only need to set the lib file in the linker path – Mansuro Sep 08 '16 at 08:30
  • @Mansuro My problem is, that i didn't create a .lib file and never made one. Have to read through some documentation first. – Eggi Sep 08 '16 at 08:54
  • @Eggi not your lib file, snmp lib file, the third party library you are using – Mansuro Sep 08 '16 at 08:55
  • @Mansuro There is/are none. – Eggi Sep 08 '16 at 09:00
  • @Eggi How did you get snmp, it should have something in addition to the header files – Mansuro Sep 08 '16 at 09:09
  • @Mansuro what Snmp do you mean? The public class? It's declared in a header file (uxsnmp). The files i am using are from the Snmp++ project from HP, Link to the page is on the bottom of the question. – Eggi Sep 08 '16 at 09:12
  • 1
    @Eggi From where did you download it? – Mansuro Sep 08 '16 at 09:14
  • 1
    @Eggi You need to build the library manually, and then you can use it – Mansuro Sep 08 '16 at 09:19