No rule to make target makefile when using an external library - c++11

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.

Related

How do I tell Cmake where to find gfortran?

I've tried to install DAMASK for study but faced many problems with it. There's nobody at university to help me with these problems, so I'm lookin' for help here.
For installing and working, DAMASK requires Cmake with gcc and gfortran and many programmer things like PETSc, scipy and numpy. I successfully installed all of that using homebrew, but cmake cause trouble. It doesn't seem to find gcc and gfortran at default.
With gcc I used
CC=/usr/bin/gcc
and it worked.
But it doesn't working with FC, 'cause
FC=/usr/bin/gfortran
not working
even
FC=/usr/local/cellar/gcc/10.2.0_4/bin/gfortran-10
not working
ah, I'm using Macos Catalina and the code of the whole errors there:
MacBook-Pro-Andrej:DAMASK admin1$ make spectral
/var/folders/yb/bvc9yx0s2mx4yts18q2hfblw0000gp/T/tmp.XzJ43HLr/Makefile.petsc:4: /lib/petsc/conf/rules: No such file or directory
/var/folders/yb/bvc9yx0s2mx4yts18q2hfblw0000gp/T/tmp.XzJ43HLr/Makefile.petsc:5: /lib/petsc/conf/variables: No such file or directory
make[1]: *** No rule to make target `/lib/petsc/conf/variables'. Stop.
/var/folders/yb/bvc9yx0s2mx4yts18q2hfblw0000gp/T/tmp.XzJ43HLr/Makefile.petsc:4: /lib/petsc/conf/rules: No such file or directory
/var/folders/yb/bvc9yx0s2mx4yts18q2hfblw0000gp/T/tmp.XzJ43HLr/Makefile.petsc:5: /lib/petsc/conf/variables: No such file or directory
make[1]: *** No rule to make target `/lib/petsc/conf/variables'. Stop.
/var/folders/yb/bvc9yx0s2mx4yts18q2hfblw0000gp/T/tmp.XzJ43HLr/Makefile.petsc:4: /lib/petsc/conf/rules: No such file or directory
/var/folders/yb/bvc9yx0s2mx4yts18q2hfblw0000gp/T/tmp.XzJ43HLr/Makefile.petsc:5: /lib/petsc/conf/variables: No such file or directory
make[1]: *** No rule to make target `/lib/petsc/conf/variables'. Stop.
/var/folders/yb/bvc9yx0s2mx4yts18q2hfblw0000gp/T/tmp.XzJ43HLr/Makefile.petsc:4: /lib/petsc/conf/rules: No such file or directory
/var/folders/yb/bvc9yx0s2mx4yts18q2hfblw0000gp/T/tmp.XzJ43HLr/Makefile.petsc:5: /lib/petsc/conf/variables: No such file or directory
make[1]: *** No rule to make target `/lib/petsc/conf/variables'. Stop.
/var/folders/yb/bvc9yx0s2mx4yts18q2hfblw0000gp/T/tmp.XzJ43HLr/Makefile.petsc:4: /lib/petsc/conf/rules: No such file or directory
/var/folders/yb/bvc9yx0s2mx4yts18q2hfblw0000gp/T/tmp.XzJ43HLr/Makefile.petsc:5: /lib/petsc/conf/variables: No such file or directory
make[1]: *** No rule to make target `/lib/petsc/conf/variables'. Stop.
Found PETSC_DIR:
Found PETSC_INCLUDES:
Found PETSC_EXTERNAL_LIB:
Found PETSC_LINKER:
Found MPI Fortran Compiler:
Found MPI C Compiler:
-- The Fortran compiler identification is unknown
CMake Error at CMakeLists.txt:109 (project):
No CMAKE_Fortran_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable "FC" or the CMake cache entry CMAKE_Fortran_COMPILER to the full
path to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
See also "/Users/admin1/DAMASK/CMakeFiles/CMakeOutput.log".
See also "/Users/admin1/DAMASK/CMakeFiles/CMakeError.log".
make: *** [build/spectral] Error 1
I don't know anything in UNIX programming but I was told to do it if I want to write my
thesis and my diploma...
So I'm lookin' for help here...

Adding ffmpeg to clion project

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.

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>

Pachi cant find caffe/caffe.hpp: No such file or directory

I am attempting to compile Go/Baduk engine Pachi (https://github.com/pasky/pachi) with Caffe (http://caffe.berkeleyvision.org) support.
Caffe install is OK and its located in /something/PACHI_GO_ENGINE/caffe/
caffe.hpp is located in:
/something/PACHI_GO_ENGINE/caffe/distribute/include/caffe/caffe.hpp
/something/PACHI_GO_ENGINE/caffe/include/caffe/caffe.hpp
I also updated the Pachi Makefile as instructed:
- Edit Makefile, set DCNN=1, point it to where caffe is installed and build.
I even made a copy of ...include/caffe/ to /usr/local/lib because this supposed to be a default location.
Makefile changes:
DCNN=1
#CAFFE_LIB=/usr/local/lib
CAFFE_LIB=/something/PACHI_GO_ENGINE/caffe/distribute/include
#(and several variations of this)
And I still get:
[CC] fbook.c
[CC] chat.c
[CXX] dcnn.cpp
dcnn.cpp:13:27: fatal error: caffe/caffe.hpp: No such file or directory
compilation terminated.
Makefile.lib:78: recipe for target 'dcnn.o' failed
make: *** [dcnn.o] Error 1
CAFFE_LIB variable is to point to the path where caffe.so or caffe.a is present. In your case, the dcnn.cpp cannot find the header files.
Though the header files are present in /something/PACHI_GO_ENGINE/caffe/distribute/include they won't be detected by pachi because, header file paths are mentioned using the variable INCLUDES. You will have to append this path to the INCLUDES variable and not the CAFFE_LIB.

Trying to add Boost libraries to Cmake.txt (Clion Ide)

I see a lot of questions that are similar to this though not the same.
I'm having an issue getting boost recognized/working in Cmake using Clion I've tried a handful of ways including the incboost template included with Clion, moving on.
Cmake version 3.4
Boost version 1.60.0
find_package(Boost)
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
endif()
That's the auto generated incboost I mentioned above, This produces no results.
Second attempt is the following
set(Boost_Dir "C:\\Program Files (x86)\\boost_1_60_0\\")
find_package(Boost 1.60.0 COMPONENTS filesystem)
include_directories(${Boost_Dir})
CmakeFiles\Svp.dir/objects.a(main.cpp.obj):
In function`_static_initialization_and_destruction_0':
C:/PROGRA~2/BOOST_~1/boost/system/error_code.hpp:221: undefined reference
to `boost::system::generic_category()'
C:/PROGRA~2/BOOST_~1/boost/system/error_code.hpp:222: undefined
reference to `boost::system::generic_category()'
C:/PROGRA~2/BOOST_~1/boost/system/error_code.hpp:223: undefined
reference to `boost::system::system_category()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Svp.exe] Error 1
CMakeFiles\Svp.dir\build.make:96: recipe for target 'Svp.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Svp.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/Svp.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Svp.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/Svp.dir/rule] Error 2
Makefile:117: recipe for target 'Svp' failed
mingw32-make.exe: *** [Svp] Error 2
My Third attempt produces this
set(boost "C:\\Program Files (x86)\\boost_1_60_0\\boost\\")
INCLUDE_DIRECTORIES(${boost})
fatal error: boost/filesystem/config.hpp: No such file or directory
# include <boost/filesystem/config.hpp>
^
compilation terminated.
mingw32-make.exe[3]: *** [CMakeFiles/Svp.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Svp.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Svp.dir/rule] Error 2
And finally the solutions given here How do you add boost libraries in CMakeLists.txt have not changed my error output.
here's how I use it, in a portable way, in my projects:
if (WIN32)
set(BOOST_ROOT "C:/Program Files (x86)/boost_1_60_0/")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREAD ON)
include_directories(${BOOST_ROOT})
link_directories(${BOOST_ROOT}/stage/lib) # add this before add_executable()
endif()
# Here call your add_executable method
# add_executable(TARGET_NAME ... )
if(NOT MSVC)
find_package(Boost REQUIRED COMPONENTS date_time filesystem wserialization system serialization thread regex)
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(TARGET_NAME ${Boost_LIBRARIES})
endif()
endif()
You sometime need to specify the Boost_USE_STATIC_LIBS and Boost_USE_MULTITHREAD variables to help find_package find boost.
Also please specify the correct path to boost library with set(BOOST_ROOT, $path). Moreover, specify the boost packages you need in find_package. For instance
find_package(Boost REQUIRED COMPONENTS date_time filesystem thread regex)
if you only need the date_time filesystem thread regex packages
let me know if it works or not for you.

Resources