'Cmake finds packages but is unable to link them
I'm trying to compile a CMake project that requires the import of two external libraries. Both libraries have their .so file in "/usr/lib" and "/usr/include". And I've created the Find.cmake file for both of them; here is an example (of FindECDSA.cmake):
find_path(ECDSA_INCLUDE_DIR NAMES ecdsa.h DOC "ECDSA-lib include directory")
find_library(ECDSA_LIBRARY NAMES ECDSA DOC "ECDSA-lib library")
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(ECDSA
REQUIRED_VARS ECDSA_INCLUDE_DIR ECDSA_LIBRARY)
if(ECDSA_FOUND AND NOT TARGET ECDSA::ECDSA)
add_library(ECDSA::ECDSA UNKNOWN IMPORTED)
set_target_properties(ECDSA::ECDSA PROPERTIES
IMPORTED_LOCATION "${ECDSA_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${ECDSA_INCLUDE_DIR}")
endif()
mark_as_advanced(ECDSA_INCLUDE_DIR ECDSA_LIBRARY)
set(ECDSA_INCLUDE_DIRS ${ECDSA_INCLUDE_DIR})
set(ECDSA_LIBRARIES ${ECDSA_LIBRARY})
Then, at the CMakeLists.txt they are found and linked to the target the following way:
cmake_minimum_required(VERSION 3.12)
project(GEONET VERSION 0.1)
set(CMAKE_CXX_STANDARD 11)
# project variables
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
set(CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/its")
find_package(ECDSA REQUIRED)
find_package(v2xSe REQUIRED)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
add_executable(geonet
application.cpp
...
)
target_link_libraries(geonet PUBLIC v2xSe::v2xSe ECDSA::ECDSA ...)
install(TARGETS geonet EXPORT ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
The thing is that the CMake finds the libraries with no problem, but on the linking stage of the compilation is completely unable to find the references. Here is the error:
CMakeFiles/geonet.dir/main.cpp.o: In function `main':
/src/src/main.cpp:181: undefined reference to `boost::log::v2s_mt_posix::core::set_filter(boost::log::v2s_mt_posix::filter const&)'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::compute_hash_sha256(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/src/src/ahd_connection.cpp:210: undefined reference to `ecdsa_sha256(void const*, unsigned int, unsigned char*)'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::sign_with_hsm(unsigned char*)':
/src/src/ahd_connection.cpp:225: undefined reference to `v2xSe_createRtSign(unsigned short, TypeHash_t*, unsigned short*, TypeSignature_t*)'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::compute_hash_sha256(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/src/src/ahd_connection.cpp:210: undefined reference to `ecdsa_sha256(void const*, unsigned int, unsigned char*)'
/src/src/ahd_connection.cpp:210: undefined reference to `ecdsa_sha256(void const*, unsigned int, unsigned char*)'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::verify_data(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/src/src/ahd_connection.cpp:310: undefined reference to `ecdsa_verify_signature(int, ecdsa_point_t, unsigned char*, ecdsa_sig_t, int, void (*)(void*, int, int), void*)'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::AHDConnection(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int const&)':
/src/src/ahd_connection.cpp:61: undefined reference to `v2xSe_generateRtEccKeyPair(unsigned short, unsigned char, unsigned short*, TypePublicKey_t*)'
/src/src/ahd_connection.cpp:83: undefined reference to `ecdsa_open()'
CMakeFiles/geonet.dir/ahd_connection.cpp.o: In function `AHDConnection::~AHDConnection()':
/src/src/ahd_connection.cpp:89: undefined reference to `ecdsa_close()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/geonet.dir/build.make:472: bin/geonet] Error 1
make[1]: *** [CMakeFiles/Makefile2:116: CMakeFiles/geonet.dir/all] Error 2
make: *** [Makefile:150: all] Error 2
What I did do wrong?
Solution 1:[1]
I've finally solved the issue: It happens to be that some of the imported libraries were just C libraries, so they had to be imported using the following lines:
extern "C" {
#include "v2xSe.h"
#include "ecdsa.h"
}
I discovered that they were possible to be compiled using GCC but not g++.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marias |
