Adding ffmpeg to clion project - ffmpeg

I would liket to add ffmpeg to Clion but I have some problems with it.
My MakeLists.txt looks liek this:
cmake_minimum_required(VERSION 3.10)
project(ffmpeg)
set(CMAKE_CXX_STANDARD 11)
include_directories(libs/ffmpeg/)
include_directories(libs/ffmpeg/include/libavutil/)
include_directories(libs/ffmpeg/include/libaccodec/)
include_directories(libs/ffmpeg/include/libavdevice/)
include_directories(libs/ffmpeg/include/libavfilter/)
include_directories(libs/ffmpeg/include/libavformat/)
include_directories(libs/ffmpeg/include/)
link_directories(libs/ffmpeg/lib/)
set(SOURCE_FILES main.cpp)
add_executable(ffmpeg main.cpp)
target_link_libraries(
ffmpeg
avcodec
avdevice
avfilter
avformat
avresample
avutil
postproc
swresample
swscale
)
I am not sure if I added the libraries and includes in the right way, beacause in my simple main.cpp it can't resolve avcodec_configuration().
My project layout looks as follows:
ffmpeg
-libs
-include
-libavcodec
.
.
.
-lib
-avcodec.lib
-main.cpp
EDIT:
Now all includes are found by the compiler. BUt if I compile I get following error:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lavresample
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [ffmpeg.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/ffmpeg.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
CMakeFiles\ffmpeg.dir\build.make:96: recipe for target 'ffmpeg.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/ffmpeg.dir/all' failed
Makefile:82: recipe for target 'all' failed

I don't recommend to hardcode paths to the headers/libraries in your CMakeLists.txt; this is not portable.
Search for a ready FindFFmpeg.cmake (maybe this one will work?),
add it to your project directory (e.g. into a cmake/ subdirectory),
and connect it to CMAKE_MODULE_PATH. E.g. list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake").
Then refer to ${FFMPEG_LIBRARIES}, ${FFMPEG_INCLUDE_DIRS} and ${FFMPEG_DEFINITIONS}.
Sometimes you have to fix up the FindXXX.cmake modules.

I was able to include the precompiled ffmpeg library to cmake project by creating find cmake scripts and including them in main CMakeLists.txt.
Full example is here : https://github.com/tomkordic/Java-native-HTTP/tree/master/src/main/cpp
In my project a FFMPEG_INCLUDE_DIRECTORY and FFMPEG_LIB_DIRECTORY are passed by gradle build script you can set these manually or discover them in some other way.
FFMPEG_INCLUDE_DIRECTORY is a path to ffmpeg installation include directory, in my case:
/mnt/7fab2260-fb19-41a7-ac7c-816bab2f3b92/install/ffmpeg_build/include
FFMPEG_LIB_DIRECTORY is a path to ffmpeg installation lib directory, in my case:
/mnt/7fab2260-fb19-41a7-ac7c-816bab2f3b92/install/ffmpeg_build/lib
Make sure to set CMAKE_MODULE_PATH to the directory where your find cmake scripts are located.
Build system is for ubuntu 18.04.

Related

Cannot find a link library (lNrrdIO)

I am trying to install NrrdIO on Ubuntu 18.04, to run Marching Cubes to segment medical images. This is the link from which I'm trying to run it.
http://web.cse.ohio-state.edu/research/graphics/isotable/
I'm trying to install ijkmcube-v0-3-3.tar, which requires the ITKNrrdIO.a library. I'm running into this error:
[ 7%] Linking CXX executable ijkmcube
/usr/bin/ld: cannot find -lNrrdIO
collect2: error: ld returned 1 exit status
CMakeFiles/ijkmcube.dir/build.make:406: recipe for target 'ijkmcube' failed
make[2]: *** [ijkmcube] Error 1
CMakeFiles/Makefile2:131: recipe for target 'CMakeFiles/ijkmcube.dir/all' failed
make[1]: *** [CMakeFiles/ijkmcube.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2
I've installed NrrdIO 1.11.0, and tried with NrrdIO 1.9.0 as well, but while running
make
I always run into this error, for some reason its not able to find lNrrdIO . Can someone please help?
Thanks
Edit:
I think its a problem with the linking, but when I copied the NrrdIO.a file to /usr/bin and modified the symbolic link to point to it, I got an error as follows:
[ 7%] Linking CXX executable ijkmcube
collect2: fatal error: cannot find 'ld'
compilation terminated.
CMakeFiles/ijkmcube.dir/build.make:406: recipe for target 'ijkmcube'
failed
make[2]: *** [ijkmcube] Error 1
CMakeFiles/Makefile2:131: recipe for target '
CMakeFiles/ijkmcube.dir/all' failed
make[1]: *** [CMakeFiles/ijkmcube.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2
Can someone help? Thank you
Edit 2
PROJECT(IJKMCUBE)
#---------------------------------------------------------
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
IF (NOT DEFINED ${IJK_DIR})
GET_FILENAME_COMPONENT(IJK_ABSOLUTE_PATH "../.." ABSOLUTE)
SET(IJK_DIR ${IJK_ABSOLUTE_PATH} CACHE PATH "IJK directory")
ENDIF (NOT DEFINED ${IJK_DIR})
SET(CMAKE_INSTALL_PREFIX "${IJK_DIR}/")
SET(LIBRARY_OUTPUT_PATH ${IJK_DIR}/lib CACHE PATH "Library directory")
SET(IJKMCUBE_DIR "src/ijkmcube")
SET(NRRD_LIBDIR "${IJK_DIR}/lib")
SET(IJK_ISOTABLE_DIR "${IJK_DIR}/isotable" CACHE PATH "Isotable
directory")
#---------------------------------------------------------
IF (NOT CMAKE_BUILD_TYPE)
SET (CMAKE_BUILD_TYPE Release CACHE STRING
"Default build type: Release" FORCE)
ENDIF (NOT CMAKE_BUILD_TYPE)
INCLUDE_DIRECTORIES("${IJK_DIR}/include")
LINK_DIRECTORIES("${NRRD_LIBDIR}")
LINK_LIBRARIES(expat NrrdIO z)
ADD_DEFINITIONS(-DIJK_ISOTABLE_DIR=\"${IJK_ISOTABLE_DIR}\")
ADD_EXECUTABLE(ijkmcube ijkmcube_main.cxx ijkmcubeIO.cxx ijkmcube.cxx
ijkmcube_datastruct.cxx ijkmcube_sub.cxx
ijkmcube_extract.cxx ijkmcube_util.cxx
ijksnapmc.cxx
ijktable.cxx ijktable_poly.cxx ijktable_ambig.cxx
ijkoctree.cxx ijkxitIO.cxx)
ADD_LIBRARY(ijkmcubeL STATIC EXCLUDE_FROM_ALL ijkmcubeIO.cxx
ijkmcube.cxx ijkmcube_datastruct.cxx ijkmcube_sub.cxx
ijkmcube_extract.cxx ijkmcube_util.cxx ijksnapmc.cxx
ijktable.cxx ijkoctree.cxx ijkxitIO.cxx)
SET_TARGET_PROPERTIES(ijkmcubeL PROPERTIES OUTPUT_NAME ijkmcube)
ADD_CUSTOM_TARGET(lib DEPENDS ijkmcubeL)
SET(CMAKE_INSTALL_PREFIX ${IJK_DIR})
INSTALL(TARGETS ijkmcube DESTINATION "bin/linux")
ADD_CUSTOM_TARGET(tar WORKING_DIRECTORY ../.. COMMAND tar cvfh
${IJKMCUBE_DIR}/ijkmcube.tar ${IJKMCUBE_DIR}/README
${IJKMCUBE_DIR}/INSTALL ${IJKMCUBE_DIR}/RELEASE_NOTES
${IJKMCUBE_DIR}/*.cxx ${IJKMCUBE_DIR}/*.h ${IJKMCUBE_DIR}/*.txx
${IJKMCUBE_DIR}/CMakeLists.txt ${IJKMCUBE_DIR}/man/*
${IJKMCUBE_DIR}/ijkmcube_doxygen.config)
ADD_CUSTOM_TARGET(doc COMMAND doxygen ijkmcube_doxygen.config)
SOLVED
Tsyvarev solved it, the libNrrdIO.a file has to be copied to /usr/lib/. Refer to comments for the exact solution
This answer was given by #Tsyvarev:
By making a link named ld you are removing the original linker (ld). Without the linker you definitely cannot build any library. What you need is to make a link named lNrrdIO.a, so it will point to the actual library location: sudo ln -sfn /home/subham/Downloads/NrrdIO-1.9.0-src/lNrrdIO.a lNrrdIO.a (run this command from /usr/lib directory). And restore original /usr/bin/ld file.

Caffe build gives GCC Link Error "can not be used when making shared object.; recompile with -fPIC"

I'm trying to install caffe, using CMake, but when I run make all (after running cmake .. from a build directory) I get the following error:
me#gimli:~/Downloads/caffe/build$ make all
[ 1%] Built target caffeproto
[ 1%] Linking CXX shared library ../../lib/libcaffe.so
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libleveldb.a(db_impl.cc.o): relocation R_X86_64_PC32 against symbol `_ZN7leveldb10EnvWrapper8ScheduleEPFvPvES1_' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
src/caffe/CMakeFiles/caffe.dir/build.make:40060: recipe for target 'lib/libcaffe.so.1.0.0' failed
make[2]: *** [lib/libcaffe.so.1.0.0] Error 1
CMakeFiles/Makefile2:267: recipe for target 'src/caffe/CMakeFiles/caffe.dir/all' failed
make[1]: *** [src/caffe/CMakeFiles/caffe.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2
I don't really understand CMake, but gather that somewhere I'm supposed to add -fPIC to a gcc command. But, I have no idea where I should make this change, or if there's somewhere in Cmake where I should tell it to construct the gcc command correctly.
How can I force CMake to create/use a gcc command with the -fPIC option, or is there something else entirely I should be doing?
The error is not from CMake but from the linker. It actually tells, that:
You cannot build shared library libcaffe.so with PIC (Position independent code) feature and link it with the static library libleveldb.a compiled without this feature.
Possible solutions are:
Get shared version of the static library (libleveldb.a in your case), so it will be compiled with PIC. This is what the error message suggests you.
Instead of building shared library (Caffe in your case), build static one, without using of PIC. Note, that in this case your will face with similar issues when trying to use resulted library in the future shared libraries.
For most CMake projects forcing the building static libraries can be performed with:
cmake -DBUILD_SHARED_LIBS=OFF <other parameters>
Strictly speaking, PIC feature is independent from the type (shared or static) of a library. So you may have a static library with PIC, or build a shared library without it.
For many CMake projects you may control PIC feature of the created libraries with
cmake -DPOSITION_INDEPENDENT_CODE=<ON|OFF> <other parameter>

Create a shared lib that depends on dlib using Cmake

I'm building a shared library in C++ that depends on dlib using CMake.
While it has been possible for me to build and install a shared dlib using make and make install, so far I haven't figured out the way to link to this shared dlib library.
The examples of usage given in the DLib website always link to a static library.
This is what I have so far:
cmake_minimum_required(VERSION 2.8.12)
project(face_align)
set(CMAKE_CXX_STANDARD 11)
find_package(dlib)
add_library(face_align SHARED src/mylib.cpp)
target_link_libraries(face_align dlib::dlib)
The linker complains like so:
/usr/bin/ld: cannot find -ldlib::dlib
collect2: error: ld returned 1 exit status
CMakeFiles/face_align.dir/build.make:94: recipe for target 'libface_align.so' failed
make[2]: *** [libface_align.so] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/face_align.dir/all' failed
make[1]: *** [CMakeFiles/face_align.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
If I remove the SHARED from
add_library(face_align SHARED src/mylib.cpp) the project builds successfully. This makes me think that dlib:dlib is pointing to the static library. I see that there is a dlib:dlib_shared but no success linking to this one either.
The answer seems to be (1) linking to dlib as a static library (2) making the code linking to dlib position independent:
cmake_minimum_required(VERSION 2.8.12)
project(face_align)
set(CMAKE_CXX_STANDARD 11)
# Unfortunately, this only links dlib as static library
add_subdirectory(libs/dlib dlib_build)
add_library(face_align SHARED src/mylib.cpp)
# This makes the target position independent, allowing to link an static library to a dynamic one.
set_target_properties(face_align PROPERTIES POSITION_INDEPENDENT_CODE ON) # This made it work
# Link Dlib
target_link_libraries(face_align dlib::dlib)
EDIT:
Also, in Linux (I haven't tested this in other S.O.s) CMake does some magic and, after installing dlib library in shared mode:
$ cd $DLIB
$ mkdir build && cd build
$ cmake ../dlib
$ make && make install
Is possible to simply add the library using target_link_libraries as follows:
cmake_minimum_required(VERSION 2.8.12)
project(face_align)
set(CMAKE_CXX_STANDARD 11)
add_library(face_align SHARED src/mylib.cpp)
# Link Dlib
target_link_libraries(face_align dlib)
Then, in your code, include headers as follows:
#include <dlib/your_dlib_header_here.h>

How to link jemalloc shared library using cmake

I'm trying to link the jemalloc library into my application at build time using it as a generic implementation. According to https://github.com/jemalloc/jemalloc/wiki/Getting-Started the linking flags to use are:
-L`jemalloc-config --libdir` -Wl,-rpath,`jemalloc-config --libdir` -ljemalloc `jemalloc-config --libs`
So I did the following CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.12.2)
project(widget)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_executable(widget ${SOURCES})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L`jemalloc-config --libdir` -Wl,-rpath,`jemalloc-config --libdir` -ljemalloc `jemalloc-config --libs`")
But when I do make I get the following errors:
Linking CXX executable widget
c++: error: `jemalloc-config: No such file or directory
c++: error: unrecognized command line option ‘--libdir`’
c++: error: unrecognized command line option ‘--libdir`’
c++: error: unrecognized command line option ‘--libs`’
make[2]: *** [widget] Error 1
make[1]: *** [CMakeFiles/widget.dir/all] Error 2
For future generations, as this still comes up as one of the first links on Google.
Jemalloc comes with pkg-config setup, which can be used like this:
find_package(PkgConfig REQUIRED)
pkg_check_modules (JEMALLOC jemalloc)
pkg_search_module(JEMALLOC REQUIRED jemalloc)
include_directories(${JEMALLOC_INCLUDE_DIRS})
target_link_libraries(your_target_name ${JEMALLOC_LIBRARIES})
execute_process() command is your friend. Use it to run jemalloc-config executable and then put its output into CMake variables.
find you root_dir of jemalloc. Mine is /Users/lion/homebrew/Cellar/jemalloc/5.2.1_1/lib/
(I install jemalloc by brew on macOS)
link (soft link) all its lib to your local lib ln -s /Users/lion/homebrew/Cellar/jemalloc/5.2.1_1/lib/* /usr/local/lib
Then it works!

No rule to make target makefile when using an external library

I have attempted to troubleshoot this issue, but eventually I gave up and I cannot figure it out.
I am using CLion and I need to import an external shared library (ts3client.so). Whatever I do, it fails in one way or another.
Currently, I have the following:
cmake_minimum_required(VERSION 3.8)
project(TSMusicBot)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES src/main.cpp)
# Teamspeak Libraries
INCLUDE_DIRECTORIES(libs/ts3_sdk_3.0.4/include)
ADD_LIBRARY (libts3client SHARED IMPORTED GLOBAL)
SET_PROPERTY (
TARGET libts3client PROPERTY IMPORTED_LOCATION
libs/ts3_sdk_3.0.4/bin/linux/amd64/libts3client.so)
add_executable(TSMusicBot ${SOURCE_FILES})
target_link_libraries(TSMusicBot libts3client)
This gives me the following error:
make[2]: *** No rule to make target 'libs/ts3_sdk_3.0.4/bin/linux/amd64/libts3client.so', needed by 'TSMusicBot'. Stop.
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/TSMusicBot.dir/all' failed
make[1]: *** [CMakeFiles/TSMusicBot.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
My directory structure is the following:
src/main.cpp (the code)
libs/ts3_sdk_3.0.4/include/teamspeak/ (which contains headers for the library)
libs/ts3_sdk_3.0.4/bin/linux/amd64/libts3client.so (which is the library I cannot import).
The problem is here:
SET_PROPERTY (
TARGET libts3client PROPERTY IMPORTED_LOCATION
libs/ts3_sdk_3.0.4/bin/linux/amd64/libts3client.so)
You have a plain relative path there, which often will cause problems with CMake because it will run your commands in different directories than the one where CMakeLists.txt exists.
As we discovered in a comment, changing the IMPORTED_LOCATION to an absolute path fixes it. However, the proper solution is to use a relative path with a known base:
SET_PROPERTY (
TARGET libts3client PROPERTY IMPORTED_LOCATION
${CMAKE_CURRENT_SOURCE_DIR}/libs/ts3_sdk_3.0.4/bin/linux/amd64/libts3client.so)
This says explicitly that the libs directory is under the directory where this CMakeLists.txt file is.

Resources