I am trying to make a cmake file for my project just to learn how to make a cmake file.
My directory Structure. I am getting an error :
square_and_add
|- src
| |- <Some source files>
|- libsrc
| |- include
| |- <library header files>
| |- src
| |- <library source files>
|- build
|- libs
|- <installed libraries>
cmake_minimum_required(VERSION 2.6)
project(SQUARE_AND_ADD)
set(CMAKE_CXX_FLAGS "-g -Wall")
include_directories(./libsrc/include)
include_directories(./libsrc/src)
include_directories(./build)
add_executable(no_lib_exec ./src/main.cc)
target_link_libraries(no_lib_exec ./libsrc/src/square.cc)
set_target_properties(no_lib_exec PROPERTIES PREFIX "" OUTPUT_NAME sqnadd_no_lib)
add_library(build_static_lib STATIC ./libsrc/src/square.cc)
set_target_properties(build_static_lib PROPERTIES PREFIX "" OUTPUT_NAME static_library)
add_executable(static_lib_exec ./src/main.cc)
target_link_libraries(static_lib_exec ./build/static_library.a)
set_target_properties(static_lib_exec PROPERTIES PREFIX "" OUTPUT_NAME sqnadd_stat_lib)
add_library(build_dynamic_lib SHARED ./libsrc/src/square.cc)
set_target_properties(build_dynamic_lib PROPERTIES PREFIX "" OUTPUT_NAME dynamic_library)
add_executable(dynamic_lib_exec ./src/main.cc)
target_link_libraries(dynamic_lib_exec ./build/dynamic_library.so)
set_target_properties(dynamic_lib_exec PROPERTIES PREFIX "" OUTPUT_NAME sqnadd_dyn_lib)
install(TARGETS dynamic_lib_exec static_lib_exec
RUNTIME DESTINATION ./libs CONFIGURATIONS debug)
I am getting this error
/usr/bin/ld: cannot find -l./build/dynamic_library
collect2: error: ld returned 1 exit status
CMakeFiles/dynamic_lib_exec.dir/build.make:85: recipe for target 'sqnadd_dyn_lib' failed
Here is my approach on doing such things:
cmake_minimum_required(VERSION 2.6)
project(SQUARE_AND_ADD)
set(CMAKE_CXX_FLAGS "-g -Wall")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libsrc/include)
set(EXEC_NAME_BASE "sqnadd")
set(EXEC_NOLIB_NAME "${EXEC_NAME_BASE}_no_lib")
set(EXEC_STATICLIB_NAME "${EXEC_NAME_BASE}_stat_lib")
set(EXEC_SHAREDLIB_NAME "${EXEC_NAME_BASE}_dyn_lib")
set(STATICLIB_NAME "square_static")
set(SHAREDLIB_NAME "square_shared")
# if no lib, then just compile the square.cc into the executable
add_executable(${EXEC_NOLIB_NAME} src/main.cc libsrc/src/square.cc)
add_library(${STATICLIB_NAME} STATIC libsrc/src/square.cc)
add_executable(${EXEC_STATICLIB_NAME} src/main.cc)
target_link_libraries(${EXEC_STATICLIB_NAME} ${STATICLIB_NAME} )
add_library(${SHAREDLIB_NAME} SHARED libsrc/src/square.cc)
add_executable(${EXEC_SHAREDLIB_NAME} src/main.cc)
target_link_libraries(${EXEC_SHAREDLIB_NAME} ${SHAREDLIB_NAME})
# installing libraries:
install(TARGETS ${SHAREDLIB_NAME} ${STATICLIB_NAME}
RUNTIME DESTINATION lib CONFIGURATIONS debug)
# installing executables
install(TARGETS ${EXEC_NOLIB_NAME}
${EXEC_STATICLIB_NAME}
${EXEC_SHAREDLIB_NAME}
RUNTIME DESTINATION bin
CONFIGURATIONS debug)
Related
I have a vendor library which needs to be included with my project's installer, I'm installing it with INSTALL(FILES ...). The library is stored along with the source, the problem is that the linked path (shown by readelf) is relative and that prefix doesn't get removed during installation, so instead of the dynamic linker searching for hhlib.so its looking for ../hhlib-linux-64bit/hhlib.so.
How can I get a working installed binary? Can I get the install step or CPack to remove this relative path to the library?
I've reduced the problem down to a simple example:
.
├── build
├── CMakeLists.txt
├── hhlib-linux-64bit
│ ├── hhlib.h
│ └── hhlib.so
└── use_hydroharp.c
cmake_minimum_required (VERSION 3.19)
# These sets have no effect on the issue
# use, i.e. don't skip the full RPATH for the build tree
#SET(CMAKE_SKIP_BUILD_RPATH FALSE)
# when building, don't use the install RPATH already
# (but later on when installing)
#SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
# the RPATH to be used when installing
#SET(CMAKE_INSTALL_RPATH "")
# don't add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
#SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
enable_language(C)
# Find the header files
find_path(HydroHarp_INCLUDEDIR
NAMES hhlib.h
PATHS "${CMAKE_CURRENT_SOURCE_DIR}/hhlib-linux-64bit"
)
# Find the shared library
find_library(HydroHarp_LIBRARY
NAMES hhlib.so
HINTS "${CMAKE_CURRENT_SOURCE_DIR}/hhlib-linux-64bit"
)
if(HydroHarp_INCLUDEDIR AND HydroHarp_LIBRARY)
message("Found HyroHarp library")
# Get the containing folder for the library
get_filename_component(HydroHarp_LIBDIR ${HydroHarp_LIBRARY} DIRECTORY)
# Import the library
add_library(HydroHarp SHARED IMPORTED)
set_target_properties(HydroHarp PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${HydroHarp_INCLUDEDIR}
IMPORTED_LOCATION ${HydroHarp_LIBRARY}
)
# copy the library when installing
install(FILES ${HydroHarp_LIBRARY} TYPE LIB)
else()
message(WARNING "Failed to find HydroHarp")
endif()
project(extlink)
add_executable(${PROJECT_NAME} use_hydroharp.c)
target_link_libraries(${PROJECT_NAME} PRIVATE HydroHarp)
# install the library
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
)
set(CPACK_GENERATOR DEB CACHE INTERNAL "")
SET(CPACK_PACKAGE_CONTACT "asdaksd#kjhk.com")
SET(CPACK_PACKAGE_VENDOR "asdfasd")
include(CPack)
Built with:
build$ cmake .. && make package
The program references the shared library with a relative path (which is fine for the build tree but not needed due to the RUNPATH):
build$ readelf -d extlink
Dynamic section at offset 0x2da8 contains 29 entries:
Tag Type Name/Value
0x0000000000000001 (NEEDED) Shared library: [../hhlib-linux-64bit/hhlib.so]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000001d (RUNPATH) Library runpath: [/code/hhlib-linux-64bit:]
0x000000000000000c (INIT) 0x1000
...
but after make install it's still there and the RUNPATH has been removed:
build$ readelf -d /usr/bin/extlink
Dynamic section at offset 0x2da8 contains 28 entries:
Tag Type Name/Value
0x0000000000000001 (NEEDED) Shared library: [../hhlib-linux-64bit/hhlib.so]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000000c (INIT) 0x1000
I'm building on Ubuntu 20.04 with CMake 3.16.3 but also tested with the latest CMake version 3.19.4
At first I thought this issue provided a solution, but no combination of those RPATH variables fixes the problem.
After a nice, long scream into my favourite screaming pillow I had another think:
Adding the property IMPORTED_NO_SONAME removes the offending path but the linker fails to find it - even when the path is explicitly included with LINK_DIRECTORIES, why? Because the vendor library doesn't follow the naming convention!
Renaming the library to libhh.so and adding set_target_properties(HydroHarp PROPERTIES IMPORTED_NO_SONAME TRUE) fixes the issue. Don't ask me why.
The full working CMakeLists.txt:
cmake_minimum_required (VERSION 3.19)
enable_language(C)
# Find the header files
find_path(HydroHarp_INCLUDEDIR
NAMES hhlib.h
PATHS "${CMAKE_CURRENT_SOURCE_DIR}/hhlib-linux-64bit"
)
# Find the shared library
find_library(HydroHarp_LIBRARY
NAMES hh # <-- Note removed .so so standard extensions are searched
HINTS "${CMAKE_CURRENT_SOURCE_DIR}/hhlib-linux-64bit"
)
if(HydroHarp_INCLUDEDIR AND HydroHarp_LIBRARY)
message("Found HyroHarp library")
# Import the library
add_library(HydroHarp SHARED IMPORTED)
set_target_properties(HydroHarp PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${HydroHarp_INCLUDEDIR}
IMPORTED_NO_SONAME TRUE # <-- This lib wasn't built with an SONAME
IMPORTED_LOCATION ${HydroHarp_LIBRARY}
)
# copy the library when installing
install(FILES ${HydroHarp_LIBRARY} TYPE LIB)
else()
message(WARNING "Failed to find HydroHarp")
endif()
project(extlink)
add_executable(${PROJECT_NAME} use_hydroharp.c)
target_link_libraries(${PROJECT_NAME} PRIVATE HydroHarp)
# install the library
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
)
set(CPACK_GENERATOR DEB CACHE INTERNAL "")
SET(CPACK_PACKAGE_CONTACT "asdaksd#adssd.com")
SET(CPACK_PACKAGE_VENDOR "asdfasd")
include(CPack)
I have tried following this solution this individual has from this question. I am trying to do this with boost with the following cmake file in a separate directory from my Source:
#---------------------------------------------------------------------------
# Get and build boost
SET_PROPERTY(DIRECTORY PROPERTY "EP_BASE" ${ep_base})
SET(boost_GIT_TAG "origin/master")
set( Boost_Bootstrap_Command )
if( UNIX )
set( Boost_Bootstrap_Command ./bootstrap.sh )
set( Boost_b2_Command ./b2 )
else()
if( WIN32 )
set( Boost_Bootstrap_Command bootstrap.bat )
set( Boost_b2_Command b2.exe )
endif()
endif()
ExternalProject_Add(Boost_external_Download
GIT_REPOSITORY "https://github.com/boostorg/boost.git"
GIT_TAG ${boost_GIT_TAG}
BUILD_IN_SOURCE 1
UPDATE_COMMAND ""
PATCH_COMMAND ""
CONFIGURE_COMMAND ${Boost_Bootstrap_Command}
BUILD_COMMAND ${Boost_b2_Command} install
--without-python
--without-mpi
--disable-icu
--prefix=${CMAKE_BINARY_DIR}/Boost
--threading=single,multi
--link=shared
--variant=release
-j8
INSTALL_COMMAND ""
INSTALL_DIR ""
)
if( NOT WIN32 )
set(Boost_LIBRARY_DIR ${CMAKE_BINARY_DIR}/Boost/lib/ )
set(Boost_INCLUDE_DIR ${CMAKE_BINARY_DIR}/Boost/include/boost/ )
else()
set(Boost_LIBRARY_DIR ${CMAKE_BINARY_DIR}/Boost/lib/ )
set(Boost_INCLUDE_DIR ${CMAKE_BINARY_DIR}/Boost/include/boost/ )
endif()
ExternalProject_Get_Property(Boost_external_Download BINARY_DIR)
SET(Boost_DIR ${BINARY_DIR} CACHE PATH "")
add_library(Boost_external SHARED IMPORTED)
The only thing that is different between one of the solutions and mine is that I don't have the set_target_properties right after I add the library. Since boost has a lot of libraries in there and I am not exactly sure what needs to be added.
In my Source Folder, my CMakeLists.txt does the following:
include_directories(${Boost_INCLUDE_DIR})
add_executable(Main main.cpp)
target_link_libraries(Main Boost_external)
add_dependencies(Main Boost_external_Download)
However, in main.cpp when I use #include <boost/process.hpp> just to see if it links, I get the error that #include <boost/process.hpp> no such file or directory. When I try to make the project. I am not really sure where I am going wrong with this so any helpful advice would be appreciated.
Edit: structure
Root Folder
|
- CmakeLists.txt
- Externals Directory
|
| - Externals.cmake
| - boostExternal.cmake
| - other external cmake files
- Source Directory
|
| - CmakeLists.txt
| - main.cpp
The Root Cmake calls the Externals.cmake file. Which then includes all the other Cmake files within that directory. Once those are finished, the root Cmake file add the directory Source, and that Cmake file has the call to include_directories(${Boost_INCLUDE_DIR}).
In my project I have the following configuration:
low level shared library
shared library that is a wrapper of low level shared library
executable
so the files tree is :
CMakeLists.txt
SharedLibraryBase
CMakeLists.txt
inc
myLibBase.h -> defines MyLibBaseFunction()
src
myLibBase.cpp -> implements MyLibBaseFunction()
MySharedLibrary
CMakeLists.txt
inc
myLib.h -> defines MyLibFunction()
src
myLib.cpp -> implements MyLibFunction()
Executables
CMakeLists.txt
inc
main.h
src
main.cpp -> calls MyLibFunction()
When I perform make , I get a following link error:
ld.exe: cannot find -lMySharedLibrary
the main CMakeLists.txt is:
cmake_minimum_required(VERSION 3.5)
project(TestSharedLibraryProject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "my_outputs")
message("Binary tree path : ${PROJECT_BINARY_DIR}")
add_subdirectory(SharedLibraryBase)
add_subdirectory(MySharedLibrary)
add_subdirectory(Executables)
the low level shared library CMakeLists.txt is:
###########################
# SharedLibraiesBase #
###########################
project(SharedLibraryBase)
# include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${Boost_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/inc)
# find source files
set(${PROJECT_NAME}_headers inc/myLibBase.h)
set(${PROJECT_NAME}_sources src/myLib.cpp)
# create shared library
add_library(${PROJECT_NAME} SHARED ${${PROJECT_NAME}_headers} ${${PROJECT_NAME}_sources})
# add files to export to 'include'
set_property(TARGET ${PROJECT_NAME} PROPERTY PUBLIC_HEADER inc/myLibBase.h)
install (TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static
PUBLIC_HEADER DESTINATION include)
the wrapper shared library CMakeLists.txt is:
#######################
# SharedLibraies #
#######################
project(MySharedLibrary)
# include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${Boost_INCLUDE_DIRS} inc ../SharedLibraryBase/inc)
# find source files
set(${PROJECT_NAME}_headers inc/myLib.h)
set(${PROJECT_NAME}_sources src/myLib.cpp)
# create shared library
add_library(${PROJECT_NAME} SHARED ${${PROJECT_NAME}_headers} ${${PROJECT_NAME}_sources})
#dependency shared library (from this project)
target_link_libraries(${PROJECT_NAME} SharedLibraryBase)
# add files to export to 'include'
set_property(TARGET ${PROJECT_NAME} PROPERTY PUBLIC_HEADER inc/myLib.h)
install (TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static
PUBLIC_HEADER DESTINATION include)
the executable CMakeLists.txt is:
####################
# Executables #
####################
project(Executables)
# include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${Boost_INCLUDE_DIRS}/inc ${CMAKE_CURRENT_SOURCE_DIR}/../MySharedLibrary/inc)
set(${PROJECT_NAME}_headers inc/main.h)
set(${PROJECT_NAME}_sources src/main.cpp)
#create executable
add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_headers} ${${PROJECT_NAME}_sources})
#dependency shared library (from this project)
target_link_libraries(${PROJECT_NAME} MyShraedLibrary)
install (TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static)
You have ordinary type error MyShraedLibrary instead of MySharedLibrary
The problem is as follows:
Project structure:
ProjectDir
- CMakeLists.txt
- src
-- CMakeLists.txt
-- FirstApp
--- CMakeLists.txt
--- ... sources ...
-- SecondApp
--- CMakeLists.txt
--- ... sources ...
- tools
-- CMakeLists.txt
-- SomeTool
--- CMakeLists.txt
--- ... sources ...
-- SomeOtherTool
--- CMakeLists.txt
--- ... sources ...
What I want to achieve is:
package (let it be rpm or deb) with SomeTool binaries;
package with FirstApp, SecondApp AND SomeOtherTool binaries;
As far as I know:
CPack is putting into packages content the stuff that is defined in install command;
CPack cannot be called ( include(CPack) ) twice in the same tree (for example it cannot be called in CMakeLists.txt at src level AND SecondApp level);
What do I have
In ProjectDir/tools/SomeTool/CMakeLists.txt I've (it's pseado code):
PROJECT( SomeTool )
SET(TARGET SomeTool)
SET(CPACK_GENERATOR "RPM")
SET(CPACK_PACKAGE_NAME "SomeTool")
# ...
# all the other CPACK_* stuff
# ...
include(CPack)
add_custom_target(${TARGET}-package
COMMAND cpack --config "${CMAKE_CURRENT_BINARY_DIR}/CPack${PROJECT_NAME}Config.cmake")
SET(SOURCES ...)
SET(HEADERS ...)
add_executable(${TARGET} ${SOURCES} ${HEADERS})
install(TARGETS ${TARGET}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/bin)
This way I can call make SomeTool-package and it'll create package (SomeTool.rpm) with content /usr/bin/SomeTool. Great!
I can have very similar CMakeLists.txt in ProjectDir/src/ dir:
# src/CMakeLists.txt
PROJECT( MainProject )
SET(CPACK_GENERATOR "RPM")
SET(CPACK_PACKAGE_NAME "MainProject")
# ...
# all the other CPACK_* stuff
# ...
include(CPack)
add_custom_target(MainProject-package
COMMAND cpack --config "${CMAKE_CURRENT_BINARY_DIR}/CPack${PROJECT_NAME}Config.cmake")
add_subdirectory(FirstApp)
add_subdirectory(SecondApp)
# src/FirstApp/CMakeLists.txt
SET(TARGET FirstApp)
SET(SOURCES ...)
SET(HEADERS ...)
add_executable(${TARGET} ${SOURCES} ${HEADERS})
install(TARGETS ${TARGET}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/bin)
# src/SecondApp/CMakeLists.txt
SET(TARGET SecondApp)
SET(SOURCES ...)
SET(HEADERS ...)
add_executable(${TARGET} ${SOURCES} ${HEADERS})
install(TARGETS ${TARGET}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/bin)
This way I can call make MainProject-package and it'll create package (MainProject.rpm) with content /usr/bin/FirstApp and /usr/bin/SecondApp. Great!
SomeOtherTool also have a CMakeLists.txt which looks the same as FirstApp and SecondApp CMakeLists.txt and I can call make SomeOtherTool and it'll produce SomeOtherTool binaries.
The Question
As CPack is puting into packages stuff connected to install command, how can I connect SomeOtherTool with my MainProject-package ?
My solution would (is) be to put another install clause at the end of src/CMakeLists.txt that'd look like:
add_dependency(MainProject SomeOtherTool)
install(FILES ${CMAKE_BINARY_DIR}/SomeOtherTool DESTINATION ${CMAKE_INSTALL_PREFIX}/usr/bin )
Is there any other way? This solution doesn't look clean.. If I change the SomeOtherTool executable name I'll have to change it in here also. The same goes with install path.
I have just added google-test source code to libs/gtest-1.6.4 directory in my project. There is a libs/gtest-1.6.4/CMakeLists.txt file. In the top-most CMakeLists.txt, I have added add_subdirectory('libs/gtest-1.6.4'). The structure of the project is
|- CMakeLists.txt
|- src
|- CMakeLists.txt
|- *.h and *.cc
|- libs
|- gtest-1.6.4
|- CMakeLists.txt
|- gtest source code etc.
|- other subdirectories
Now I add #include "gtest/gtest.h" in one of the header file. Compilation fails with
gtest/gtest.h: No such file or directory
compilation terminated.
Here is the snippet of my src/CMakeLists.txt file.
set( Boost_USE_STATIC_LIBS ON )
find_package( Boost COMPONENTS graph regex system filesystem thread REQUIRED)
.. Normal cmake stuff ...
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} )
# This line is added for google-test
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS} ${COMMON_INCLUDES})
add_executable(Partitioner
print_function.cc
methods.cc
partitioner.cc
main.cc
)
TARGET_LINK_LIBRARIES(Partitioner ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(Partitioner ${GTEST_LIBRARIES})
What am I missing?
Looking at GTest's CMakeLists.txt, it looks like their include path is ${gtest_SOURCE_DIR}/include. They also define the library as a CMake target called gtest (this is wrapped in a macro cxx_library(gtest ...) currently on line 70).
So it looks like you need to do:
...
# This line is added for google-test
INCLUDE_DIRECTORIES(${GTEST_INCLUDE_DIRS} ${COMMON_INCLUDES})
INCLUDE_DIRECTORIES(${gtest_SOURCE_DIR}/include ${COMMON_INCLUDES})
...
TARGET_LINK_LIBRARIES(Partitioner ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(Partitioner ${GTEST_LIBRARIES})
TARGET_LINK_LIBRARIES(Partitioner ${Boost_LIBRARIES} gtest)
You'd also have to ensure that in your root CMakeLists.txt, you've called add_subdirectory(libs/gtest-1.6.4) before add_subdirectory(src) so that the GTest variables are correctly set when they're being used in "src/CMakeLists.txt".
As mentioned in accepted answer,
I did put my GoogleTest stuff up before my add_subdirectory() lines and it worked.
# The ROOT CMakeLists.txt
enable_testing()
include(CTest)
# https://google.github.io/googletest/quickstart-cmake.html
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/....zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)
add_subdirectory(some)
add_subdirectory(other)
add_subdirectory(another)
And in one of those sub directory, I see tests work.
# A CMakeLists.txt in a sub directory
#enable_testing() # NOT REQUIRED, hence, commented out
#include(CTest) # NOT REQUIRED, hence, commented out
#include(GoogleTest) # NOT REQUIRED, hence, commented out
add_executable(
mytest
test/mytest.cpp
)
target_link_libraries(
mytest
gtest_main
)
gtest_discover_tests(
mytest
)