How to link prebuilt .so files using CMakeLists.txt - c++

I am trying to use cmake with android studio to compile c++ code, and I have a .so library which needs to be linked with the target.
I am new to cmake and this is what I have:
add_library(
my_target
SHARED
${SRCS}
)
find_library(
SSL
apex_fips_libs/libccmssl.so
)
target_link_libraries(
my_target
${SSL}
)
Folder structure:
-- Src
-- some.cpp
-- some2.cpp
-- CMakeLists.txt
-- apex_fips_libs
--libccmssl.so
But I am getting following error:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
SSL
I understand that SSL is not being set as find_library does not find the .so, but why?
I even tried using the absolute path. Can someone point me to the correct way of doing this?
Thank You.

If you're guaranteed to have that library there already built then perhaps this is the easiest solution
target_link_libraries(my_target ${CMAKE_SOURCE_DIR}/apex_fips_libs/libccmssl.so)
Otherwise you might want to try something more like
find_library(SSL NAMES ccmssl PATHS ${CMAKE_SOURCE_DIR}/apex_fips_libs)
target_link_libraries(my_target ${SSL})

Related

Imported target "Boost::system" includes non-existent path "/include"

I am a newbie with CMake please bear with me. I have a library (libvpop) which I created in c++ using some Boost components (system and date_time). I can link to it without a problem in windows but on Ubuntu, I am getting an error that implies the path to the boost include files cannot be found. Here is the simple CMakeLists.txt file.
cmake_minimum_required(VERSION 3.0.0)
set (Boost_DEBUG 1)
project(vpoplibuser)
find_package(fmt CONFIG REQUIRED)
find_package(Boost CONFIG REQUIRED system )
find_package(Boost CONFIG REQUIRED date_time)
add_executable(vpoplibuser vpoplibuser.cpp vpoplib.h)
find_library(VPLIB libvpop HINTS ~/projects/vpoplibuser/ )
message(STATUS "VPLib include dir: ${VPLIB}")
target_include_directories(vpoplibuser PUBLIC ${PROJECT_SOURCE_DIR} )
target_link_libraries(vpoplibuser PUBLIC ${VPLIB})
target_link_libraries(vpoplibuser PRIVATE fmt::fmt)
target_link_libraries(vpoplibuser PRIVATE Boost::system Boost::date_time)
When I run CMake, I get message:
CMake Error in CMakeLists.txt
Imported target "Boost::system" includes non-existent path "/include"
in its INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:
The path was deleted, renamed, or moved to another location.
An install or uninstall procedure did not complete successfully.
The installation package was faulty and references files it does not provide.
I have removed and reinstalled Boost. My Boost libraries are at /lib/x86_64-linux-gnu. I cannot figure out exactly where CMake is searching for the boost include file. When I inspect the _BOOST_INCLUDEDIR variable in boost_header-1.71.0/boost_headers-config.cmake it tells me _BOOST_INCLUDEDIR is "/include". I have read something about the PATH variable being an issue so I added /usr to the beginning of my PATH (there is a folder /usr/include/boost which has the boost .hpp files so I was making an assumption that is what CMake is looking for). I have been stuck on this for a couple of days so I would appreciate any advice from the expert community.
I have found a work around thanks to this article: https://github.com/VowpalWabbit/vowpal_wabbit/issues/3003
Something in the Boost cmake process is causing boost to look for the include files at /include when they are really at /usr/include. I created a symbolic link for /include to point to /usr/include and this allowed cmake to find everything. I have not solved the root cause but can move forward with this approach.

cmake cannot find library when linking library to another application

I am trying to make generate a shared library of an existing application, so that i can link it to gtest application.
My main applications cmake file looks like this:
project(audiodLib CXX C)
cmake_minimum_required(VERSION 2.8.7)
## Lets store all the source code in ${SOURCES}
file(GLOB SOURCES src/*.cpp src/controls/*.cpp src/controls/pulse/*.cpp src/modules/*.cpp src/product/*.cpp src/umi/*.cpp src/umi/modules/*.cpp src/umi/soundSettings/*.cpp src/utils/*.cpp utils/*.cpp pmtrace/*.c)
##Lets generate the library
##Please note, here instead of ${SOURCES} if i try to directly add source code, I always get
##an error saying that cmake could not find any *.cpp files
add_library(audiodLib SHARED ${SOURCES})
##Lets link it with libraries
target_link_libraries(audiodLib ${GLIB2_LDFLAGS}
${LUNASERVICE_LDFLAGS}
${PBNJSON_C_LDFLAGS}
${LUNAPREFS_LDFLAGS}
${POWERD_LDFLAGS}
${PMLOGLIB_LDFLAGS}
${NYXLIB_LDFLAGS}
${LIBPBNJSON_LDFLAGS}
${PULSE_LDFLAGS}
${LTTNG_UST_LDFLAGS}
${URCU_BP_LDFLAGS}
${PULSE_SIMPLE_LDFLAGS}
${WEBOSI18N_LDFLAGS}
pthread
rt
dl
-lsnapshot-boot
)
##Lets make this library availabel for other modules
install(TARGETS audiodLib LIBRARY DESTINATION ${WEBOS_INSTALL_LIBDIR})
After compilation, libaudiodLib.so is generated in /usr/lib directory.
And now if I try to access the audiodLib in my gtest code like this:
##${WEBOS_INSTALL_LIBDIR} = /usr/lib/
include_directories(${WEBOS_INSTALL_LIBDIR})
target_link_libraries(${GTEST_EXECUTABLE}
${WEBOS_GTEST_LIBRARIES}
${GLIB2_LDFLAGS}
${LUNASERVICE_LDFLAGS}
${PBNJSON_C_LDFLAGS}
${LUNAPREFS_LDFLAGS}
${POWERD_LDFLAGS}
${PMLOGLIB_LDFLAGS}
${NYXLIB_LDFLAGS}
${LIBPBNJSON_LDFLAGS}
${PULSE_LDFLAGS}
${LTTNG_UST_LDFLAGS}
${URCU_BP_LDFLAGS}
${PULSE_SIMPLE_LDFLAGS}
${WEBOSI18N_LDFLAGS}
pthread
rt
dl
-lsnapshot-boot
-laudiodLib
)
I get the following error:
cannot find -laudiodLib
The folder structure is as follows:
audiod/
CMakeList
|src
|tests
CMakelist
If someone can point out what I am doing wrong, it would be of great help. I am kind of stuck and clueless after spending 2-3 days on this.
Thank you so very much for offering me suggestions, and pointers, I was finally able to sove the problem.
Regarding library not found issue, I resolved it by rearranging the TARGET_LINK_LIBRARIES as follows:
target_link_libraries(audiod
audiodLib
${GLIB2_LDFLAGS}
${LUNASERVICE_LDFLAGS}
${PBNJSON_C_LDFLAGS}
${LUNAPREFS_LDFLAGS}
${POWERD_LDFLAGS}
${PMLOGLIB_LDFLAGS}
${NYXLIB_LDFLAGS}
${LIBPBNJSON_LDFLAGS}
${PULSE_LDFLAGS}
${LTTNG_UST_LDFLAGS}
${URCU_BP_LDFLAGS}
${PULSE_SIMPLE_LDFLAGS}
${WEBOSI18N_LDFLAGS}
pthread
rt
dl
-lsnapshot-boot
)
And how i resolved the BAD RPATH Error during do_package_qa, I have already answerd it here:
bitbake do_package_qa issue contains bad RPATH

How to build only one target for dependency?

I want to build an application under Windows using CMake + Visual Studio with a lot of dependencies, such as zlib. All of them are static libraries.
I've tried ADD_SUBDIRECTORY and this works pretty well but instead of building only depending target (zlibstatic) it builds all of them.
How to remove unused targets (with their solutions) or choose only one?
Mainly I'm searching for feature to define only needed targets.
Part of my CMakeLists.txt:
ADD_SUBDIRECTORY("${CMAKE_CURRENT_SOURCE_DIR}/deps/zlib")
TARGET_INCLUDE_DIRECTORIES(MyProject PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/deps/zlib")
TARGET_LINK_LIBRARIES(MyProject zlibstatic)
I finally figured out how to do it.
MyProject
├───build <- here I call cmake
├───deps
│ └───zlib
│ └───CMakeLists.txt
├───inc
├───src
└───CMakeLists.txt
# Include project but remove all tartgets
ADD_SUBDIRECTORY(${CMAKE_CURRENT_SOURCE_DIR}/deps/zlib EXCLUDE_FROM_ALL)
# Use only specific one target
ADD_DEPENDENCIES(MyProject zlibstatic)
# Include dirs from zlib source directory and from output directory becuse it generates some headers
TARGET_INCLUDE_DIRECTORIES(MyProject PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/deps/zlib
${CMAKE_CURRENT_BINARY_DIR}/deps/zlib
)
# Just to create beautiful structure in project tree
SET_PROPERTY(TARGET zlibstatic PROPERTY FOLDER Deps)
# Link after all
TARGET_LINK_LIBRARIES(MyProject zlibstatic)
I suggest you use vcpkg or conan instead to resolve your dependent library issue this is much cleaner and works well except for header only libraries.
You can of cause do that manually but than you loose the nice cmake setup.

Link External Library when using CMakeLists used by CLion

I have build libmemcached.a and copied it to /usr/local/lib on my mac and I have tried all the following options to link libmemcached.a and yet get compile time errors that libmemcached/memcached.h is not found.
link_libraries (${libmemcached})
include_directories(SYSTEM ${libmemcached})
link_directories("/usr/local/lib")
find_package(libmemcached.a REQUIRED)
link_libraries`enter code here`(libmemcached.a)
find_library(RESULT libmemcached.a PATHS /usr/local/lib)
target_link_libraries(dnsa_pcl libmemcached.a)
It is a simple -L -l using MakeFile. Not sure what needs to be done to make this work using CMakeLists. Any help is much appreciated.
you should use so called imported library, like it's described in the official documentation
The solution that worked for me is a bit weird. I had to both set the CMAKE_PREFIX_PATH to the directory that has the lib and also have include_directories() with the include folder of the source. For some reason, I was under the impression that the libmemcached.a had the header files as well.

Cmake cannot find library using "link_directories"

I Ubuntu, I am learning about cmake and make, and just trying a simple example. I have two directories: src and build. In src, I have two files: main.cpp, and CMakeLists.txt, which has (only) the following text:
add_executable(test main.cpp)
link_directories(/usr/lib/x86_64-linux-gnu)
target_link_libraries(test protobuf)
In /usr/lib/x86_64-linux-gnu, there is a shared library called libprotobuf.so, which I want to link against. My main.cpp uses functions in this library, by including the releveant header file, #include <google/protobuf/message.h>.
Now, in my build directory, I run cmake ../src, and then make. However, I then get linker errors telling me that there are undefined references to some of the functions in the protobuf library. If I do a search through all the files and subdirectories in build, there is not mention of anything related to protobuf.
However, if I remove the link_directories line in my CMakeLists.txt file, and instead write the full path to the library when specifying the executable, i.e. target_link_libraries(test /usr/lib/x86_64-linux-gnu/libprotobuf.so), it compiles and links fine.
Why is link_directories not allowing cmake to find this library?
Do not use link_directories like this in CMake.
This is a common beginner's mistake, as many other build environments work like this, but in CMake it's just asking for trouble. Even the official documentation specifically advises against it:
Note that this command [link_directories] is rarely necessary. Library locations returned
by find_package() and find_library() are absolute paths. Pass these
absolute library file paths directly to the target_link_libraries()
command. CMake will ensure the linker finds them.
So instead, always pass absolute paths to target_link_libraries and use find_library to resolve the link directory:
find_library(PROTOBUF_LIBRARY protobuf HINTS /usr/lib/x86_64-linux-gnu)
target_link_libraries(test PUBLIC ${PROTOBUF_LIBRARY})
This has the huge benefit that you will probably get a diagnostic at CMake configure time if the expected library cannot be found, instead of a random linker error at compile time. Also, this allows the user to specify a library location via the GUI if the target machine has a non-standard directory layout.
So if it doesn't work right away, be sure to check the result of the find_library call and consult the official documentation to track down why it doesn't find your library as intended.
Make sure that your call to link_directories takes place before your call to the relevant add_executable.
I had mistakenly believed it only needed to be before the call to target_link_libraries, but that's not the case. After moving the call, the library is linked properly.
Make sure that the order will be link_directories, set PROJECT_LINK_LIBS, add_executable and then target_link_libraries.
Below is example to demonstarte it:
cmake_minimum_required(VERSION 2.8.9)
project (Logging)
include_directories(include)
file(GLOB LOGGINGSOURCES "libsrc/*.cpp")
file(GLOB SOURCES "src/*.cpp")
add_library(convertString SHARED ${LOGGINGSOURCES})
install(TARGETS convertString DESTINATION /root/Deepak/)
link_directories( /root/Deepak/ )
set(PROJECT_LINK_LIBS libconvertString.so)
add_executable(hello ${SOURCES})
target_link_libraries(hello ${PROJECT_LINK_LIBS} )
Perhaps it's very old topic but none of proposed solutions worked for me. So I had to make my own dirty hack. I do crosscompiling with buildroot and include toolchainfile.cmake.
#...
set(LIB_PATH ${PROJECT_SOURCE_DIR}/relative/path/to/your/lib)
#...
include_directories(/path/to/library/include)
set(LIB_MYLIB ${LIB_PATH}/libmylib.so)
#...
add_executable(${PROJECT_NAME} ${APP_SOURCES})
target_link_libraries(${PROJECT_NAME}
${LIB_MYLIB}
)
Hope this will help