Strange linker error while compiling OpenCV2.3.1 on Ubuntu 11.10 - opencv

I'm trying to compile OpenCV version 2.3.1 on an Ubuntu 11.10 following instructions described here. I'm getting following error. Can't understand what is happening... /usr/local/lib/libavcodec.a exists but linker can't link against it, or something else?
error:
[ 20%] Built target pch_Generate_opencv_highgui
Linking CXX shared library ../../lib/libopencv_highgui.so
/usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32S against `av_destruct_packet'
can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libavcodec.a: could not read symbols: Bad value
collect2: ld returned 1 exit status

The problem is that you are attempting to link libopencv_highgui.so with libavcodec.a. The latter is built from code compiled without -fPIC (which is quite usual), and such code can not be linked into shared libraries on x86_64.
Your choices are:
Obtain libavcodec.so and arrange to link against it, or
Remove libavcodec or -lavcodec from the link line completely.
For the first, you most likely just need to install libavcodec-dev package.
If you do the second, you will still have to arrange for symbols that libopencv_highgui.so needs from libavcodec to be available at runtime. You can achieve that by linking the main executable with libavcodec (either archive or shared variant).

my take would be that, first run sudo apt-get remove libavcodec , then re-install with sudo apt-get install libopencv-dev
I once had similar issue, and the above resolved it

Running a 64-bit version of Ubuntu you have to configure and build ffmpeg with
./configure --enable-shared --enable-pic
as it is described in step 7b and 8b

Related

Building FFmpeg for use in Swift

With the new XCode/Swift release comes the ability to use binary dependencies. This seems to me to be an ideal time to create an SPM package for FFMpeg.
However, while I've spent the last year learning to code i Swift, I'm actually not all that familiar with how to build libraries, especially those as complex as FFmpeg with all the configurable libraries and third-party dependencies.
There's kewlbear's iOS build scripts, but these are for iOS/tvOS and ideally an FFMpeg SPM package would be usable for MacOS also. It's also not updated for the newest Xcode and Swift versions.
My personal interest is simply in audio and I don't need a lot of bells and whistles, but I figure the ideal situation would be a full package with the entire source and whatever dependencies it needs, and then when it's used as a package dependency, the compiler will just use the parts it needs.
I guess my question is...how would I ideally compile the ffmpeg code for this purpose. I'm trying to follow the directions for compiling yourself, but I'm stuck at the point of compiling gettext because I'm not sure if I should follow the directions (in the gettext source code) for compiling a fat binary for multiple architectures, and when I try to run:
./configure CC="gcc -arch i386 -arch x86_64 -arch ppc -arch ppc64" \
CXX="g++ -arch i386 -arch x86_64 -arch ppc -arch ppc64" \
CPP="gcc -E" CXXCPP="g++ -E"
I get the following error:
checking whether the C compiler works... no configure: error: in
`/Users/nolainecrusher/Downloads/FFMpeg-source/gettext-0.21/gettext-runtime':
configure: error: C compiler cannot create executables See
`config.log' for more details configure: error: ./configure failed for
gettext-runtime
and config.log doesn't really tell me anything useful:
This is what I see at the end of the log:
mkdir_p='$(MKDIR_P)' oldincludedir='/usr/include' pdfdir='${docdir}'
prefix='/usr/local' program_transform_name='s,x,x,' psdir='${docdir}'
sbindir='${exec_prefix}/sbin' sharedstatedir='${prefix}/com' subdirs='
gettext-runtime libtextstyle gettext-tools' sysconfdir='${prefix}/etc'
target_alias=''
I feel like maybe I'm going about this the wrong way, but I'm not sure what the right way is.
See the following:
A Swift wrapper package for FFmpeg and
A C wrapper package for FFmpeg
SwiftFFmpeg assumes ffmpeg is installed locally and will link with those libraries. Also, SwiftFFmpeg uses CFFmpeg as a package dependency, and your app can use SwiftFFmpeg as a dependency.
If you want to see a build script for compiling ffmpeg (x86_64 and arm64), check out my own audio player project.

Problems linking FFTW with gfortran (symbol(s) not found for architecture x86_64)

I am on macOS Catalina (10.15.5) and I have installed FFTW using brew install fftw. I also have GCC (version 10.2.0) installed which is symlinked to the commands I am using below.
I am able to compile and run a simple C++ program like so:
g++-10 test.cpp -L/usr/local/lib -I/usr/local/include -lfftw3 -lm
But, I had to modify the environment variable CPATH to make this work.
I am unable to get a simple Fortran FFTW example to work (I am using this one). The command I am using to compile is:
gfortran-10 test.f90 -L/usr/local/lib -I/usr/local/include -lfftw3 -lm
The error I get is as follows:
Undefined symbols for architecture x86_64:
"__gfortran_os_error_at", referenced from:
_MAIN__ in ccRvJaEQ.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
If I understand correctly, the linking step fails. Any inputs on how I might find the relevant paths that I may have to pass to the compiler would be very helpful. I searched for solutions and I wasn't able to find one. But, in case I missed something obvious, I apologise.
Thanks to msi_gerva's helpful hints through comments, I was able to solve the issue by downloading FFTW and compiling it to a custom directory following the installation directions.
Now, the linking doesn't fail if I pass the new locations of the libraries to gfortran like so:
gfortran-10 test.f90 -L/new/path/to/lib -I/new/path/to/include -lfftw3 -lm
Since /usr/local/ is the default installation path, it's possible that there is a mixing up of the FFTW library files from previous installations.

Problems Building MPFR using iOS SDK 8.3

I am having trouble building MPFR for iOS, armv7s architecture. I am using this command after successfully building GMP,
./configure CC=clang CPP="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -E" CPPFLAGS="-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.3.sdk/ -miphoneos-version-min=8.0 -arch armv7s -target arm-apple-darwin" --host=aarch64-apple-darwin --disable-assembly --enable-static --disable-shared
However, I configure is giving me the error
libgmp not found or uses a different ABI.
I built GMP with the same configure settings as above, then make, make install, etc. After this, I copied the gmp.h file and libgmp.la file to
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.3.sdk/usr/include/
and
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.3.sdk/usr/lib
respectively, but I get the same error.
Any ideas?
Okay, I think being tired got to me, and I was using an x86_64 version of GMP to try to compile the armv7 mpfr. I retried today, and everything worked. Make sure to move the gmp header and the libgmp.a file to the iOS sdk directories as mentioned in the question, else you will get an error at configure time. Other than that, it should work.

GMP Library for iOS

I have a GMP Library that is used for BIG integer calculation in C++.
I am unable to compile the same in Xcode for iOS development.
Please Provide with details steps regarding how to compile GMP for iOS Development
I realize that there has been some considerable time gone by but none of the information in the previous answer worked for me. Here is how I was able to do it, hope this helps someone else in the future.
Grab the gmp snapshot from https://gmplib.org/download/snapshot/
It is in .lz format, it will need to be uncompressed. if you have a mac, uncompress it using lzip, install with homebrew brew install lzip
Once its uncompressed from lzip, then it will be a .tar file, the finder can take care of that for you. You will have a gmp folder with a bunch of files and other sub-folders.
You will need Xcode commandline tools installed. Make sure you have that pre-installed.
Run this configure line. Take care to note that you need to put in your path to the directory you want it to be stored, in --prefix.
./configure CC="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -fembed-bitcode -arch arm64e --sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk" CPPFLAGS="-fembed-bitcode -arch arm64e --sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk " --disable-shared --enable-static --host=arm-apple-darwin --disable-assembly --prefix="{ YOUR_PATH_HERE }/GMP_6_1_99/gmp-6.1.99-20200117/gmplib-iphoneos-arm64e"
Then you run (notice the same path as before for the log files)
make -j &> "{ YOUR_PATH_HERE }/gmp-6.1.99-20200117/gmplib-iphoneos-arm64e-build.log"
make install &> "{ YOUR_PATH_HERE }/gmp-6.1.99-20200117/gmplib-iphoneos-arm64e-install.log"
There are two main points to those lines that are changeable, the platform, and the architecture. The above uses the arm64e architecture but you can just as easily build for arm64, armv7, armv7s. Also you can change out the platform as well and build for macosx, watchos, iphonesimulator, or appletvos. To change the platform you need to find the sdk of where it lives on your machine. The other architecture options are x86_64 i386, To do that, type this into your terminal...
xcrun --sdk iphoneos --show-sdk-path
(replace "iphoneos" with the platform you are looking to use from above)
And replace the SDK path. Make sure you match with the correct architecture or else you will get an error, cannot find compiler.
As GenieWanted said, provide more information!!!
However, I compiled successfully the GMP library for iOS 7 64 bits following the next step:
./configure
CC="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang"
CPP="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
-E" CPPFLAGS="-target arm64-apple-darwin -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk/
-miphoneos-version-min=7.0" --host=aarch64-apple-darwin --disable-assembly --enable-static --disable-shared --prefix=/your-path/
I would like to point out that it only creates the C library, I do not think is different for C++, however I will try this afternoon.
#bolnad's answer worked for me doing a small modification, removing the -fembed-bitcode flag both from CC and CPPFLAGS.
EDIT: it ended up not correctly compiling the different architectures, so I found this script on github https://github.com/FlowCrypt/GMP and was able to build correctly.

Installing OpenCV 3.0.0-dev, issue with configuration options

I know OpenCV 3.0 is not a release version, yet. I'm trying to evaluate a few features in the 3.0 version for a prototype system we are building. I cloned the latest source code from the git repository and I use the following cmake to build the libraries:
cmake -D BUILD_SHARED_LIBS=ON -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
However, I encounter this issue when it builds the libraries when i do a make:
/usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32S against `av_destruct_packet' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/libavcodec.a: could not read symbols: Bad value
collect2: error: ld returned 1 exit status
make[2]: *** [lib/libopencv_highgui.so.3.0.0] Error 1
make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
I'm not quite sure where to set the -fPIC flag in the CMakeLists.txt. Just a point to note, I have OpenCV 2.4.8 libraries already built and running on my machine. Thanks for taking time to help me out!
Thanks for the answer. I managed to install OpenCV 3.0.0-dev version by removing my existing OpenCV installation. Although this was not required, I removed my previous 2.4.8 library files and managed to compile it. CCMake helped make the decision for the installation much simpler. Use that to decide which libraries you'd like to compile before running make.

Resources