How to use SNMP++ functions in your own code

Hello all,
I built the SNMP++ library in a home directory as described in the README with the following commands:

autoreconf -i
./configure
make

Then added the following command:

sudo make install

To test the library I wrote the following minimal application up to the point of:

#include <libsnmp.h>
#include “/home/user/path/to/snmp+±3.4.10/include/snmp_pp/snmp_pp.h”

int main(int argc, char **argv) {
Snmp_pp::Snmp::socket_startup();
Snmp_pp::Snmp::socket_cleanup();
return 0;
}

(Please excuse the incorrect indentation. Unfortunately I don’t know how to do this correctly in Markdown. )

To build I try this as follows:

g++ myTest.cpp -o test

Unfortunately I get the following errors among others

myTest.cpp: In function ‘int main(int, char**)’:
myTest.cpp:7:3: error: ‘Snmp_pp’ has not been declared
Snmp_pp::Snmp::socket_startup();
^~~~~~~
myTest.cpp:8:3: error: ‘Snmp_pp’ has not been declared
Snmp_pp::Snmp::socket_cleanup();
^~~~~~~

My guess is, that I still need to tell the linker where to find the library. Unfortunately I don’t know the command for this.
I have also already tried it with -libsnmp. Unfortunately without success.

Thanks in advance and kind regards
schande

Hi,

there are surely some errors about missing includes. You should change the includes to

#include <libsnmp.h>
#include <snmp_pp/snmp_pp.h>

and then compile using g++ -I /home/user/path/to/snmp++-3.4.10/include/ -L /home/user/path/to/snmp++-3.4.10/lib64 -l snmp++ -o test (maybe lib64 has to be replaced with lib).

Kind regards,
Jochen

1 Like

Hello jkatz,
thank you for your reply!
Your linker commands worked like a charm. I was able to build my test file and can start to work on my project now.

A small addition to the compile command: The filename was missing :wink:

g++ myTest.cpp -I /home/user/path/to/snmp+±3.4.10/include/ -L /home/user/path/to/snmp+±3.4.10/lib -l snmp++ -o test

For future people who read the answer here and want to know more about the different flags like “-L” or “-I” I can recommend the following page:
https://linux.die.net/man/1/g++

Kind regards
schande