Tried to build a project and install directory using cmake. Used following cmakefile.
CMake version used: 3.8.2
# Specifying minimum cmake version
cmake_minimum_required(VERSION 3.8.2)
# Project name
project(pro LANGUAGES C CXX VERSION 0.1.0)
# Set C++11 compiler
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Global Install Directories
include(GNUInstallDirs)
# Source files
SET(SRC_FILES
src/file.cpp
)
# Project requirements
find_package(OpenCV REQUIRED)
# Creating static library
add_library(${PROJECT_NAME} STATIC ${SRC_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<BUILD_INTERFACE:${OpenCV_INCLUDE_DIRS}>
$<INSTALL_INTERFACE:include>)
# Linking opencv dynamically
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS})
# Set correct Install directories
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Config
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
It is giving me following error while installing the directory
install TARGETS given unknown argument "DIRECTORY".
-- Configuring incomplete, errors occurred!
I am not able to understand the error, can anyone pleae help me out? Thank you !
Related
Context:
I have a cpp program built on MacOS 12.6 with the following CMakeLists.txt file.
cmake_minimum_required(VERSION 3.19.0)
project(cpp-test VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(cpp-test main.cpp)
add_library(test-helpers main.cpp ${PROJECT_SOURCE_DIR}/helpers.hpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
# this is super important in order for cmake to include the vcpkg search/lib paths!
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
# find library and its headers
find_path(IXWEBSOCKET_INCLUDE_DIR ixwebsocket/IXWebSocket.h)
find_library(IXWEBSOCKET_LIBRARY ixwebsocket)
find_package(OpenSSL REQUIRED)
find_package(CURL REQUIRED)
# include headers
include_directories(${IXWEBSOCKET_INCLUDE_DIR} ${CURL_INCLUDE_DIR})
# Cmake will automatically fail the generation if the lib was not found, i.e is set to NOTFOUND
target_link_libraries(
${PROJECT_NAME} PRIVATE
${IXWEBSOCKET_LIBRARY}
OpenSSL::SSL
OpenSSL::Crypto
${CURL_LIBRARIES}
"-framework Foundation"
"-framework Security"
"-lz"
)
This compiles just fine. However, when I try to pull it into my Ubuntu VM and try to build it /build> cmake .., I get the following errors
CMake Error in CMakeLists.txt:
Found relative path while evaluating include directories of "cpp-test":
"IXWEBSOCKET_INCLUDE_DIR-NOTFOUND"
CMake Error in CMakeLists.txt:
Found relative path while evaluating include directories of
"test-helpers":
"IXWEBSOCKET_INCLUDE_DIR-NOTFOUND"
-- Generating done
What I have tried...
I have installed vcpkg and created my symlink ln -s /path/to/vcpkg /usr/local/bin/vcpkg.
I have installed ixwebsocket via vcpkg install ixwebsocket, but it seems that the CMAKE_TOOLCHAIN_FILE is not being parsed correctly.
I'm a bit lost, any help would be appreciated
This is not a great answer to the issue, but I ended up resolving it by building ixwebsocket via CMake instead.
It seems that vcpkg was not compatible with the linux distro in my VM.
I created a ROS2 rviz plugin in C++ which I need to compile into a shared library (.so) using cmake. I already have a working CMakeLists.txt (see below) which creates a static library (.a); I need it to be shared, though.
However, when I add the SHARED keyword to the add_library macro (commented out in the code below), it throws this strange error:
/usr/bin/ld: cannot find -lXAW_LIBRARY-NOTFOUND
Now, I have looked at the many "/usr/bin/ld: cannot find [some library]" questions here on SO (like this), but my error seems to be more strange since it seems to contain an error ("-lXAW_LIBRARY-NOTFOUND") IN THE error (/usr/bin/ld: cannot find...). I mean, why is he even looking for a library called LIBRARY_NOTFOUND??
I'm on Ubuntu xenial 16.04 using cmake 3.10.
CMakeLists.txt:
project(traffic_sign_delegation_manager)
set(CMAKE_CXX_STANDARD 17)
if(NOT WIN32)
add_definitions(-fPIC)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic -Wno-deprecated-declarations)
endif()
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rviz_common REQUIRED)
find_package(std_msgs REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(rosidl_generator_cpp)
find_package(pluginlib REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5OpenGL REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Widgets)
set(msg_files
"msg/TrafficSignList.msg"
"msg/TrafficSign.msg"
"msg/TrafficSignSetList.msg"
"msg/TrafficSignSet.msg"
"msg/TrafficSignSetStatus.msg"
"msg/TrafficSignsManaged.msg"
"msg/AccLever2.msg"
"msg/VehicleOdometry.msg"
)
rosidl_generate_interfaces(${PROJECT_NAME}
${msg_files}
DEPENDENCIES std_msgs
)
link_directories(${ament_cmake_LIBRARY_DIRS})
add_definitions(-DQT_NO_KEYWORDS)
qt5_wrap_ui(QT_UI_FILES ui/traffic_sign_delegation_manager_panel.ui)
qt5_wrap_ui(QT_UI_FILES ui/traffic_sign_list_item.ui)
qt5_add_resources(QT_QRC_FILES ui/traffic_sign_delegation_manager.qrc)
set_property(SOURCE traffic_sign_delegation_manager_panel.h PROPERTY SKIP_AUTOMOC ON)
set_property(SOURCE draw_area.h PROPERTY SKIP_AUTOMOC ON)
set_property(SOURCE adv_interaction_groupbox.h PROPERTY SKIP_AUTOMOC ON)
set_property(SOURCE traffic_sign_delegation_manager_display.h PROPERTY SKIP_AUTOMOC ON)
add_library(delegator_lib # SHARED # <=== WHY IS THIS NOT WORKING?
vec2d.cpp
vec2d.h
traffic_sign_delegation_manager_panel.cpp
traffic_sign_delegation_manager_panel.h
draw_area.cpp
draw_area.h
traffic_sign_delegation_manager_display.cpp
traffic_sign_delegation_manager_display.h
adv_interaction_groupbox.cpp
adv_interaction_groupbox.h
ui/traffic_sign_list_item.ui
ui/traffic_sign_delegation_manager_panel.ui
${QT_UI_FILES}
${MOC_FILES}
)
rosidl_target_interfaces(delegator_lib ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_include_directories(delegator_lib PUBLIC
${rvizCommon_DIR}
${rosidl_generator_cpp_INCLUDE_DIRS}
${ament_cmake_INCLUDE_DIRS}
${rviz2_INCLUDE_DIRS}
${rviz_common_INCLUDE_DIRS}
${FREETYPE_INCLUDE_DIRS}
${Qt5_INCLUDE_DIRS}
)
target_link_libraries(delegator_lib
rviz_common::rviz_common
)
target_compile_definitions(delegator_lib PRIVATE "RVIZ_DEFAULT_PLUGINS_BUILDING_LIBRARY")
target_compile_definitions(delegator_lib PUBLIC "PLUGINLIB__DISABLE_BOOST_FUNCTIONS")
pluginlib_export_plugin_description_file(rviz_common plugin_description.xml)
ament_target_dependencies(delegator_lib
geometry_msgs
laser_geometry
nav_msgs
map_msgs
rclcpp
resource_retriever
urdf
visualization_msgs
)
ament_export_include_directories(${INCLUDE_DIRS} ${ament_cmake_INCLUDE_DIRS} include)
ament_export_interfaces(delegator_lib HAS_LIBRARY_TARGET)
ament_export_dependencies(
Qt5
rviz_common
geometry_msgs
laser_geometry
map_msgs
nav_msgs
rclcpp
urdf
visualization_msgs
rosidl_generator_cpp
)
install(FILES plugin_description.xml
DESTINATION share/${PROJECT_NAME})
install(DIRECTORY images
DESTINATION share/${PROJECT_NAME})
install(DIRECTORY ui
DESTINATION share/${PROJECT_NAME}
PATTERN "*.ui"
EXCLUDE)
install(
TARGETS delegator_lib
EXPORT delegator_lib
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
install(
DIRECTORY "${CMAKE_SOURCE_DIR}/icons"
DESTINATION "share/${PROJECT_NAME}"
)
ament_package()
Please note: This question is not about ROS; I am not a cmake wizard, so probably I'm just doing something terribly wrong in cmake... I already asked a more broad version of this question on answers.ros, but it appears to be too cmake-specific or something. Anyway, I did not get an answer there. (The above code is not an MWE, sorry; I could create one if neccessary, but it would require ROS2 to compile...)
Normally that error pops up if you are giving some library names as arguments to the target_link_libraries function.
Either,
The names are not correct or,
The path is not or,
The libraries are not installed.
I'd look at target_link_libraries(delegator_lib rviz_common::rviz_common) and link_directories(${ament_cmake_LIBRARY_DIRS}) as suspects.
In CMake you can use MESSAGE command to do debugging of sorts where you can display the values of the CMake variables to check if they make sense with what is on the system.
Also you can try installing the XAW library like this sudo apt-get install libxaw7-dev. Could be that one of the libraries that you link to has a dependency on the XAW library.
My project uses the QuaZip library, and I need to build the project through CMake. How to add this library to CMakeLists?
From the library I need JlCompress
My CMakeLists:
cmake_minimum_required(VERSION 3.6)
#set_property(GLOBAL PROPERTY USE_FOLDERS ON)
#set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "cmake")
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
project(Archiver LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui)
find_package(zlib)
find_package(QuaZip5)
include_directories(${QUAZIP_INCLUDE_DIRS})
set(project_ui
mainwindow.ui)
set(project_headers
archive.h
mainwindow.h)
set(project_sources
main.cpp
archive.cpp
mainwindow.cpp)
qt5_wrap_ui(project_headers_wrapped ${project_ui})
qt5_wrap_cpp(project_sources_moc ${project_headers})
add_executable(${PROJECT_NAME} ${project_headers} ${project_sources}
${project_sources_moc} ${project_headers_wrapped})
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::Core
Qt5::Gui
Qt5::Widgets
${QUAZIP_LIBRARIES}
)
Build error:
CMake Warning at CMakeLists.txt:13 (find_package): By not providing
"Findquazip.cmake" in CMAKE_MODULE_PATH this project has asked CMake
to find a package configuration file provided by "quazip", but CMake
did not find one.
Could not find a package configuration file provided by "quazip"
with any of the following names:
quazipConfig.cmake
quazip-config.cmake
Add the installation prefix of "quazip" to CMAKE_PREFIX_PATH or set
"quazip_DIR" to a directory containing one of the above files. If
"quazip" provides a separate development package or SDK, be sure it
has been installed.
CMake Error at CMakeLists.txt:37 (target_link_libraries): The
keyword signature for target_link_libraries has already been used with
the target "Archiver". All uses of target_link_libraries with a
target must be either all-keyword or all-plain.
The uses of the keyword signature are here:
CMakeLists.txt:31 (target_link_libraries)
The find script for quazip is named FindQuaZip5.cmake (it is renamed during installation). So for find quazip you need to use
find_package(QuaZip5)
Meaning of the find script is described in its head:
# QUAZIP_FOUND - QuaZip library was found
# QUAZIP_INCLUDE_DIR - Path to QuaZip include dir
# QUAZIP_INCLUDE_DIRS - Path to QuaZip and zlib include dir (combined from QUAZIP_INCLUDE_DIR + ZLIB_INCLUDE_DIR)
# QUAZIP_LIBRARIES - List of QuaZip libraries
# QUAZIP_ZLIB_INCLUDE_DIR - The include dir of zlib headers
That is, for use quazip with zlib in your code, add these lines:
include_directories(${QUAZIP_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${QUAZIP_LIBRARIES})
I have a custom library which exports include/, lib/ and lib/cmake/ which contains MyProjectConfig.cmake with the following contents:
set(MyProject_INCLUDE_DIRS "/home/.../cmake-build-debug/thirdparty/myproj/include")
set(MyProject_LIBRARY_DIRS "/home/.../cmake-build-debug/thirdparty/myproj/lib")
set(MyProject_LIBRARIES "MyProject")
message(STATUS "MyProject found. Headers: ${MyProject_INCLUDE_DIRS}")
I use it in another project like this:
include(ExternalProject)
ExternalProject_Add(MyProjectExternal
PREFIX "${CMAKE_BINARY_DIR}/external/myproject"
GIT_REPOSITORY "git#bitbucket.org:myproject.git"
GIT_TAG "master"
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/thirdparty/myproject
)
add_dependencies(${PROJECT_NAME} MyProjectExternal)
# prevent error on first project use when dir doesn't exist
if(EXISTS ${CMAKE_BINARY_DIR}/thirdparty/myproject/lib)
find_package(MyProject REQUIRED HINTS ${CMAKE_BINARY_DIR}/thirdparty/myproject/lib/cmake)
find_library(MyProject_LIB MyProject HINTS ${MyProject_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${MyProject_LIB})
target_include_directories(${PROJECT_NAME} PRIVATE ${MyProject_INCLUDE_DIRS})
endif()
I expected that the variables set in MyProjectConfig.cmake would be picked up automatically by cmake to find the library by name and this would work:
find_package(MyProject REQUIRED HINTS ${CMAKE_BINARY_DIR}/thirdparty/myproject/lib/cmake)
target_link_libraries(${PROJECT_NAME} PUBLIC MyProject)
But it doesn't:
[ 87%] Built target MyProjectExternal
[ 90%] Linking CXX executable RootProject
/usr/bin/ld: cannot find -lMyProject
collect2: error: ld returned 1 exit status
Part II (as requested here) The full code and the steps to reproduce the problem
Library Code (mylib branch)
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(MyLib VERSION 1.0.0 LANGUAGES CXX)
add_library(${PROJECT_NAME} SHARED
src/mylib/hello.cpp
)
target_include_directories(${PROJECT_NAME}
PUBLIC $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
include(cmake/install.cmake)
cmake/install.cmake
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
include(GenerateExportHeader)
generate_export_header(${PROJECT_NAME}
EXPORT_MACRO_NAME EXPORT
NO_EXPORT_MACRO_NAME NO_EXPORT
PREFIX_NAME MYLIB_
EXPORT_FILE_NAME ${CMAKE_BINARY_DIR}/include-exports/mylib/export.h)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include-exports>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
install(DIRECTORY ${CMAKE_BINARY_DIR}/include-exports/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
include(CMakePackageConfigHelpers)
set_property(TARGET ${PROJECT_NAME} PROPERTY VERSION ${PROJECT_VERSION})
set_property(TARGET ${PROJECT_NAME} PROPERTY SOVERSION ${PROJECT_VERSION_MAJOR})
set_property(TARGET ${PROJECT_NAME} PROPERTY INTERFACE_${PROJECT_NAME}_MAJOR_VERSION ${PROJECT_VERSION_MAJOR})
set_property(TARGET ${PROJECT_NAME} APPEND PROPERTY COMPATIBLE_INTERFACE_STRING ${PROJECT_VERSION_MAJOR})
write_basic_package_version_file(
"${CMAKE_BINARY_DIR}/CMakePackage/${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
export(EXPORT ${PROJECT_NAME}Targets
FILE "${CMAKE_BINARY_DIR}/CMakePackage/${PROJECT_NAME}.cmake"
)
SET(CONFIG_SOURCE_DIR ${CMAKE_SOURCE_DIR})
SET(CONFIG_DIR ${CMAKE_BINARY_DIR})
SET(${PROJECT_NAME}_INCLUDE_DIR "\${${PROJECT_NAME}_SOURCE_DIR}/include")
configure_package_config_file(${CMAKE_SOURCE_DIR}/cmake/Config.cmake.in
"${CMAKE_BINARY_DIR}/CMakePackage/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}
PATH_VARS ${PROJECT_NAME}_INCLUDE_DIR)
install(EXPORT ${PROJECT_NAME}Targets
FILE ${PROJECT_NAME}.cmake
DESTINATION lib/cmake/${PROJECT_NAME}
)
install(
FILES
"${CMAKE_BINARY_DIR}/CMakePackage/${PROJECT_NAME}Config.cmake"
"${CMAKE_BINARY_DIR}/CMakePackage/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION lib/cmake/${PROJECT_NAME}
COMPONENT Devel
)
cmake/Config.cmake.in
#PACKAGE_INIT#
set_and_check(#PROJECT_NAME#_INCLUDE_DIRS "#CMAKE_INSTALL_PREFIX#/#CMAKE_INSTALL_INCLUDEDIR#")
set_and_check(#PROJECT_NAME#_LIBRARY_DIRS "#CMAKE_INSTALL_PREFIX#/#CMAKE_INSTALL_LIBDIR#")
set(#PROJECT_NAME#_LIBRARIES "#PROJECT_NAME#")
check_required_components(#PROJECT_NAME#)
message(STATUS "#PROJECT_NAME# found. Headers: ${#PROJECT_NAME#_INCLUDE_DIRS}")
include/mylib/hello.h
#ifndef MYLIB_HELLO_H
#define MYLIB_HELLO_H
#include <mylib/export.h>
namespace mylib {
extern MYLIB_EXPORT void hello();
}
#endif
src/mylib/hello.cpp
#include <mylib/hello.h>
#include <iostream>
namespace mylib {
void hello() {
std::cout << "Hello, MyLib!" << std::endl;
}
}
Application Code (master branch)
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(MyLibConsumer VERSION 1.0.0 LANGUAGES CXX)
add_executable(${PROJECT_NAME}
main.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
include(ExternalProject)
set(LIB_INSTALL_DIR ${CMAKE_BINARY_DIR}/thirdparty/mylib)
ExternalProject_Add(MyLibExternal
PREFIX "${CMAKE_BINARY_DIR}/external/mylib"
GIT_REPOSITORY "https://github.com/arteniioleg/stackoverflow-question-46772541.git"
GIT_TAG "mylib"
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR}
)
add_dependencies(${PROJECT_NAME} MyLibExternal)
if(EXISTS ${LIB_INSTALL_DIR}/lib) # prevent error on first cmake load
find_package(MyLib REQUIRED HINTS ${LIB_INSTALL_DIR}/lib/cmake)
# fixme: make this work
target_link_libraries(${PROJECT_NAME} PUBLIC MyLib)
# to test: comment the above line and uncomment the below lines
#find_library(MyLib_LIB MyLib HINTS ${MyLib_LIBRARY_DIRS})
#target_link_libraries(${PROJECT_NAME} PRIVATE ${MyLib_LIB})
#target_include_directories(${PROJECT_NAME} PRIVATE ${MyLib_INCLUDE_DIRS})
endif()
main.cpp
#include <mylib/hello.h>
int main()
{
mylib::hello();
return 0;
}
The Error
main.cpp:1:25: fatal error: mylib/hello.h: No such file or directory
#include <mylib/hello.h>
^
Using the MyLib_* variables created by find_package() fixes the problem but it's too verbose.
As suggested here:
However, recommended way is to use CMake functionality for create configuration file. Such way, fully-fledged target will be created, and can be used for linking.
How to do that?
I want to use my library in 2 steps, like Qt:
find_package(Qt5Widgets)
target_link_libraries(myApp Qt5::Widgets)
Just include MyProject.cmake in MyProjectConfig.cmake (it is not included by default)
include(${CMAKE_CURRENT_LIST_DIR}/MyProject.cmake)
The final version of key files:
LIBRARY/cmake/Config.cmake.in
Removed redundant variables: *_INCLUDE_DIRS, *_LIBRARY_DIRS, *_LIBRARIES
#PACKAGE_INIT#
include(${CMAKE_CURRENT_LIST_DIR}/#PROJECT_NAME#.cmake)
check_required_components(#PROJECT_NAME#)
message(STATUS "#PROJECT_NAME# found.")
EXECUTABLE/CMakeLists.txt
Moved external projects before executable and return() if external projects are not built.
cmake_minimum_required(VERSION 3.8)
project(MyLibConsumer VERSION 1.0.0 LANGUAGES CXX)
set(LIB_INSTALL_DIR ${CMAKE_BINARY_DIR}/thirdparty/mylib)
include(ExternalProject)
ExternalProject_Add(MyLibExternal
PREFIX "${CMAKE_BINARY_DIR}/external/mylib"
GIT_REPOSITORY "https://github.com/arteniioleg/stackoverflow-question-46772541.git"
GIT_TAG "mylib"
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${LIB_INSTALL_DIR}
)
if(NOT EXISTS ${LIB_INSTALL_DIR}/lib)
# Happens on first CMake run.
# Can't continue because the below `find_package(REQUIRED)` will fail.
# Build all external project targets then rerun CMake and build the project target.
message(AUTHOR_WARNING "Build all external projects then reload cmake.")
return()
endif()
add_executable(${PROJECT_NAME} main.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
add_dependencies(${PROJECT_NAME} MyLibExternal)
find_package(MyLib REQUIRED HINTS ${LIB_INSTALL_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE MyLib)
I expected that the variables set in MyProjectConfig.cmake would be picked up automatically by cmake to find the library by name.
The most "magic" in find_package command is how it searches *Config.cmake script. After the script is found, it is simply executed in the context of the caller.
In you case, CMake sets variables MyProject_INCLUDE_DIRS, MyProject_LIBRARY_DIRS and MyProject_LIBRARIES. Nothing more. It doesn't create MyProject target and so.
If you want find_package() preparing linking with MyProject library, you need to write your MyProjectConfig.cmake script accordingly (e.g., call link_directories() from it).
However, recommended way is to use CMake functionality for create configuration file. Such way, fully-fledged target will be created, and can be used for linking. See cmake-packages documentation for more info.
I am having a problem linking two libraries using CMake on linux (ubuntu). I have the following CMake setup.
cmake_minimum_required(VERSION 3.3)
project(lib1)
set(SOURCE_FILES
source_1.cpp)
# set library output directory
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ../build/)
# Base directory relative to which all includes are
set(BASE_DIR ../)
#include base directory
include_directories ("${BASE_DIR}")
# create a shared library
add_library(lib1 SHARED ${SOURCE_FILES})
# make install target
install(TARGETS lib1 DESTINATION /usr/local/lib)
For Library 2 we have
cmake_minimum_required(VERSION 3.3)
project(lib2)
set(SOURCE_FILES
source_2.cpp)
set(BASE_DIR ../)
#include base directory
include_directories ("${BASE_DIR}")
# create a shared library
add_library(lib2 SHARED ${SOURCE_FILES})
# include lib1 library
target_link_libraries(lib2 PUBLIC lib1)
install(TARGETS lib2 DESTINATION /usr/local/lib)
Running these with
cmake_minimum_required(VERSION 3.3)
project(all)
add_subdirectory(lib1)
add_subdirectory(lib2)
creates liblib1.so and liblib2.so. liblib2.so depends on liblib1.so (ldd liblib2.so gives the link to liblib1.so in the CMAKE_LIBRARY_OUTPUT_DIRECTORY directory.)
After running
make install
I get
-- Install configuration: "Release"
-- Up-to-date: /usr/local/lib/liblib1.so
-- Installing: /usr/local/lib/liblib2.so
-- Set runtime path of "/usr/local/lib/liblib2.so" to ""
and in /usr/local/lib liblib2.so is no longer linked to liblib1.so.
I tried many reworkings of my cmake file, (e.g. using :
set(CMAKE_INSTALL_RPATH "/usr/local/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
but nothing seemed to help. Anyone can explain me how to get the libraries to link after make install?
I did run ldconfig manually, but no luck. /usr/local/lib is also part of ld.conf and I am running ubuntu 16.04.