C program using net-snmp does not compile - c

I have installed net-snmp 5.8 on a Ubuntu 16.0.4 machine and then I have checked the correct installation:
snmpget --version
NET-SNMP version: 5.8
Next, I am trying to write and compile my first SNMP C program example.
I have copied the one that is included as example on the tutorial from Ben Rockwood ("The Net-SNMP Programming Guide) and I have tried to compile it with the command:
gcc ‘net-snmp-config --cflags‘ ‘net-snmp-config --libs‘ \
> ‘net-snmp-config --external-libs‘ snmp_test.c -o snmp_test
As indicated in this tutorial.
When do, I get the errors:
gcc: error: unrecognized command line option ‘--cflags‘’
gcc: error: unrecognized command line option ‘--libs‘’
gcc: error: unrecognized command line option ‘--external-libs‘’
Then I have changed the gcc command to:
gcc net-snmp-config --cflags net-snmp-config --libs \
net-snmp-config --external-libs snmp_test.c -o snmp_test
And get the error:
bash: net-snmp-config --external-libs: ambiguous redirect
What is wrong on the gcc call? Any comments or suggestions are welcome.

You are probably using the wrong ticks. Use this method for command substitution, it is a bit safer:
gcc $(net-snmp-config --cflags) $(net-snmp-config --libs) $(net-snmp-config --external-libs) snmp_test.c -o snmp_test
The ticks you show here (‘) are the wrong ones, the ones you need are these: ` . However, I prefer the $() syntax, which has the additional advantage, that it is nestable.

I have tryied the command you tell me:
gcc $(net-snmp-config --cflags) $(net-snmp-config --libs) $(net-snmp-config --external-libs) snmp_test.c -o snmp_test
And I get these errors:
/tmp/ccKrUliA.o: In function `main':
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:29: undefined reference to `init_snmp'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:31: undefined reference to `snmp_sess_init'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:36: undefined reference to `snmp_open'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:38: undefined reference to `add_mibdir'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:39: undefined reference to `read_mib'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:40: undefined reference to `snmp_pdu_create'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:42: undefined reference to `read_objid'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:43: undefined reference to `snmp_add_null_var'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:45: undefined reference to `read_objid'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:46: undefined reference to `snmp_add_null_var'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:48: undefined reference to `snmp_synch_response'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:51: undefined reference to `print_value'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:53: undefined reference to `snmp_free_pdu'
/home/jose/NETWORK_PROTOCOLS/ej_02_SNMP/snmp_test.c:54: undefined reference to `snmp_close'
collect2: error: ld returned 1 exit status
Then I have tried to add -L and -lsnmp, with this result:
jose#jose-VirtualBox:~/NETWORK_PROTOCOLS/ej_02_SNMP$ gcc -L/usr/locallib/ -lsnmp $(net-snmp-config --cflags) $(net-snmp-config --libs) $(net-snmp-config --external-libs) snmp_test.c -o snmp_test
/usr/bin/ld: cannot find -lsnmp
collect2: error: ld returned 1 exit status

Related

Setting up libcurl on Linux

I'm trying to use libcurl, but am failing to set it up correctly. I've been reading the documentation for the past hours, but I'm confused and lost. (This is my first time using an external library with C)
Based on these instructions, I've correctly configured and installed libcurl and curl-config. A minimal C program that simply includes <curl/curl.h> compiles; however, when I run any example program (say, chkspeed.c), I get the following "undefined" errors.
/tmp/ccprXNBB.o: In function `main':
chkspeed.c:(.text+0x1bf): undefined reference to `curl_version'
chkspeed.c:(.text+0x408): undefined reference to `curl_global_init'
chkspeed.c:(.text+0x40d): undefined reference to `curl_easy_init'
chkspeed.c:(.text+0x432): undefined reference to `curl_easy_setopt'
chkspeed.c:(.text+0x454): undefined reference to `curl_easy_setopt'
chkspeed.c:(.text+0x476): undefined reference to `curl_easy_setopt'
chkspeed.c:(.text+0x482): undefined reference to `curl_easy_perform'
chkspeed.c:(.text+0x4b0): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x50b): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x566): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x5c9): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x624): undefined reference to `curl_easy_getinfo'
chkspeed.c:(.text+0x66a): undefined reference to `curl_easy_strerror'
chkspeed.c:(.text+0x696): undefined reference to `curl_easy_cleanup'
chkspeed.c:(.text+0x69b): undefined reference to `curl_global_cleanup'
collect2: error: ld returned 1 exit status
The following is my output for the three curl-config flags featured in this guide. I'm not sure how to use this information:
$: curl-config --cflags
-I/usr/local/include
$: curl-config --libs
-L/usr/local/lib -lcurl
$: curl-config --feature
IPv6
UnixSockets
libz
AsynchDNS
I would really appreciate any help that might get me in the right direction, if not solve the issue. Thank you for your time!
You should compile it like this:
$ gcc chkspeed.c -o chkspeed $(curl-config --cflags) $(curl-config --libs)
so that the gcc command can have the proper CFLAGS and LDFLAGS for compiling and linking against libcurl.
Note when working with a shell (like bash) and you execute a command like this:
$ cmd1 arg1 arg2 $(cmd2 arg3)
the shell will evaluate first cmd arg3 by executing it and using the stdout output of cmd2 as an argument of for cmd1. Let's say that cmd2 arg3 prints (on stdout) hello, then the shell will execute cmd1 arg1 arg2 hello.
So
$ gcc chkspeed.c -o chkspeed $(curl-config --cflags) $(curl-config --libs)
will be executed as
$ gcc chkspeed.c -o chkspeed -I/usr/local/include -L/usr/local/lib -lcurl
because the output of curl-config --cflags is -I/usr/local/include and the output of curl-config --libs is -L/usr/local/lib -lcurl.

Unable to make ethping program because of missing pcap definitions

I seem to be having an issue building the ethping executable. It cannot find any of the functions that are defined in pcap.h.
At first I thought it wasn't finding pcap.h, but I have checked the $PATH and /usr/include is there (the pcap.h there is a stub that then includes pcap/pcap.h, but that looks all good too).
I even tried -I /usr/include and -I /usr/include/pcap, but still no luck.
I have searched through tons of forum postings, but could not find a solution. So that's why I am here. Any ideas?
root:src# gcc -Wall -Werror -ggdb -g -O2 -lpcap -o ethping ethping.o ieee8021ag.o dot1ag_eth.o
ethping.o: In function `timeout_handler':
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:65: undefined reference to `pcap_breakloop'
ethping.o: In function `main':
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:183: undefined reference to `pcap_open_live'
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:202: undefined reference to `pcap_compile'
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:203: undefined reference to `pcap_setfilter'
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:254: undefined reference to `pcap_next_ex'
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:257: undefined reference to `pcap_perror'
collect2: error: ld returned 1 exit status
Similarly:
root:src# gcc -Wall -Werror -ggdb -g -O2 -lpcap -I /usr/include/pcap -o ethping ethping.o ieee8021ag.o dot1ag_eth.o
ethping.o: In function `timeout_handler':
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:65: undefined reference to `pcap_breakloop'
ethping.o: In function `main':
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:183: undefined reference to `pcap_open_live'
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:202: undefined reference to `pcap_compile'
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:203: undefined reference to `pcap_setfilter'
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:254: undefined reference to `pcap_next_ex'
/home/ubuntu/Downloads/dot1ag-utils-master/src/ethping.c:257: undefined reference to `pcap_perror'
collect2: error: ld returned 1 exit status
root:src#
Changing the compile line fixed it. Placing -lpcap at the far right got it going as suggested.
gcc -Wall -Werror -ggdb -g -O2 -o ethping ethping.o ieee8021ag.o dot1ag_eth.o -lpcap
Either you are invoking the linker without the arguments to include the libpcap library, or the libraries are not installed. See http://www.tcpdump.org/ to obtain the library. Or if you are on a well-supported distribution, use the applicable install command:
sudo apt-get install libpcap-dev
or
sudo yum install libpcap-dev

undefined reference to `dlopen' since ubuntu upgrade

i experience the undefined reference to `dlopen' problems since I have upgraded to ubuntu 13.10 and gcc 4.8.1.
The makefiles are working for years already. the particular call is
gcc -rdynamic -o ov_dbutil ov_dbutil.o libov.so -ldl
The errors are:
libov.so: undefined reference to `dlopen'
libov.so: undefined reference to `dlclose'
libov.so: undefined reference to `dlerror'
libov.so: undefined reference to `dlsym'
collect2: error: ld returned 1 exit status
make: *** [ov_dbutil] Error 1
is it a gcc problem?
ov_dbutil is compiled with the following lines
gcc -g -Wall -O0 -shared -std=c99 -fno-strict-aliasing -DPLT_SYSTEM_LINUX=1 -DPLT_USE_BUFFERED_STREAMS=1 -DPLT_SERVER_TRUNC_ONLY=1 -DNDEBUG -DOV_SYSTEM_LINUX=1 -I../../../plt/include/ -I../../../ks/include/ -I../../include/ -I../../model/ -I../../source/codegen/ -I../../source/builder/ -I../../source/example/ -I../../source/kshist/ -I../../source/dynov/ -I../../source/tasklib/ -I../../source/dbparse/ -I../../source/dbdump/ -I../../../../libml/ -I../../include/runtimeserver/ -I. -c ../../source/dbutil/ov_dbutil.c -o ov_dbutil.o
no errors or warnings
Add -Wl,--no-as-needed as linker arguments.
My issue was solved by adding -ldl in the lining line of the .so you want to link agains, it was called libov.so in the upper example.

How to compile using libmosquitto

Iam trying to compile the code example available on the libmosquitto website (at the bottom):
http://mosquitto.org/man/libmosquitto-3.html
Iam using Ubuntu 12.04 and I've installed libmosquitto1 and libmosquitto1-dev packages. Before installing them I added the mosquitto repository:
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
Iam trying to compile the example as follows:
gcc -lmosquitto mosquito.c -o mosquito
But I get the following errors:
/tmp/cc6eU8kw.o: In function `my_connect_callback':
mosquito.c:(.text+0xf8): undefined reference to `mosquitto_subscribe'
/tmp/cc6eU8kw.o: In function `main':
mosquito.c:(.text+0x298): undefined reference to `mosquitto_lib_init'
mosquito.c:(.text+0x2b4): undefined reference to `mosquitto_new'
mosquito.c:(.text+0x310): undefined reference to `mosquitto_log_callback_set'
mosquito.c:(.text+0x324): undefined reference to `mosquitto_connect_callback_set'
mosquito.c:(.text+0x338): undefined reference to `mosquitto_message_callback_set'
mosquito.c:(.text+0x34c): undefined reference to `mosquitto_subscribe_callback_set'
mosquito.c:(.text+0x364): undefined reference to `mosquitto_connect'
mosquito.c:(.text+0x3b4): undefined reference to `mosquitto_loop'
mosquito.c:(.text+0x3c8): undefined reference to `mosquitto_destroy'
mosquito.c:(.text+0x3d0): undefined reference to `mosquitto_lib_cleanup'
collect2: ld returned 1 exit status
Can someone give me some tips on how to compile this simple example?
Thanks
You have to put the -lmosquitto at the end (after the source files).
gcc mosquito.c -lmosquitto -o mosquito
# or
gcc mosquito.c -o mosquito -lmosquitto
# or
gcc -o mosquito mosquito.c -lmosquitto
Or better:
gcc -Wall -Wextra -pedantic -o mosquito mosquito.c -lmosquitto

What causes this link failure (undefined reference)?

I just switched my dev machine from Gentoo to Arch Linux and, when I try to compile a fresh build of my project, linking fails:
clang -O0 -g -pipe -Wall -DDEBUG -o slug announce.o bitfield.o main.o metadata.o network.o parser.o peer.o piece.o scheduler.o torrent.o url.o util.o -I. -lssl -lm -lcurl -levent
/usr/bin/ld.gold: metadata.o: in function get_info_hash:metadata.c:186: error: undefined reference to 'SHA1_Init'
/usr/bin/ld.gold: metadata.o: in function get_info_hash:metadata.c:187: error: undefined reference to 'SHA1_Update'
/usr/bin/ld.gold: metadata.o: in function get_info_hash:metadata.c:188: error: undefined reference to 'SHA1_Final'
/usr/bin/ld.gold: piece.o: in function verify_piece:piece.c:40: error: undefined reference to 'SHA1_Init'
/usr/bin/ld.gold: piece.o: in function verify_piece:piece.c:41: error: undefined reference to 'SHA1_Update'
/usr/bin/ld.gold: piece.o: in function verify_piece:piece.c:42: error: undefined reference to 'SHA1_Final'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [slug] Error 1
The entire source is on GitHub: https://github.com/robertseaton/slug.
Looks like you're missing -lcrypto
From my experience, the location of -lssl or -lcrypto may be the source of the problem. Try to push -lcrypto option to the end of the gcc options, or before input object files.

Resources