Cmake linker error with shared libraries - cmake

Im having an error on a chain linking, I've read lots of post but can't find a solution.
I build 3 libraries, in separated folders:
lib_1 as shared without dependencies
lib_2 as shared requiring lib_1 (build without problems)
lib_3 as shared that requires both lib_1 & lib_2
Here are the simplified CMakelists.txt (path have been simplified & triple-checked and mines are ok)
project (lib_1)
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Release)
include_directories( lib_1/include/path/)
add_library(lib_1 SHARED lib_1/source/path)
====================================
cmake_minimum_required(VERSION 2.8)
project (lib_2)
set (CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Release)
include_directories( lib_2/include/path/)
add_library(lib_2 SHARED lib_2/source/path)
add_library(lib_1 SHARED IMPORTED)
set_property(TARGET lib_1 PROPERTY IMPORTED_LOCATION "lib_1/path/file.dylib")
=============
cmake_minimum_required(VERSION 2.8)
project (lib_3)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Release)
include_directories( lib_3/include/path/)
add_library(lib_3 SHARED lib_3/sources/path)
add_library(lib_1 SHARED IMPORTED)
set_property(TARGET lib_1 PROPERTY IMPORTED_LOCATION "/lib_1/path/file.dylib")
add_library(lib_2 SHARED IMPORTED)
set_property(TARGET lib_2 PROPERTY IMPORTED_LOCATION "/lib_2/path/file.dylib")
Funny thing is I think that add_library & set_property of lib_3 do nothing as removing the line doesn't change the error, i.e. I'm not doing what I should...but I've no idea anymore.
(Side note code runs smoothly if I build all files at once, but I'm trying my hands at shared libraries)
Error for reference:
[100%] Linking CXX shared library lib_3.dylib
Undefined symbols for architecture x86_64:
"LIB_1 function", referenced from:
in lib_3.cpp.o
[...]
Followed by all lib_1 & lib_2 functions marker as undefined

Related

CMake Submodules Do Not Resolve Project Dependencies on MSVC "Cannot open include files"

I am using the following cmake file:
cmake_minimum_required(VERSION 3.19)
project(Neon LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
option(LIBIGL_WITH_OPENGL "Use OpenGL" ON)
option(LIBIGL_WITH_OPENGL_GLFW "Use GLFW" ON)
option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use ImGui" ON)
include(libigl)
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
add_executable(Neon src/main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(Neon PUBLIC Eigen3::Eigen igl::core igl::opengl_glfw solvers)
add_subdirectory(solvers)
I have my main executable, and I have my subdirectory "solvers" which has some other numerical routines. The CMake file for that is as follows:
project(solvers)
add_library(${PROJECT_NAME} SHARED src/LinearElastic.cpp)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src)
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Config
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
target_link_libraries(${PROJECT_NAME} igl::core Eigen3::Eigen)
install(DIRECTORY include/ DESTINATION "${INSTALL_INCLUDE_DIR}")
install(EXPORT ${PROJECT_NAME}Config DESTINATION share/${PROJECT_NAME}/cmake)
export(TARGETS ${PROJECT_NAME} FILE ${PROJECT_NAME}Config.cmake
Eigen and IGL are both libraries which are found from the following:
if(TARGET igl::core)
return()
endif()
include(FetchContent)
FetchContent_Declare(
libigl
GIT_REPOSITORY https://github.com/libigl/libigl.git
GIT_TAG v2.3.0
)
# Note: In libigl v3.0.0, the following will become a one-liner:
# FetchContent_MakeAvailable(libigl)
FetchContent_GetProperties(libigl)
if(NOT libigl_POPULATED)
FetchContent_Populate(libigl)
endif()
list(PREPEND CMAKE_MODULE_PATH "${libigl_SOURCE_DIR}/cmake")
include(${libigl_SOURCE_DIR}/cmake/libigl.cmake)
Building the main executable has no issues whatsoever, however, when attempting to use the dependencies from the libigl library in the submodule, I am encountering a lot of problems where the imports are unable to resolve. In particular, when attempting to use Eigen, I get "Cannot open include file" errors.
I have tried googling this issue, but none seem to cover the case when I am using a custom-written include() for a library. Ordinarily I'd just find_package in the submodules, but this doesn't seem to work correctly. I assume this is something silly, or perhaps I'm misunderstanding. Please let me know if I can improve the clarity of the question.
The particular error is here:
#ifndef NEON_LINEARELASTIC_H
#define NEON_LINEARELASTIC_H
#include <Eigen/Dense> // "Cannot include" error
class LinearElastic {
public:
Eigen::MatrixXi foobar;
auto doIt() -> void;
};
#endif//NEON_LINEARELASTIC_
I was able to solve this problem. I am typically used to a unix-based environment, so when attempting to apply the same typical thinking to windows, I encountered a few issues, namely, the way different compilers handle shared linking. To avoid rehashing what is already known, I defer to this post to allow anyone else encountering this issue to solve the problem.
I changed the CMakeLists.txt for my main executable to the following:
cmake_minimum_required(VERSION 3.19)
project(Neon LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(INSTALL_LIB_DIR lib CACHE PATH "Install directory for library code")
set(INSTALL_BIN_DIR CACHE PATH "Install directory for executables")
set(INSTALL_INCLUDE_DIR include CACHE PATH "Install directory for header files")
list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
option(LIBIGL_WITH_OPENGL "Use OpenGL" ON)
option(LIBIGL_WITH_OPENGL_GLFW "Use GLFW" ON)
option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use ImGui" ON)
include(libigl)
if(MSVC) // This line fixed it!
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
set(BUILD_SHARED_LIBS TRUE)
endif()
foreach(p LIB BIN INCLUDE CMAKE)
set(var INSTALL_${p}_DIR)
if(NOT IS_ABSOLUTE "${${var}}")
set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
endif()
endforeach()
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_BINARY_DIR}")
add_executable(Neon src/main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(Neon PRIVATE Eigen3::Eigen igl::core igl::opengl_glfw solvers)
add_subdirectory(solvers)
By setting the windows-specific options, the app was able to build and run without issue.

What does this CMake error mean? And how could I go about resolving it?

I get this error, which I've been trying to understand for some time now but getting nowhere.
add_library cannot create target "Plugin" because another target
with the same name already exists. The existing target is a shared library created
in source directory "D:/CHAI3D/SOFA/src/applications/plugins/plugin".
See documentation for policy CMP0002 for more details.
Below is the CMakelists.txt added for reference. I tried to remove the unnecessary code. So far, I've tried allowing duplicate targets with set(ALLOW_DUPLICATE_CUSTOM_TARGETS TRUE)
But to no avail
cmake_minimum_required(VERSION 3.1)
project(Plugin VERSION 21.06.99)
# Policies
cmake_policy(SET CMP0079 NEW)
set(ALLOW_DUPLICATE_CUSTOM_TARGETS TRUE)
set (PLUGIN_VERSION ${PROJECT_VERSION})
set(HEADER_FILES
src/initPlugin.h
...
)
set(SOURCE_FILES
src/initPlugin.cpp
...
)
file(GLOB_RECURSE RESOURCE_FILES "*.md" "*.psl" "*.py" "*.pyscn" "*.scn" "*.ah")
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${RESOURCE_FILES} )
target_include_directories(${PROJECT_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>")
set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-DSOFA_BUILD_PLUGIN")
target_link_libraries(${PROJECT_NAME} SofaCore SofaConstraint SofaSimpleFem SofaBaseMechanics SofaRigid SofaBaseVisual SofaOpenglVisual)
## Install rules for the library and headers; CMake package configurations files
sofa_create_package_with_targets(
PACKAGE_NAME ${PROJECT_NAME}
PACKAGE_VERSION ${PROJECT_VERSION}
TARGETS ${PROJECT_NAME} AUTO_SET_TARGET_PROPERTIES
INCLUDE_SOURCE_DIR "src"
INCLUDE_INSTALL_DIR ${PROJECT_NAME}
RELOCATABLE "plugins"
)
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(TARGET ${PROJECT_NAME} PROPERTY FOLDER "plugins")

Cannot specify compile options for imported target "..."

I want to provide the users of my library with two targets: one that specifies the include path etc., and one that carries useful extra compile options. However, for the extra target some of my users are getting the error
Cannot specify compile options for imported target "myproject::extra"
so it seems on older CMake versions.
I tested with CMake 3.9.2. The test project, including CI is on GitHub, with failing build here.
(How) can my approach be rendered robust for all CMake versions?
The project's main CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(myproject)
add_library(myproject INTERFACE)
set(MYPROJECT_VERSION "1.0.0")
target_include_directories(myproject INTERFACE
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/" DESTINATION include)
install(TARGETS myproject EXPORT myproject-targets)
install(EXPORT myproject-targets FILE myprojectTargets.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/myproject")
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/myprojectConfigVersion.cmake" VERSION ${MYPROJECT_VERSION} COMPATIBILITY AnyNewerVersion)
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/myprojectConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/myprojectConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/myproject")
The project's myprojectConfig.cmake:
include(CMakeFindDependencyMacro)
if(NOT TARGET myproject)
include("${CMAKE_CURRENT_LIST_DIR}/myprojectTargets.cmake")
endif()
if(NOT TARGET myproject::extra)
add_library(myproject::extra INTERFACE IMPORTED)
if(MSVC)
target_compile_options(myproject::extra INTERFACE /W4)
else()
target_compile_options(myproject::extra INTERFACE -Wall)
endif()
endif()
The user's project CMakeLists.txt could then look as follows:
cmake_minimum_required(VERSION 3.0)
project(myexec)
find_package(myproject REQUIRED)
add_executable(myexec main.cpp)
target_link_libraries(myexec PRIVATE myproject myproject::extra)
List of functions applicable for IMPORTED and INTERFACE targets changes as CMake evolves.
Most of such functions affects only on specific target properties. So, instead of calling a function, you may set the property directly. This will work in any CMake version:
# Works only in new CMake versions
target_compile_options(myproject::extra INTERFACE /W4)
# Equivalent which works in any CMake version
set_property(TARGET myproject::extra PROPERTY INTERFACE_COMPILE_OPTIONS /W4)

CMake: How to use LINK_INTERFACE_MULTIPLICITY?

Linking against the Intel MKL static libraries introduces circular dependencies. When I import the libraries,
set(LIBRARIES mkl_intel_lp64 mkl_sequential mkl_core)
foreach(_lib ${LIBRARIES})
add_library(${_lib} UNKNOWN IMPORTED)
set_target_properties(${_lib} PROPERTIES IMPORTED_LOCATION
/opt/intel/mkl/lib/intel64/lib${_lib}.a)
endforeach()
and link to my executable,
target_link_libraries(main PRIVATE ${LIBRARIES})
I get a ton of undefined references to the linear algebra calls. For example,
ztrevc3_gen.f:(.text+0x1af7): undefined reference to `mkl_blas_zdscal'
One way to get around this is to use the appropriate linker flags:
target_link_libraries(main PRIVATE -Wl,--start-group ${LIBRARIES} -Wl,--end-group)
Another option is to do this:
target_link_libraries(main PRIVATE ${LIBRARIES} ${LIBRARIES} ${LIBRARIES})
However, as I was searching for a more elegant solution I came across the LINK_INTERFACE_MULTIPLICITY property. If set this property along with the imported library location,
set(LIBRARIES mkl_intel_lp64 mkl_sequential mkl_core)
foreach(_lib ${LIBRARIES})
add_library(${_lib} UNKNOWN IMPORTED)
set_target_properties(${_lib} PROPERTIES IMPORTED_LOCATION
/opt/intel/mkl/lib/intel64/lib${_lib}.a
LINK_INTERFACE_MULTIPLICITY 3)
endforeach()
I get the same undefined references as before, so appearently this is not working. What is the proper way to use LINK_INTERFACE_MULTIPLICITY and is there a more elegant way to get around circular dependencies?
EDIT
Here is a minimal example that fails, this time with the correct IMPORTED_LINK_INTERFACE_MULTIPLICITY variable.
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(test Fortran)
add_executable(main main.f90)
set(LIBRARIES mkl_intel_lp64 mkl_sequential mkl_core)
foreach(_lib ${LIBRARIES})
add_library(${_lib} UNKNOWN IMPORTED)
set_target_properties(${_lib} PROPERTIES IMPORTED_LOCATION
/opt/intel/mkl/lib/intel64/lib${_lib}.a
IMPORTED_LINK_INTERFACE_MULTIPLICITY 3)
endforeach()
list(APPEND LIBRARIES dl pthread)
target_link_libraries(main ${LIBRARIES})
#target_link_libraries(main ${LIBRARIES} ${LIBRARIES} ${LIBRARIES})
# main.f90
call zpotrf
end program
If you uncomment the last line then the build succeeds. Unfortunately, the MKL in not free (except in some cases) but hopefully somebody can test this. I should note that it fails with some linear algebra calls and not others like dgemm.
Property LINK_INTERFACE_MULTIPLICITY is for "normal" STATIC library. For IMPORTED library IMPORTED_LINK_INTERFACE_MULTIPLICITY property should be used instead:
set_target_properties(${_lib} PROPERTIES
IMPORTED_LOCATION /opt/intel/mkl/lib/intel64/lib${_lib}.a
IMPORTED_LINK_INTERFACE_MULTIPLICITY 3)

How to instruct CMake to use external pre-built libraries

I'm struggling how to find a way to say to CMake to include external prebuilt libraries... It's driving me insane.
It's understatement that I'm new to CMake. I just want that MinGW linker add two external .lib files to link list... I'm pulling my hair at this point.
This is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.9)
project(opengltest C CXX)
set(CMAKE_CXX_STANDARD 11)
file(GLOB
TestSRC
"src/*.h"
"src/*.cpp"
"src/*.c"
)
add_executable(opengltest ${TestSRC})
include_directories(
3rdparty/glew/include
3rdparty/glfw/include
)
link_directories(
${PROJECT_SOURCE_DIR}/3rdparty/glew/lib/Release/x64/
${PROJECT_SOURCE_DIR}/3rdparty/glfw/build/src/Debug/
)
target_link_libraries(${PROJECT_NAME} glew32s glfw3)
Linker says it cannot find glew32s and glfw3.
EDIT: I think I've found the solution:
...
add_library(glew32s STATIC IMPORTED)
set_property(TARGET glew32s PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/glew32s.lib)
add_library(glfw3 STATIC IMPORTED)
set_property(TARGET glfw3 PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/glfw3.lib)
target_link_libraries(${PROJECT_NAME} glew32s glfw3)
I've moved all my .lib files to one folder and used add_library to include them.
...
add_library(glew32s STATIC IMPORTED)
set_property(TARGET glew32s PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/glew32s.lib)
add_library(glfw3 STATIC IMPORTED)
set_property(TARGET glfw3 PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/glfw3.lib)
target_link_libraries(${PROJECT_NAME} glew32s glfw3)
After that linker was able to find lib files.