What causes this link failure (undefined reference)? - c

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.

Related

C program using net-snmp does not compile

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

Littlecms - error during compiling "undefined reference to `cmsOpenProfileFromFile' "

Please help me to use LittleCMS framework - I'm receiving
" undefined reference to `cmsOpenProfileFromFile' "
I had made:
1) Downloaded from https://github.com/mm2/Little-CMS file lcms2.h
2) Downloaded https://sourceforge.net/projects/lcms/and tried to install it like it written in documentation (downloaded tar file, unTARed it, runned
./configure
make
sudo make install
3) Tried to implement a sample from tutorial:
#include "lcms2.h"
int main(void)
{
cmsHPROFILE hInProfile, hOutProfile;
cmsHTRANSFORM hTransform;
hInProfile = cmsOpenProfileFromFile("AdobeRGB1998.icc", "r");
hOutProfile = cmsOpenProfileFromFile("WebCoatedSWOP2006Grade5.icc", "r");
hTransform = cmsCreateTransform(hInProfile, TYPE_BGR_8, hOutProfile, TYPE_BGR_8, INTENT_PERCEPTUAL, 0);
cmsCloseProfile(hInProfile);
cmsCloseProfile(hOutProfile);
return 0;
}
After runing "make" I had received " undefined reference to `cmsOpenProfileFromFile' ".
I think I have to install some library (and I have to add something like this -L/opt/local/lib64 -llcms2 to a makefile or add it to clang ...), but I had read kilobytes of webpages and no one is telling how to do it from scratch - libraries does not appears in folder /opt/local/lib64 or /opt/local/lib (and I cannot find lcms2 in any directory) .
My IDE is: CS50 IDE, Linux version 4.9.17-c9 (root#30db80bfe262) (gcc version 4.9.2 (Debian 4.9.2-10) ).
I am very new in programming, so please, be condescending.
Please help me to solve and to inmplement Little CMS.
Update:
3 types of compilations:
1) Command prompt (internal IDE's makefile, without my Makefile):
~/workspace/cmyk/ $ make cmyk
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow -c -o cmyk.o cmyk.c
clang cmyk.o -lcrypt -lcs50 -lm -o cmyk
cmyk.o: In function `main':
/home/ubuntu/workspace/cmyk/cmyk.c:9: undefined reference to `cmsOpenProfileFromFile'
/home/ubuntu/workspace/cmyk/cmyk.c:10: undefined reference to `cmsOpenProfileFromFile'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `__ubsan_handle_shift_out_of_bounds'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `__ubsan_handle_shift_out_of_bounds'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `__ubsan_handle_shift_out_of_bounds'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `__ubsan_handle_shift_out_of_bounds'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `__ubsan_handle_shift_out_of_bounds'
cmyk.o:/home/ubuntu/workspace/cmyk/cmyk.c:12: more undefined references to `__ubsan_handle_shift_out_of_bounds' follow
cmyk.o: In function `main':
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `cmsCreateTransform'
/home/ubuntu/workspace/cmyk/cmyk.c:14: undefined reference to `cmsCloseProfile'
/home/ubuntu/workspace/cmyk/cmyk.c:15: undefined reference to `cmsCloseProfile'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [cmyk] Error 1
2) Makefile1 (with clang):
CC = clang
CFLAGS = -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror
EXE = cmyk
HDRS = lcms2.h
LIBS =
SRCS = cmyk.c
OBJS = $(SRCS:.c=.o)
$(EXE): $(OBJS) $(HDRS) Makefile
$(CC) $(CFLAGS) -o $# $(OBJS) $(LIBS)
$(OBJS): $(HDRS) Makefile
clean:
rm -f core $(EXE) *.o
Output:
~/workspace/cmyk/ $ make cmyk
clang -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror -c -o cmyk.o cmyk.c
clang -ggdb3 -O0 -Qunused-arguments -std=c99 -Wall -Werror -o cmyk cmyk.o
cmyk.o: In function `main':
/home/ubuntu/workspace/cmyk/cmyk.c:9: undefined reference to `cmsOpenProfileFromFile'
/home/ubuntu/workspace/cmyk/cmyk.c:10: undefined reference to `cmsOpenProfileFromFile'
/home/ubuntu/workspace/cmyk/cmyk.c:12: undefined reference to `cmsCreateTransform'
/home/ubuntu/workspace/cmyk/cmyk.c:14: undefined reference to `cmsCloseProfile'
/home/ubuntu/workspace/cmyk/cmyk.c:15: undefined reference to `cmsCloseProfile'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [cmyk] Error 1
3) Makefile2:
iEdit: cmyk.o
gcc $^ -o $# -std=c99
.c.o:
gcc -c $< -std=c99
cmyk.o: lcms2.h
Output:
~/workspace/cmyk/ $ make cmyk
gcc -c cmyk.c -std=c99
clang cmyk.o -lcrypt -lcs50 -lm -o cmyk
cmyk.o: In function `main':
cmyk.c:(.text+0x13): undefined reference to `cmsOpenProfileFromFile'
cmyk.c:(.text+0x26): undefined reference to `cmsOpenProfileFromFile'
cmyk.c:(.text+0x50): undefined reference to `cmsCreateTransform'
cmyk.c:(.text+0x60): undefined reference to `cmsCloseProfile'
cmyk.c:(.text+0x6c): undefined reference to `cmsCloseProfile'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [cmyk] Error 1
Solution is to use other IDE - CS50 IDE does not allowed to install additional libraries. I had installed VirtualBox, mounted xubuntu, installed lib, added -L/usr/local/lib -llcms2 to Makefile and my program was compiled without errors.

MonetDB test client application not linking

I tried to compile test application for MonetDB under Ubuntu 14.04 LTS Trusty Tahr. I followed the download instructions from official site, installation was successful, then I installed a bunch of other packages in order to copmpile it. Now when I try to compile and link test application I get the following errors:
libtool: compile: gcc -c -I/usr/include/monetdb test.c -fPIC -DPIC -o .libs/test.o
libtool: compile: gcc -c -I/usr/include/monetdb test.c -o test.o >/dev/null 2>&1
libtool: link: gcc -o test test.o -lmapi -lstream -lssl -lcrypto -lcurl -lz
test.o: In function `die':
test.c:(.text+0x1c): undefined reference to `mapi_explain_query'
test.c:(.text+0x27): undefined reference to `mapi_result_error'
test.c:(.text+0x3f): undefined reference to `mapi_explain_result'
test.c:(.text+0x4a): undefined reference to `mapi_next_result'
test.c:(.text+0x5a): undefined reference to `mapi_close_handle'
test.c:(.text+0x65): undefined reference to `mapi_destroy'
test.c:(.text+0x81): undefined reference to `mapi_explain'
test.c:(.text+0x8c): undefined reference to `mapi_destroy'
test.o: In function `query':
test.c:(.text+0xde): undefined reference to `mapi_query'
test.c:(.text+0xf2): undefined reference to `mapi_error'
test.o: In function `update':
test.c:(.text+0x133): undefined reference to `mapi_close_handle'
test.o: In function `main':
test.c:(.text+0x190): undefined reference to `mapi_connect'
test.c:(.text+0x1a0): undefined reference to `mapi_error'
test.c:(.text+0x222): undefined reference to `mapi_fetch_field'
test.c:(.text+0x23a): undefined reference to `mapi_fetch_field'
test.c:(.text+0x266): undefined reference to `mapi_fetch_row'
test.c:(.text+0x276): undefined reference to `mapi_close_handle'
test.c:(.text+0x282): undefined reference to `mapi_destroy'
collect2: error: ld returned 1 exit status
./t.sh: 11: ./t.sh: ./test: not found
What I'm doing wrong? What should I additionally install or change in compilation options for successful linking of test example?
I was able to compile the example on Ubuntu 14.04 with MonetDB installed from the Ubuntu binary packages after installing the (additional) package libmonetdb-client-dev with the following command:
gcc test.c -I /usr/include/monetdb -lmapi
Did you compile and install MonetDB from source? In this case, which prefix was used?
Here how it worked using ubuntu 18.04
gcc main.c -I /usr/include/monetdb -lmapi -lssl -lcrypto -lstream -lcurl -llzma -lbz2

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.

GNU readline History feature

I'm using this code for the history features in my shell:
http://cc.byexamples.com/20080613/gnu-readline-how-to-keep-a-history-list-of-entered-command-lines/
but when I compile this using gcc, I got this error
$ gcc filename.c
/tmp/ccay2CgM.o: In function `main':
rl.c:(.text+0x9): undefined reference to `rl_abort'
rl.c:(.text+0x13): undefined reference to `rl_bind_key'
rl.c:(.text+0x1d): undefined reference to `readline'
rl.c:(.text+0x61): undefined reference to `add_history'
collect2: ld returned 1 exit status
$
The example compile line is
g++ -o simple_rl{,.cpp} -lreadline
Did you forget to add the -lreadline?
Compile with this:
gcc -lreadline -lncurses -o simple_rl{,.cpp}

Resources