(Newbie) ld exit return 1, and libgcrypt - c

After much research, trying to find out how to link libraries to gcc, going to /usr/bin and /usr/lib confirming the stuff are there. When I try to compile my keygen file, this is the error it blurts out.
$ gcc keygen.c -W -Wall /usr/bin/libgcrypt-config
/usr/bin/libgcrypt-config: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
I've been told by numerous sources that I should compile this way to check if libgcrypt installed correctly.
$ gcc -o foo foo.c 'libgcrypt-config --cflags --libs'
But everytime I try to do that this is what it blurts out:
gcc: error: libgcrypt-config --cflags --libs: No such file or directory
I've confirmed that libgcrypt20 and libgcrypt20-dev are installed using dpkg --get-selections>installed. But I am just so utterly confused as to what may be wrong.
Any form of help would be much appreciated.

Try:
$ gcc -o foo foo.c `libgcrypt-config --cflags --libs`
` instead of '

Related

Linker issue in g++

I have the following .sh file (from here).
g++ -c -pipe -g -std=gnu++11 -Wall -W -fPIC -I. -I./tensorflow
-I./tensorflow/bazel-tensorflow/external/eigen_archive -I./tensorflow/bazel-tensorflow/external/protobuf/src -I./tensorflow/bazel-genfiles -o main.o ./main.cpp
g++ -o Tutorial main.o -L./tensorflow/bazel-bin/tensorflow
-ltensorflow_cc
cp ./tensorflow/bazel-bin/tensorflow/libtensorflow* .
When I try to run this .sh file from terminal I got an error. Therefore I executed the commands one by one. First one worked fine and I saw that when I run the second command ( g++ -o Tutorial main.o -L./tensorflow/bazel-bin/tensorflow
-ltensorflow_cc) I get the following error.
/usr/bin/ld: main.o: undefined reference to symbol '_ZN10tensorflow3Env19NewRandomAccessFileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPSt10unique_ptrINS_16RandomAccessFileESt14default_deleteISA_EE'
libtensorflow_framework.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
I saw the answer here and I see it as closely related to mine. But I cannot figure out how to adapt it to my problem.
Can someone please help with this?
The linker is saying that the linkage requires shared library libtensorflow_framework.so (presumably because -ltensorflow_cc depends on it and requests it) but is not given in your commandline. This should be solved by adding -ltensorflow_framework at the end, with an additional -L option if necessary.
I was too getting the same error.
If you are using tensorflow 2, then you need to link .so.2 files. You should find them in the bazel build directory. For me it is :
/tmp/bazel/output/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow
I linked the files using the below in my CMAKE:
file(GLOB LIBRARIES "${bazel_bin}/tensorflow/*.so.2")
message("LIBRARIES = ${LIBRARIES}")

Cannot find -lCommunication collect2: error: ld returned 1 exit status

I do not know gcc and c well. In my /home/pi/Desktop/intern/adis16227_generic directory I have following 5 files.
ADIS16227.c
ADIS16227.h
Communication.c
Communication.h
main.c
main.c
#include<stdio.h>
#include "Communication.h" // Communication definitions.
int main() {
printf("hello!!\n");
unsigned char status = 0;
status = SPI_Init(0, 1000000, 1, 1);
printf("%u", status);
return 0;
}
Run command:
$ sudo gcc -L /home/pi/Desktop/intern/adis16227_generic main.c -lCommunication
Error:
/usr/bin/ld: cannot find -lCommunication
collect2: error: ld returned 1 exit status
Question:
What I am missing here?
What do I need to run the code?
-l is for libraries, and you never built a library from your Communication.c. The simplest solution is just add Communication.c to your compiler command line.
For larger projects, compile each translation unit separately with the -c switch like this:
gcc -c -Wall -Wextra -pedantic -omain.o main.c
gcc -c -Wall -Wextra -pedantic -oCommunication.o Communication.c
and so on ... (as a suggestion, I added some common warning options here, they help you spot errors)
The resulting .o files are object code. That's already compiled machine code, but with meta-information needed for a linker to link it with other object code into a complete executable.
Then link them all with one command:
gcc -oprogram main.o Communication.o
If you actually want a library from -- say -- Communication.c and ADIS16227.c, you could compile both to object code:
gcc -c -Wall -Wextra -pedantic -oCommunication.o Communication.c
gcc -c -Wall -Wextra -pedantic --oADIS16227.o ADIS16227.c
and then use ar to create a static library from them:
ar rcs libCommunication.a Communication.o ADIS16227.o
Then your initial compiler command would work (with the -lCommunication switch).
Final piece of advice: Never compile as root. This is completely unnecessary. So remove your sudo here.
those options:
-L /home/pi/Desktop/intern/adis16227_generic -lCommunication
suggest that the linker should find libCommunication.a (or .so) in the /home/pi/Desktop/intern/adis16227_generic directory.
But there are only sources in this directory. The linker won't build the sources of your "Communication" library for you.
So you could build the library and link with it:
gcc -c ADIS16227.c Communication.c
ar r libCommunication.a ADIS16227.o Communication.o
but maybe the fastest & quickest way to achieve a successful build would be:
sudo gcc -o main *.c
so it compiles all the files of the directory into the executable called main
Of course, it makes compilation times longer, but maybe it's not noticeable.
First move into the /home/pi/Desktop/intern/adis16227_generic directory:
cd /home/pi/Desktop/intern/adis16227_generic
Then, compile the source:
gcc ADIS16227.c Communication.c main.c -I .
You can now run your compiled program (called by default a.out):
./a.out
You have to compile separatedly files and then compile main with related obj file.
gcc -c Communication.c Communication.h
gcc main.c Communication.o -o main

undefined reference to `explain_read' ...... No such file or directory

I need to include libexplain to my project to do certain job. I install it and add the header libexplain/read.h to my code, so far so good and no error reported by the complier. But when I use the function explain_read() provided by libexplain and build the project it says:
/tmp/cc7NjAw0.o: In function `xl45_read(int, unsigned char*)':
connections.cpp:(.text+0x2f): undefined reference to `explain_read'
collect2: error: ld returned 1 exit status
and the build script is:
#!/bin/bash
echo > ./stdafx.h
g++ -O1 -Wall -o ./local_proxy (*.cpp...here is the source file list) -lz -lpthread -lpcap -L/usr/local/lib
actually when I type
whereis libexplain
in terminal, I get
libexplain: /usr/lib/libexplain.so /usr/lib/libexplain.a /usr/include/libexplain
I do a lot of searches and still have no idea what's going wrong. ):
You need to link your object files with libexplain. You can do it using the -l<library name>, like so:
g++ -O1 -Wall -o ./local_proxy *.cpp -lz -lpthread -lpcap -lexplain -L/usr/local/lib
Note the -lexplain flag. For a library with the a file name like libABC.so, you'd use -lABC to refer to that library. The documentation for link options with GCC can shed more light on it.

haartraining in open cv 2.4.9 and ubuntu 14.04 LTS

I have tried the tutorial from http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html
I got error while run this command:
g++ `pkg-config --libs --cflags opencv` -I. -o mergevec mergevec.cpp\
cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp\
cvhaartraining.cpp\ -lopencv_core -lopencv_calib3d -lopencv_imgproc -lopencv_highgui -lopencv_objdetect
the error message :
g++: error: mergevec.cppcvboost.cpp: No such file or directory
g++: error: cvhaarclassifier.cppcvhaartraining.cpp-lopencv_core: No such file or directory
then I tried to run the command like this:
g++ `pkg-config --libs --cflags opencv` -I. -o mergevec mergevec.cpp
cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp
cvhaartraining.cpp -lopencv_core -lopencv_calib3d -lopencv_highui -lopencv_objdetect/
and still met the error message like this:
/usr/bin/ld: cannot find -lopencv_highui
/usr/bin/ld: cannot find -lopencv_objdetect/
collect2: error: ld returned 1 exit status
can anyone help me? thank you for your help
I had the same problem when scaling OpenCV haar up and down on different arch's of Ubuntu on EC2 until I found this python version of mergevec in github: https://github.com/wulfebw/mergevec created by wulfebw
It did the trick.
Just use the python version instead of compiling the one in the tutorial. As long as your OpenCV libraries are installed correctly with python, it should work.

undefined reference to `ftdi_init'

I have used libftdi in the past and compiled using the command:
gcc -lftdi -o i2csend i2csend.c
Everything went fine.
Today, on Ubuntu 12.10 I get many errors such as undefined reference toftdi_init'`
I understand that libftdi was renamed to libftdi1 so I tried the same command with -lftdi1 and got error:
/usr/bin/ld: cannot find -lftdi1
collect2: error: ld returned 1 exit status
Can anyone explain why?
You should typically not directly specify external package's library names.
It's better to use the packaging system's help program, i.e. pkg-config, like so:
$ gcc -o i2csend i2csend.c $(pkg-config --cflags --libs libftdi1)
Note that this assumes that the package name is libftdi1 in pkg-config's database; I'm not sure how to verify this portably. You can run pkg-config --list-all | grep ftdi to find out.
It's generally a good idea to keep the libraries part (-l option) at the end of the command line, which the above is doing. It's somewhat cleaner to factor out the CFLAGS part, but that requires repeating the command:
$ gcc $(pkg-config --cflags libftdi1) -o i2csend i2csend.c $(pkg-config --libs libftdi1)
Here, I've used double spaces to separate the logical parts of the command line for improved clarity.

Resources