Undefined references when linking with gcc -lnetsnmp - linux

I'm trying to compile and run my code on a raspberry pi which needs the snmp libraries. However, I get linking errors such as "undefined reference to 'EVP_DigestUpdate'".
I've been researching this issue for the past few days and found out that this usually happens when snmp can't find the ssl library crypto, for example if you don't specify -lcrypto before -lsnmp when linking your object, or when the linker just can't can't find -lcrypto.
However, on my raspberry pi I have all required libraries in /usr/lib/gcc/arm-linux-gnueabihf.
This is how I installed the libraries:
apt-get install libssl-dev libsnmp-dev libsnmp-base libsnmp30
I generate trap.o with this command:
gcc -c -g -pedantic -Wall -Wshadow -I/home/pi/tests/h -I/usr/include trap.c -o trap.o
I link trap.o the libraries with this command:
gcc -static -g -pedantic -Wall -Wshadow trap.o -o trap -L/usr/lib/arm-linux-gnueabihf/lib -lcrypto -lpthread -lnetsnmpagent -lnetsnmpmibs -lnetsnmphelpers -lnetsnmp
And then I get this error:
gcc -static -g -pedantic -Wall -Wshadow trap.o -o trap -L/usr/lib/arm-linux-gnueabihf/lib -lcrypto -lpthread -lnetsnmpagent -lnetsnmpmibs -lnetsnmphelpers -lnetsnmp
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_str_to_gid':
(.text+0x1144): warning: Using 'getgrnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_str_to_gid':
(.text+0x1154): warning: Using 'endgrent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_str_to_uid':
(.text+0x10d8): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_str_to_uid':
(.text+0x10e8): warning: Using 'endpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_getaddrinfo':
(.text+0x4a4): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(system.o): In function `netsnmp_gethostbyaddr':
(.text+0x9f8): warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
trap.o: In function `parse_address':
/home/pi/tests/trap.c:56: warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/crt1.o: In function `_start':
/build/glibc-6f8a9a/glibc-2.19/csu/../ports/sysdeps/arm/start.S:119: undefined reference to `main'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_random':
(.text+0x168): undefined reference to `RAND_bytes'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_generate_keyed_hash':
(.text+0x290): undefined reference to `EVP_md5'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_generate_keyed_hash':
(.text+0x2b4): undefined reference to `HMAC'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_generate_keyed_hash':
(.text+0x33c): undefined reference to `EVP_sha1'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x424): undefined reference to `EVP_sha1'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x42c): undefined reference to `EVP_MD_CTX_create'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x438): undefined reference to `EVP_DigestInit'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x448): undefined reference to `EVP_DigestUpdate'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x458): undefined reference to `EVP_DigestFinal'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x468): undefined reference to `EVP_MD_CTX_destroy'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_hash':
(.text+0x488): undefined reference to `EVP_md5'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0x86c): undefined reference to `DES_key_sched'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0x8a0): undefined reference to `DES_ncbc_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0x8c4): undefined reference to `DES_ncbc_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0x90c): undefined reference to `AES_set_encrypt_key'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0x948): undefined reference to `AES_cfb128_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0xa74): undefined reference to `DES_key_sched'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_encrypt':
(.text+0xaac): undefined reference to `DES_ncbc_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_decrypt':
(.text+0xc78): undefined reference to `AES_set_encrypt_key'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_decrypt':
(.text+0xcb0): undefined reference to `AES_cfb128_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_decrypt':
(.text+0xd9c): undefined reference to `DES_key_sched'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(scapi.o): In function `sc_decrypt':
(.text+0xdd0): undefined reference to `DES_cbc_encrypt'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x74): undefined reference to `EVP_MD_CTX_create'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x98): undefined reference to `EVP_md5'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0xa4): undefined reference to `EVP_DigestInit'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0xe8): undefined reference to `EVP_DigestUpdate'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x10c): undefined reference to `EVP_DigestFinal'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x138): undefined reference to `EVP_MD_CTX_destroy'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x178): undefined reference to `EVP_sha1'
/usr/lib/gcc/arm-linux-gnueabihf/4.9/../../../arm-linux-gnueabihf/libnetsnmp.a(keytools.o): In function `generate_Ku':
(.text+0x184): undefined reference to `EVP_DigestInit'
collect2: error: ld returned 1 exit status
Could it be that the issue is being caused because the snmp libraries I installed were built using a different openssl version than the ones I have in my environment? My libssl-dev and libsnmp-dev versions are the following:
dpkg -l | grep libssl-dev:
ii libssl-dev:armhf 1.0.1k-3+deb8u5 armhf Secure Sockets Layer toolkit - development files
dpkg -l | grep libsnmp-dev:
ii libsnmp-dev 5.7.2.1+dfsg-1 armhf SNMP (Simple Network Management Protocol) development files
Any help would be appreciated,
Thanks

All of your undefined references are in libnetsnmp and they are all undefined
references to functions defined in libcypto. This happens because -lcrypto
precedes -lnetsnmp in your linkage.
In the linkage sequence, files that need symbol definitions must occur
before the ones that provide the definitions, because the linker by default will
only search a library to find definitions of symbols that it has
observed to be referenced, but not defined, in earlier files. (You appear to
have mis-learned this rule, the wrong way round). From man ld:
The linker will search an archive only once, at the location where
it is specified on the command line. If the archive defines a
symbol which was undefined in some object which appeared before the
archive on the command line, the linker will include the
appropriate file(s) from the archive. However, an undefined symbol
in an object appearing later on the command line will not cause the
linker to search the archive again.
So link -lcrypto after -lnetsnmp.

Related

Problems building the openssl crate with Cargo

Whenever I try to compile I get
= note: /usr/bin/ld: /home/trent/Documents/projects/coding/rust/website/auth/target/debug/deps/libnative_tls-e4af7be01e0cc1c7.rlib(native_tls-e4af7be01e0cc1c7.native_tls.c3tq55qu-cgu.0.rcgu.o): in function `<openssl::stack::StackRef<T>>::len':
/home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.9.24/src/stack.rs:171: undefined reference to `sk_num'
/usr/bin/ld: /home/trent/Documents/projects/coding/rust/website/auth/target/debug/deps/libnative_tls-e4af7be01e0cc1c7.rlib(native_tls-e4af7be01e0cc1c7.native_tls.c3tq55qu-cgu.0.rcgu.o): in function `<openssl::stack::StackRef<T>>::_get':
/home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.9.24/src/stack.rs:242: undefined reference to `sk_value'
/usr/bin/ld: /home/trent/Documents/projects/coding/rust/website/auth/target/debug/deps/libopenssl-a51d9ef9dbb88c83.rlib(openssl-a51d9ef9dbb88c83.openssl.27r2wowg-cgu.11.rcgu.o): in function `openssl::ssl::compat::tls_method':
/home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.9.24/src/ssl/mod.rs:2546: undefined reference to `SSLv23_method'
/usr/bin/ld: /home/trent/Documents/projects/coding/rust/website/auth/target/debug/deps/libopenssl-a51d9ef9dbb88c83.rlib(openssl-a51d9ef9dbb88c83.openssl.27r2wowg-cgu.15.rcgu.o): in function `<openssl::stack::Stack<T>>::new':
/home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.9.24/src/stack.rs:40: undefined reference to `sk_new_null'
/usr/bin/ld: /home/trent/Documents/projects/coding/rust/website/auth/target/debug/deps/libopenssl-a51d9ef9dbb88c83.rlib(openssl-a51d9ef9dbb88c83.openssl.27r2wowg-cgu.15.rcgu.o): in function `<openssl::stack::Stack<T> as core::ops::drop::Drop>::drop':
/home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.9.24/src/stack.rs:50: undefined reference to `sk_free'
/usr/bin/ld: /home/trent/Documents/projects/coding/rust/website/auth/target/debug/deps/libopenssl-a51d9ef9dbb88c83.rlib(openssl-a51d9ef9dbb88c83.openssl.27r2wowg-cgu.15.rcgu.o): in function `<openssl::stack::StackRef<T>>::pop':
/home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.9.24/src/stack.rs:232: undefined reference to `sk_pop'
/usr/bin/ld: /home/trent/Documents/projects/coding/rust/website/auth/target/debug/deps/libopenssl-a51d9ef9dbb88c83.rlib(openssl-a51d9ef9dbb88c83.openssl.27r2wowg-cgu.4.rcgu.o): in function `openssl::version::number':
/home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.9.24/src/version.rs:51: undefined reference to `SSLeay'
/usr/bin/ld: /home/trent/Documents/projects/coding/rust/website/auth/target/debug/deps/libopenssl-a51d9ef9dbb88c83.rlib(openssl-a51d9ef9dbb88c83.openssl.27r2wowg-cgu.6.rcgu.o): in function `openssl::x509::compat::X509_up_ref':
/home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-0.9.24/src/x509/mod.rs:1172: undefined reference to `CRYPTO_add_lock'
/usr/bin/ld: /home/trent/Documents/projects/coding/rust/website/auth/target/debug/deps/libopenssl_sys-51d0c50f2e67767f.rlib(openssl_sys-51d0c50f2e67767f.openssl_sys.afwyjyem-cgu.9.rcgu.o): in function `openssl_sys::init::{{closure}}':
/home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.40/src/lib.rs:150: undefined reference to `SSL_library_init'
/usr/bin/ld: /home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.40/src/lib.rs:151: undefined reference to `SSL_load_error_strings'
/usr/bin/ld: /home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.40/src/lib.rs:152: undefined reference to `OPENSSL_add_all_algorithms_noconf'
/usr/bin/ld: /home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.40/src/lib.rs:154: undefined reference to `CRYPTO_num_locks'
/usr/bin/ld: /home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.40/src/lib.rs:164: undefined reference to `CRYPTO_set_locking_callback'
/usr/bin/ld: /home/trent/Documents/projects/coding/rust/website/auth/target/debug/deps/libopenssl_sys-51d0c50f2e67767f.rlib(openssl_sys-51d0c50f2e67767f.openssl_sys.afwyjyem-cgu.9.rcgu.o): in function `openssl_sys::init::set_id_callback':
/home/trent/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.40/src/lib.rs:139: undefined reference to `CRYPTO_set_id_callback'
collect2: error: ld returned 1 exit status
Complete log
I have both OpenSSL and OpenSSL-1.0 installed. I have tried using both Rust stable and nightly. I've also tried to include different versions of the openssl crate, but I think Cargo is just building the version of the crate that I think grpc is using. I'm on Arch Linux.
Your project is using openssl 0.9.x (probably via tls-api-native-tls 0.1.x -> native-tls 0.1.x -> openssl 0.9.x), which doesn't support OpenSSL 1.1.1. I'm also on Arch Linux and I've had to update my programs to openssl 0.10 in order to use OpenSSL 1.1.1.
In your case, tls-api-native-tls needs to be updated to depend on native-tls 0.2.x, which uses openssl 0.10.x (which does support OpenSSL 1.1.1). There is an open issue about this.
If that's too complicated, you can also use various environment variables to tell openssl-sys's build script which installation of OpenSSL you'd like to use. If you'd prefer using OpenSSL 1.0.x, enter the following commands in your shell:
$ cargo clean
$ OPENSSL_INCLUDE_DIR=/usr/include/openssl-1.0 OPENSSL_LIB_DIR=/usr/lib/openssl-1.0 cargo build
(Note: the paths above may be specific to Arch Linux.)
For more information on the environment variables you can use to configure openssl-sys, see Manual configuration.

After I compile mrpt manually, I have a trouble with build mrpt files

I'm trying to combine every system with ros but before that I wanted to check whether it works well or not so I run file on CLion, and I got these errors.
/home/ian/Downloads/clion-2017.2.1/bin/cmake/bin/cmake --build /home/ian/CLionProjects/mrqt --target mrpt -- -j 4
[ 50%] Linking CXX executable mrpt
/usr/bin/ld: warning: libopencv_imgcodecs3.so.3.2, needed by /usr/local/lib/libmrpt-base.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libopencv_imgproc3.so.3.2, needed by /usr/local/lib/libmrpt-base.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libopencv_core3.so.3.2, needed by /usr/local/lib/libmrpt-base.so, not found (try using -rpath or -rpath-link)
/usr/local/lib/libmrpt-base.so: undefined reference to `cvLoadImage'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvConvertImage'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvReleaseImage'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvSplit'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvMatchTemplate'
/usr/local/lib/libmrpt-base.so: undefined reference to `_IplImage::_IplImage(cv::Mat const&)'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvCreateImage'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvSmooth'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvFlip'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvGetSize'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvCvtColor'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvCircle'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvGetQuadrangleSubPix'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvRemap'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvLine'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvSaveImage'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvCopy'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvCloneImage'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvSetImageROI'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvEqualizeHist'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvMinMaxLoc'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvMerge'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvResize'
/usr/local/lib/libmrpt-base.so: undefined reference to `cvUndistort2'
collect2: error: ld returned 1 exit status
CMakeFiles/mrpt.dir/build.make:94: recipe for target 'mrpt' failed
make[3]: *** [mrpt] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mrpt.dir/all' failed
make[2]: *** [CMakeFiles/mrpt.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/mrpt.dir/rule' failed
make[1]: *** [CMakeFiles/mrpt.dir/rule] Error 2
Makefile:118: recipe for target 'mrpt' failed
make: *** [mrpt] Error 2
When I first compiled it, it was fine, but later I had some issues with mrpt header files, I deleted them all and compiled it manually, and this problem happened. Can I get some advice?
You probably have a mix of different MRPT versions installed in your system.
I don't recommend doing sudo make install unless you have a strong reason to do it; just compiling the project in its own build directory will generate a MRPTConfig.cmake file, which allows CMake to find MRPT from ROS or whatever other user project.
If CMake doesn't find it automatically, just set the environment variable export MRPT_DIR=/home/xxx/path/to/mrpt/build and it will work.
Your problem right now is cleaning all the mess that may be under /usr/local/. Take a look at that directory and if the only project you have make installed is MRPT, I strongly recommend deleting it with sudo rm -fr /usr/local/*.
If you have other projects there... well... you must run sudo make uninstall from the MRPT build dir, then manually make sure there are no remaining files.
The link error against OpenCV also looks like CMake configured the project for one opencv version, then another one got installed (perhaps via ROS packages?).
After doing the clean up of /usr/local mentioned above, delete your current MRPT CMake build directory and start from scratch again.

Undefine reference for libraries, so How could I find the right path?

I am trying to compile a v4l2 example in Ubuntu but I am getting the following error:
guilherme#notedev01:~/Downloads/V4l2_samples-0.4.1$ make
gcc -O2 -L/usr/include -lX11 -lXext -o viewer viewer.c
/tmp/ccUjnjWQ.o: In function `image_destroy':
viewer.c:(.text+0x234): undefined reference to `XDestroyImage'
viewer.c:(.text+0x256): undefined reference to `XFreeGC'
viewer.c:(.text+0x277): undefined reference to `XShmDetach'
viewer.c:(.text+0x2ac): undefined reference to `XFreePixmap'
/tmp/ccUjnjWQ.o: In function `image_create':
viewer.c:(.text+0x305): undefined reference to `XCreateGC'
viewer.c:(.text+0x31d): undefined reference to `XGetWindowAttributes'
viewer.c:(.text+0x39e): undefined reference to `XShmCreateImage'
viewer.c:(.text+0x3f5): undefined reference to `XShmAttach'
viewer.c:(.text+0x44e): undefined reference to `XCreateImage'
viewer.c:(.text+0x494): undefined reference to `XShmQueryExtension'
viewer.c:(.text+0x4b4): undefined reference to `XShmPixmapFormat'
viewer.c:(.text+0x4dc): undefined reference to `XShmCreatePixmap'
/tmp/ccUjnjWQ.o: In function `image_put':
viewer.c:(.text+0x54c): undefined reference to `XPutImage'
viewer.c:(.text+0x586): undefined reference to `XShmPutImage'
/tmp/ccUjnjWQ.o: In function `main':
viewer.c:(.text.startup+0x18b): undefined reference to `XOpenDisplay'
viewer.c:(.text.startup+0x1b1): undefined reference to `XScreenOfDisplay'
viewer.c:(.text.startup+0x1ee): undefined reference to `XCreateSimpleWindow'
viewer.c:(.text.startup+0x249): undefined reference to `XMapRaised'
viewer.c:(.text.startup+0x263): undefined reference to `XStoreName'
viewer.c:(.text.startup+0x280): undefined reference to `XGetWindowAttributes'
viewer.c:(.text.startup+0x92f): undefined reference to `XPending'
viewer.c:(.text.startup+0x94c): undefined reference to `XNextEvent'
viewer.c:(.text.startup+0xaee): undefined reference to `XPending'
viewer.c:(.text.startup+0xb0b): undefined reference to `XNextEvent'
viewer.c:(.text.startup+0xf39): undefined reference to `XPending'
viewer.c:(.text.startup+0xf56): undefined reference to `XNextEvent'
collect2: error: ld returned 1 exit status
make: *** [viewer] Error 1
What I can see is that the path for -lx11 and -lXext isn't -L/usr/include.
How can I find the right path for those libraries?
Thanks.
as Chris has pointed out, the order is wrong, you need to put the -lX11 -lXext after the source-code/object-files.
this is because modern compilers try to optimize the final result and not link against unused libraries.
they do so by maintaining a list of unresolved symbols within an object and use any binary files that come aferwards in the linker arguments to resolve those symbols.
example
your program test uses the function do_foo() from libfoo and the function do_bar_do() from libbar.
you link it using:
$ gcc -o test test.o -lfoo -lbar
the linker first searches test.o and notices that some symbols (do_foo and do_bar_do) are not defined anywhere. it then proceeds to libfoo (specified right after test.o) and finds that it provides do_foo, so it creates code to use it from your program. do_bar_do is still unresolved, until the linker checks upon libbar.
consider doing it the wrong way:
$ gcc -o test -lfoo test.o -lbar
the linker will first check libfoo and see that it doesn't contain any unresolved symbols. cool. it will then proceed to test.o and notice do_bar_do and do_foo. do_bar_do is resolved by the right-hand libbar but do_foo is not resolved at all, and you get an error:
undefined reference to `do_foo'
"but the code is meant to be a tutorial..."
so why is it not working?
older compilers where a bit lax about the order of dependencies (they would check all binaries/libraries/objects whether a given symbol could be resolved); that's why you can still find code out there that puts the libraries to link against before the object files.
The -lX11 -lXext must come after the viewer.c in the command line (and should probably be in the order -lXext -lX11). Also, ensure that the libx11-6-dev and libxext6-dev packages are installed.
System libraries are usually in /lib and /usr/lib, and you do not need to use -L to specify those directories.

undefined reference to `jack_client_close`, et al

I'm trying to follow this introductory tutorial on jack (audio server for linux). In the tutorial, the author explains that you should use pkg-config to find the cflags and libs for jack, making the gcc command like this:
gcc -o simple_client `pkg-config --cflags --libs jack` simple_client.c
which gives the output:
/tmp/ccyuOC0u.o: In function `signal_handler':
simple_client.c:(.text+0x16): undefined reference to `jack_client_close'
/tmp/ccyuOC0u.o: In function `process':
simple_client.c:(.text+0x6f): undefined reference to `jack_port_get_buffer'
simple_client.c:(.text+0x87): undefined reference to `jack_port_get_buffer'
/tmp/ccyuOC0u.o: In function `main':
simple_client.c:(.text+0x25b): undefined reference to `sin'
simple_client.c:(.text+0x2c1): undefined reference to `jack_client_open'
simple_client.c:(.text+0x372): undefined reference to `jack_get_client_name'
simple_client.c:(.text+0x3b1): undefined reference to `jack_set_process_callback'
simple_client.c:(.text+0x3ca): undefined reference to `jack_on_shutdown'
simple_client.c:(.text+0x3ee): undefined reference to `jack_port_register'
simple_client.c:(.text+0x419): undefined reference to `jack_port_register'
simple_client.c:(.text+0x475): undefined reference to `jack_activate'
simple_client.c:(.text+0x4c5): undefined reference to `jack_get_ports'
simple_client.c:(.text+0x514): undefined reference to `jack_port_name'
simple_client.c:(.text+0x52c): undefined reference to `jack_connect'
simple_client.c:(.text+0x56e): undefined reference to `jack_port_name'
simple_client.c:(.text+0x586): undefined reference to `jack_connect'
simple_client.c:(.text+0x5ba): undefined reference to `jack_free'
collect2: ld returned 1 exit status
I'm not very experienced using gcc or writing c programs generally (most of my experience has been with javascript, clojure, java, python, and php). What I gather from this and my research into it is that some libraries are missing or linked incorrectly (not sure which).
So just running pkg-config --cflags --libs jack on my machine, I get:
-ljack
In the tutorial referenced above, the author demonstrates the same method for gleaning the libs to be linked for jack, but his output looks like this:
-ljack -lpthread -ldl -lrt
Not sure what pthread is, but I think dl is dsp-loader, and rt has something to do with realtime. I've searched in several directories called /lib and haven't come across anything for these other libs, so I don't think they exist on my machine. However, it seems strange to me that calling pkg-config doesn't make any mention of them. How should I go about finding these libs? Or am I on the wrong track?
Your link command line is wrong, try this one instead:
gcc -o simple_client simple_client.c `pkg-config --cflags --libs jack`
The order of archive libraries on command line matters.

Undefined Reference at Linking in Static Git Build

I'm trying to install git on a shared server (with no root access or compiler) by building it statically on my own machine (running Arch Linux) with the same processor architecture (x86_64) as the server, as explained in this guide.
When I ran make, I always got these errors and a failure:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':
(.text+0x19): undefined reference to `dlopen'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':
(.text+0x2c): undefined reference to `dlsym'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':
(.text+0x37): undefined reference to `dlclose'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func':
(.text+0x364): undefined reference to `dlsym'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func':
(.text+0x422): undefined reference to `dlerror'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var':
(.text+0x494): undefined reference to `dlsym'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var':
(.text+0x552): undefined reference to `dlerror'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load':
(.text+0x5c2): undefined reference to `dlopen'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load':
(.text+0x62d): undefined reference to `dlclose'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load':
(.text+0x65a): undefined reference to `dlerror'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr':
(.text+0x6ef): undefined reference to `dladdr'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr':
(.text+0x749): undefined reference to `dlerror'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_unload':
(.text+0x7aa): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
make: *** [git-imap-send] Error 1
I finally discovered a different guide, which advised running a slightly different .configure command. What ended up working for me was this guide, with the exception of running
$ ./configure --prefix=/home/myuser/git-static CFLAGS="${CFLAGS} -static" NO_OPENSSL=1 NO_CURL=1
in step 2.
The error message indicates it can't find the definition of
dlclose
dlerror
dleopen
You need to make sure your makefile has -ldl when link the application.
LDFLAGS are for the linker flags. LIBS are for the linked libraries. Confusing them is not usually a problem, but in the case of static libs or when the linker has the --as-needed option enabled the order of libraries matters and any -l passed before libs that use it will be ignored.

Resources