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?
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++.
Related
I installed pcapplusplus on Ubuntu (Downloaded package from here: https://github.com/seladb/PcapPlusPlus/releases/tag/v21.11). The example that was in the archive compiles and works, everything is fine with it! But when I try to include the library in my project using CMake nothing works.
I write a line in CMakeLists.txt file:
include_directories("/usr/local/include/pcapplusplus")
After that, the header files are connected to the project. However, the project does not compile, various errors appear depending on the functions that I use. Most likely the linker does not see the files: libCommon++.a, libPacket++.a, and libPcap++.a.
I tried to connect them like this:
target_link_libraries(${PROJECT_NAME} libCommon++.a libPacket++.a libPcap++.a)
But it did not help.
Tried this:
find_package(pcapplusplus REQUIRED)
include_directories(${PCAPPLUSPLUS_INCLUDE_DIRS})
This didn't help either.
In fact, other people have already encountered such a problem, for example, netleap tom wrote about this on the StackOverflow. cmake linking against static libraries - do you have to tell cmake where to look?
However, no one there suggested a solution to him. I hope someone will tell me what to do.
udp.
Hello World from here for example:
#include <IPv4Layer.h>
#include <Packet.h>
#include <PcapFileDevice.h>
int main(int argc, char* argv[])
{
pcpp::PcapFileReaderDevice reader("1_packet.pcap");
if (!reader.open())
{
printf("Error opening the pcap file\n");
return 1;
}
pcpp::RawPacket rawPacket;
if (!reader.getNextPacket(rawPacket))
{
printf("Couldn't read the first packet in the file\n");
return 1;
}
if (parsedPacket.isPacketOfType(pcpp::IPv4))
{
pcpp::IPv4Address srcIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getSrcIpAddress();
pcpp::IPv4Address destIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getDstIpAddress();
printf("Source IP is '%s'; Dest IP is '%s'\n", srcIP.toString().c_str(), destIP.toString().c_str());
}
reader.close();
return 0;
}
If I add only this to CMake:
include_directories("/usr/local/include/pcapplusplus")
I have the following errors:
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `pcpp::Packet::~Packet()':
main.cpp:(.text._ZN4pcpp6PacketD2Ev[_ZN4pcpp6PacketD5Ev]+0x17): undefined reference to `pcpp::Packet::destructPacketData()'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `pcpp::Packet::~Packet()':
main.cpp:(.text._ZN4pcpp6PacketD0Ev[_ZN4pcpp6PacketD5Ev]+0x17): undefined reference to `pcpp::Packet::destructPacketData()'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `pcpp::IPv4Layer* pcpp::Packet::getLayerOfType<pcpp::IPv4Layer>(bool) const':
main.cpp:(.text._ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b[_ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b]+0x1b): undefined reference to `typeinfo for pcpp::IPv4Layer'
/usr/bin/ld: main.cpp:(.text._ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b[_ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b]+0x22): undefined reference to `typeinfo for pcpp::Layer'
/usr/bin/ld: main.cpp:(.text._ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b[_ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b]+0x4e): undefined reference to `typeinfo for pcpp::IPv4Layer'
/usr/bin/ld: main.cpp:(.text._ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b[_ZNK4pcpp6Packet14getLayerOfTypeINS_9IPv4LayerEEEPT_b]+0x55): undefined reference to `typeinfo for pcpp::Layer'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `main.cold':
main.cpp:(.text.unlikely+0x58): undefined reference to `pcpp::Packet::destructPacketData()'
/usr/bin/ld: main.cpp:(.text.unlikely+0x63): undefined reference to `pcpp::RawPacket::~RawPacket()'
/usr/bin/ld: main.cpp:(.text.unlikely+0x8a): undefined reference to `pcpp::IFileDevice::~IFileDevice()'
More here: image.
If I add this to CMake:
target_link_libraries(${PROJECT_NAME} libCommon++.a libPacket++.a libPcap++.a)
I have the following errors (first five):
/usr/bin/ld: /usr/local/lib/libPacket++.a(EthLayer.o): in function `pcpp::EthLayer::toString[abi:cxx11]() const':
/tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/EthLayer.cpp:104: undefined reference to `pcpp::MacAddress::toString[abi:cxx11]() const'
/usr/bin/ld: /tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/EthLayer.cpp:104: undefined reference to `pcpp::MacAddress::toString[abi:cxx11]() const'
/usr/bin/ld: /usr/local/lib/libPacket++.a(EthDot3Layer.o): in function `pcpp::EthDot3Layer::toString[abi:cxx11]() const':
/tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/EthDot3Layer.cpp:36: undefined reference to `pcpp::MacAddress::toString[abi:cxx11]() const'
/usr/bin/ld: /tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/EthDot3Layer.cpp:36: undefined reference to `pcpp::MacAddress::toString[abi:cxx11]() const'
/usr/bin/ld: /usr/local/lib/libPacket++.a(DhcpLayer.o): in function `pcpp::DhcpLayer::getClientHardwareAddress() const':
/tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/DhcpLayer.cpp:83: undefined reference to `pcpp::MacAddress::Zero'
/usr/bin/ld: /usr/local/lib/libPacket++.a(PayloadLayer.o): in function `pcpp::PayloadLayer::PayloadLayer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/tmp/cirrus-ci-build/PcapPlusPlus/Packet++/src/PayloadLayer.cpp:24: undefined reference to `pcpp::hexStringToByteArray(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*, unsigned long)'
More here: image2
undefined reference to `pcpp::IFileReaderDevice::IFileReaderDevice(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: main.cpp:(.text.startup+0x63): undefined reference to `vtable for pcpp::PcapFileReaderDevice'
/usr/bin/ld: main.cpp:(.text.startup+0xb5): undefined reference to `pcpp::IFileDevice::~IFileDevice()'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTIN4pcpp17IFileReaderDeviceE[_ZTIN4pcpp17IFileReaderDeviceE]+0x10): undefined reference to `typeinfo for pcpp::IFileDevice'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x28): undefined reference to `pcpp::IFileDevice::close()'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x38): undefined reference to `pcpp::IPcapDevice::setFilter(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x40): undefined reference to `pcpp::IPcapDevice::clearFilter()'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x78): undefined reference to `non-virtual thunk to pcpp::IPcapDevice::setFilter(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o:(.data.rel.ro._ZTVN4pcpp17IFileReaderDeviceE[_ZTVN4pcpp17IFileReaderDeviceE]+0x80): undefined reference to `non-virtual thunk to pcpp::IPcapDevice::clearFilter()'
collect2: error: ld returned 1 exit status
try this
target_link_libraries(${PROJECT_NAME} Pcap++ Packet++ Common++ pcap pthread)
this is the order as listed in the pcapplusplus example mk file /usr/local/etc/PcapPlusPlus.mk
Solution: https://github.com/gleb-kun/pcappp_hw
As expected, errors occurred due to incorrect library connection using CMake.
A file FindPcapPlusPlus.cmake is required to connect. Add the following lines to the main file CMakeLists.txt:
find_package(PcapPlusPlus REQUIRED)
target_link_libraries(${PROJECT_NAME} ${PcapPlusPlus_LIBRARIES})
FindPcapPlusPlus.cmake file contents:
if (PC_PcapPlusPlus_INCLUDEDIR AND PC_PcapPlusPlus_LIBDIR)
set(PcapPlusPlus_FIND_QUIETLY TRUE)
endif ()
find_package(PkgConfig REQUIRED)
pkg_check_modules(PC_PcapPlusPlus REQUIRED PcapPlusPlus)
set(PcapPlusPlus_VERSION ${PC_PcapPlusPlus_VERSION})
mark_as_advanced(PcapPlusPlus_INCLUDE_DIR PcapPlusPlus_LIBRARY)
foreach (LIB_NAME ${PC_PcapPlusPlus_LIBRARIES})
find_library(${LIB_NAME}_PATH ${LIB_NAME} HINTS ${PC_PcapPlusPlus_LIBDIR})
if (${LIB_NAME}_PATH)
list(APPEND PcapPlusPlus_LIBS ${${LIB_NAME}_PATH})
endif ()
endforeach ()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PcapPlusPlus
REQUIRED_VARS PC_PcapPlusPlus_INCLUDEDIR PC_PcapPlusPlus_LIBDIR
VERSION_VAR PcapPlusPlus_VERSION
)
if (PcapPlusPlus_FOUND)
set(PcapPlusPlus_INCLUDE_DIRS ${PC_PcapPlusPlus_INCLUDEDIR})
set(PcapPlusPlus_LIBRARIES ${PcapPlusPlus_LIBS})
endif ()
if (PcapPlusPlus_FOUND AND NOT TARGET PcapPlusPlus::PcapPlusPlus)
add_library(PcapPlusPlus::PcapPlusPlus INTERFACE IMPORTED)
set_target_properties(PcapPlusPlus::PcapPlusPlus PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${PcapPlusPlus_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${PcapPlusPlus_LIBRARIES}"
INTERFACE_COMPILE_FLAGS "${PC_PcapPlusPlus_CFLAGS}"
)
endif ()
I have downloaded the GTest sourcecode googletest-release-1.8.0.tar.gz (from : github ) and extracted it. (working on CentOS)
After that I ran following commands:
cd googletest-release-1.8.0\googletest.
cmake CMakeLists.txt.
Output:
-- Configuring done
-- Generating done
-- Build files have been written to: gtestframework/googletest-release-1.8.0/googletest
make.
Output:
[ 50%] Built target gtest
[100%] Built target gtest_main
After this I copied the file libgtest_main.a and libgtest.a into /usr/lib.
cd googletest-release-1.8.0\googlemock\gtest.
Copied libgtest_main.so libgtest.so libGTest.so in /usr/lib folder.
cd gtestframework\Testcode //{2 C++ files inside Testcode}
cmake CMakeLists.txt.
make.
I got this error:
Linking CXX executable runTests
CMakeFiles/runTests.dir/tests.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
tests.cpp:(.text+0x95a): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
tests.cpp:(.text+0xa14): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CMakeFiles/runTests.dir/tests.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<int, double>(char const*, char const*, int const&, double const&)':
tests.cpp:(.text._ZN7testing8internal18CmpHelperEQFailureIidEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal18CmpHelperEQFailureIidEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x6c): undefined reference to `testing::internal::EqFailure(char const*, char const*, std::string const&, std::string const&, bool)'
CMakeFiles/runTests.dir/tests.cpp.o: In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<double, double>(char const*, char const*, double const&, double const&)':
tests.cpp:(.text._ZN7testing8internal18CmpHelperEQFailureIddEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal18CmpHelperEQFailureIddEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x6c): undefined reference to `testing::internal::EqFailure(char const*, char const*, std::string const&, std::string const&, bool)'
collect2: error: ld returned 1 exit status
make[2]: *** [runTests] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2
CMakeLists.txt file content:
cmake_minimum_required(VERSION 2.6)
# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests tests.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)
I'm using CMAKE to build my project:
cmake_minimum_required(VERSION 3.14)
set (CMAKE_CXX_STANDARD 11)
project(saryxo_checker)
find_package( OpenCV REQUIRED )
find_package( Boost 1.71 REQUIRED COMPONENTS program_options filesystem system )
set(SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/src/main.cpp
${CMAKE_CURRENT_LIST_DIR}/src/FeatureEvaluator.cpp
${CMAKE_CURRENT_LIST_DIR}/src/Converter.cpp
${CMAKE_CURRENT_LIST_DIR}/core/util.cpp)
include_directories(
${CMAKE_CURRENT_LIST_DIR}/include
${CMAKE_CURRENT_LIST_DIR}/core
${OpenCV_INCLUDE_DIR}
${Boost_INCLUDE_DIR})
add_executable(saryxo_checker ${SOURCE_FILES})
target_link_libraries(saryxo_checker ${OpenCV_LIBS} ${Boost_LIBRARIES})
However, it throws an error during linking. The errors are thrown INSIDE of Boost.
[ 20%] Linking CXX executable saryxo_checker
CMakeFiles/saryxo_checker.dir/src/main.cpp.o: In Function »__static_initialization_and_destruction_0(int, int)«:
/usr/local/include/boost/system/error_code.hpp:221: Warning: undefined reference to »boost::system::generic_category()«
/usr/local/include/boost/system/error_code.hpp:222: Warning: undefined reference to »boost::system::generic_category()«
/usr/local/include/boost/system/error_code.hpp:223: Warning: undefined reference to »boost::system::system_category()«
CMakeFiles/saryxo_checker.dir/src/main.cpp.o: In function »boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*)«:
/usr/local/include/boost/program_options/detail/parsers.hpp:44: Warning: undefined reference to »boost::program_options::detail::cmdline::cmdline(std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)«
CMakeFiles/saryxo_checker.dir/src/main.cpp.o:(.data.rel.ro._ZTVN5boost15program_options11typed_valueIicEE[_ZTVN5boost15pro ram_options11typed_valueIicEE]+0x40): Warning: undefined reference to »boost::program_options::value_semantic_codecvt_helper<char>::parse(boost::any&, std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool) const«
CMakeFiles/keypoint_tracker_evaluator_saryxoS_Trian.dir/src/main.cpp.o:(.data.rel.ro._ZTVN5boost15program_options11typed_valueIjcEE[_ZTVN5boost15program_options11typed_valueIjcEE]+0x40): Warning: undefined reference to »boost::program_options::value_semantic_codecvt_helper<char>::parse(boost::any&, std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool) const«
CMakeFiles/saryxo_checker.dir/src/main.cpp.o:(.data.rel.ro._ZTVN5boost15program_options11typed_valueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEE[_ZTVN5boost15program_options11typed_valueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEE]+0x40): Warning: undefined reference to »boost::program_options::value_semantic_codecvt_helper<char>::parse(boost::any&, std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, bool) const«
CMakeFiles/saryxo_checker.dir/src/main.cpp.o: In function »boost::program_options::typed_value<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char>::xparse(boost::any&, std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) const«:
/usr/local/include/boost/program_options/detail/value_semantic.hpp:167: Warning: undefined reference to »boost::program_options::validate(boost::any&, std::__debug::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, int)«
CMakeFiles/saryxo_checker.dir/src/FeatureEvaluator.cpp.o: In function »__static_initialization_and_destruction_0(int, int)«:
/usr/local/include/boost/system/error_code.hpp:221: Warning: undefined reference to »boost::system::generic_category()«
/usr/local/include/boost/system/error_code.hpp:222: Warning: undefined reference to »boost::system::generic_category()«
/usr/local/include/boost/system/error_code.hpp:223: Warning: undefined reference to »boost::system::system_category()«
CMakeFiles/saryxo_checker.dir/src/Converter.cpp.o: In function »__static_initialization_and_destruction_0(int, int)«:
/usr/local/include/boost/system/error_code.hpp:221: Warning: undefined reference to »boost::system::generic_category()«
/usr/local/include/boost/system/error_code.hpp:222: Warning: undefined reference to »boost::system::generic_category()«
/usr/local/include/boost/system/error_code.hpp:223: Warning: undefined reference to »boost::system::system_category()«
CMakeFiles/saryxo_checker.dir/src/Converter.cpp.o: In function »boost::system::error_code::error_code()«:
/usr/local/include/boost/system/error_code.hpp:322: Warning: undefined reference to »boost::system::system_category()«
CMakeFiles/saryxo_checker.dir/src/Converter.cpp.o: In function »boost::system::error_code::clear()«:
/usr/local/include/boost/system/error_code.hpp:350: Warning: undefined reference to »boost::system::system_category()«
CMakeFiles/saryxo_checker.dir/core/util.cpp.o: In in function »__static_initialization_and_destruction_0(int, int)«:
/usr/local/include/boost/system/error_code.hpp:221: Warning: undefined reference to »boost::system::generic_category()«
/usr/local/include/boost/system/error_code.hpp:222: Warning: undefined reference to »boost::system::generic_category()«
/usr/local/include/boost/system/error_code.hpp:223: Warning: undefined reference to »boost::system::system_category()«
collect2: error: ld returned 1 exit status
CMakeFiles/saryxo_checker.dir/build.make:179: recipe for target 'saryxo_checker' failed
make[3]: *** [saryxo_checker] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/saryxo_checker.dir/all' failed
make[2]: *** [CMakeFiles/saryxo_checker.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/saryxo_checker.dir/rule' failed
make[1]: *** [CMakeFiles/saryxo_checker.dir/rule] Error 2
Makefile:118: recipe for target 'saryxo_checker' failed
make: *** [saryxo_checker] Error 2
I tried building boost in debug mode, as I build my program in debug mode as well, but the error stays the same. What could it be?
OS: Ubuntu 18.04
CMake: 3.14
Boost 1.71
recently I start study unit testing , and I want test my program with gtest. I install all with this order :
$ git clone https://github.com/google/googletest
$ cd googletest
$ cmake -DBUILD_SHARED_LIBS=ON .
$ make
$ cd googlemock
$ sudo cp ./libgmock_main.so ./gtest/libgtest.so gtest/libgtest_main.so ./libgmock.so /usr/lib/
$ sudo ldconfig
and now write code :
#include "gtest/gtest.h"
class Add
{
private:
int element;
public:
Add():element(0){}
~Add(){}
void setElement(int e){ element = e; }
int getElement() { return element; }
int adder(int e) { return element += e; }
};
class AddTest : public ::testing::Test
{
protected:
int abc(int a){
return a;
}
virtual void SetUp(){
add1.setElement(1);
add2.setElement(20);
}
virtual void TearDown(){}
Add add1;
Add add2;
};
TEST_F(AddTest, getTest)
{
EXPECT_EQ(1, add1.getElement());
EXPECT_EQ(20, add2.getElement());
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
end when I run test i get this error :
CMakeFiles/mock2.dir/main.cpp.o: In function AddTest_getTest_Test::TestBody()':
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::Message::Message()'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::internal::AssertHelper::operator=(testing::Message const&) const'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to testing::internal::AssertHelper::~AssertHelper()'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference totesting::Message::Message()'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference totesting::internal::AssertHelper::operator=(testing::Message const&) const'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::~AssertHelper()'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::internal::AssertHelper::~AssertHelper()'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::~AssertHelper()'
CMakeFiles/mock2.dir/main.cpp.o: In functionmain':
/home/artem/CLionProjects/mock2/main.cpp:39: undefined reference to testing::InitGoogleTest(int*, char**)'
CMakeFiles/mock2.dir/main.cpp.o: In function__static_initialization_and_destruction_0(int, int)':
/home/artem/CLionProjects/mock2/main.cpp:31: undefined reference to testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CMakeFiles/mock2.dir/main.cpp.o: In functionRUN_ALL_TESTS()':
/usr/local/include/gtest/gtest.h:2235: undefined reference to testing::UnitTest::GetInstance()'
/usr/local/include/gtest/gtest.h:2235: undefined reference totesting::UnitTest::Run()'
CMakeFiles/mock2.dir/main.cpp.o: In function AddTest::AddTest()':
/home/artem/CLionProjects/mock2/main.cpp:15: undefined reference totesting::Test::Test()'
CMakeFiles/mock2.dir/main.cpp.o: In function AddTest::~AddTest()':
/home/artem/CLionProjects/mock2/main.cpp:15: undefined reference totesting::Test::~Test()'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::internal::scoped_ptr<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::reset(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
/usr/local/include/gtest/internal/gtest-port.h:1172: undefined reference totesting::internal::IsTrue(bool)'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::internal::scoped_ptr<std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > >::reset(std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)':
/usr/local/include/gtest/internal/gtest-port.h:1172: undefined reference totesting::internal::IsTrue(bool)'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
/usr/local/include/gtest/gtest.h:1394: undefined reference totesting::AssertionSuccess()'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
/usr/local/include/gtest/gtest.h:1384: undefined reference totesting::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, bool)'
CMakeFiles/mock2.dir/main.cpp.o:(.rodata._ZTI7AddTest[_ZTI7AddTest]+0x10): undefined reference to `typeinfo for testing::Test'
collect2: error: ld returned 1 exit status
CMakeFiles/mock2.dir/build.make:94: recipe for target 'mock2' failed
make[3]: * [mock2] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mock2.dir/all' failed
make[2]: [CMakeFiles/mock2.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/mock2.dir/rule' failed
make[1]: [CMakeFiles/mock2.dir/rule] Error 2
Makefile:118: recipe for target 'mock2' failed
make: * [mock2] Error 2
but when use command
g++ main.cpp -o test -lgtest -lpthread
everytheng is good. How can I fix it and run it not in command line ?
If you are using CLion then you may have a CMakeLists.txt file you should add rules to link to the libraries. To do this add the following line to your CMakeLists.txt
enable_testing()
find_package(GTest REQUIRED) # Find the google testing framework on your system
include_directories(${GTEST_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GTEST_LIBRARIES}) # Replace ${PROJECT_NAME} with your target name
For more details go here.
I'm cross compiling project with following toolset:
cmake
Toolchain is arm-linux-gnueabihf
sysroot (which is actually rsync of necessary stuff from Raspberry Pi 2 and contains all of headers and libraries I need)
and list of find_package
Boost configured in following manner:
set(CMAKE_CXX_FLAGS ${CMAKE_CPU_FLAGS} "-DBOOST_ALL_DYN_LINK -fPIC -std=gnu++14 ${CMAKE_ARCH}")
set(Boost_DEBUG OFF)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(BOOST_ALL_DYN_LINK ON) # force dynamic linking for all libraries
set(BOOST_COMPONENTS
system
thread
program_options
regex
filesystem
unit_test_framework
date_time
chrono
log_setup
log
)
set(BOOST_ROOT ${CMAKE_SYSROOT})
set(BOOST_INCLUDEDIR ${BOOST_ROOT}/usr/inc)
set(BOOST_LIBRARYDIR ${BOOST_ROOT}/usr/lib/arm-linux-gnueabihf)
find_package(Boost 1.54.0 REQUIRED COMPONENTS ${BOOST_COMPONENTS})
Under the OS X compilation and linking just goes fine and I'm able send application to the RPI2 and run it.
The problem is in linking process under Ubuntu. It unable to find couple of symbols related to boost.log:
logger/liblogger.a(logger.cpp.o): In function `boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >& boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::formatted_write<wchar_t>(wchar_t const*, int)':
logger.cpp:(.text._ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE15formatted_writeIwEERS6_PKT_i[_ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE15formatted_writeIwEERS6_PKT_i]+0x76): undefined reference to `boost::log::v2_mt_posix::aux::code_convert(wchar_t const*, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::locale const&)'
logger/liblogger.a(logger.cpp.o): In function `void boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::aligned_write<wchar_t>(wchar_t const*, int)':
logger.cpp:(.text._ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE13aligned_writeIwEEvPKT_i[_ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE13aligned_writeIwEEvPKT_i]+0x6c): undefined reference to `boost::log::v2_mt_posix::aux::code_convert(wchar_t const*, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::locale const&)'
logger.cpp:(.text._ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE13aligned_writeIwEEvPKT_i[_ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE13aligned_writeIwEEvPKT_i]+0xc6): undefined reference to `boost::log::v2_mt_posix::aux::code_convert(wchar_t const*, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::locale const&)'
logger/liblogger.a(logger.cpp.o): In function `void boost::log::v2_mt_posix::sinks::basic_formatting_sink_frontend<char>::feed_record<boost::mutex, boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char> >(boost::log::v2_mt_posix::record_view const&, boost::mutex&, boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char>&)':
logger.cpp:(.text._ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS_5mutexENS2_26basic_text_ostream_backendIcEEEEvRKNS1_11record_viewERT_RT0_[_ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS_5mutexENS2_26basic_text_ostream_backendIcEEEEvRKNS1_11record_viewERT_RT0_]+0xda): undefined reference to `boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char>::consume(boost::log::v2_mt_posix::record_view const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
logger/liblogger.a(logger.cpp.o): In function `void boost::log::v2_mt_posix::sinks::basic_formatting_sink_frontend<char>::feed_record<boost::mutex, boost::log::v2_mt_posix::sinks::text_file_backend>(boost::log::v2_mt_posix::record_view const&, boost::mutex&, boost::log::v2_mt_posix::sinks::text_file_backend&)':
logger.cpp:(.text._ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS_5mutexENS2_17text_file_backendEEEvRKNS1_11record_viewERT_RT0_[_ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS_5mutexENS2_17text_file_backendEEEvRKNS1_11record_viewERT_RT0_]+0xda): undefined reference to `boost::log::v2_mt_posix::sinks::text_file_backend::consume(boost::log::v2_mt_posix::record_view const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
logger/liblogger.a(logger.cpp.o): In function `void boost::log::v2_mt_posix::sinks::basic_formatting_sink_frontend<char>::feed_record<boost::log::v2_mt_posix::aux::fake_mutex, boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char> >(boost::log::v2_mt_posix::record_view const&, boost::log::v2_mt_posix::aux::fake_mutex&, boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char>&)':
logger.cpp:(.text._ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS1_3aux10fake_mutexENS2_26basic_text_ostream_backendIcEEEEvRKNS1_11record_viewERT_RT0_[_ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS1_3aux10fake_mutexENS2_26basic_text_ostream_backendIcEEEEvRKNS1_11record_viewERT_RT0_]+0xda): undefined reference to `boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char>::consume(boost::log::v2_mt_posix::record_view const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
logger/liblogger.a(logger.cpp.o): In function `void boost::log::v2_mt_posix::sinks::basic_formatting_sink_frontend<char>::feed_record<boost::log::v2_mt_posix::aux::fake_mutex, boost::log::v2_mt_posix::sinks::text_file_backend>(boost::log::v2_mt_posix::record_view const&, boost::log::v2_mt_posix::aux::fake_mutex&, boost::log::v2_mt_posix::sinks::text_file_backend&)':
logger.cpp:(.text._ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS1_3aux10fake_mutexENS2_17text_file_backendEEEvRKNS1_11record_viewERT_RT0_[_ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS1_3aux10fake_mutexENS2_17text_file_backendEEEvRKNS1_11record_viewERT_RT0_]+0xda): undefined reference to `boost::log::v2_mt_posix::sinks::text_file_backend::consume(boost::log::v2_mt_posix::record_view const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
with:
make VERBOSE=1
seems all of libraries up there
/usr/local/gcc-linaro-5.1-2015.08-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ --sysroot=/home/amr/iot_hub/3rd-party/rpi2_sysroot -DBOOST_ALL_DYN_LINK -fPIC -std=gnu++14 -march=armv7-a -mtune=cortex-a7 -mfpu=vfp -mfloat-abi=hard -Wl,-rpath-link=/home/amr/iot_hub/3rd-party/rpi2_sysroot/lib/arm-linux-gnueabihf -Wl,-rpath-link=/home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf CMakeFiles/hub.dir/main/main.cpp.o -o hub -L/home/amr/iot_hub/3rd-party/rpi2_sysroot/lib/arm-linux-gnueabihf -L/home/amr/iot_hub/3rd-party/rpi2_sysroot/lib -L/home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib -L/home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf -L/home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/gcc/arm-linux-gnueabihf/5 -rdynamic sensor-framework/libsensor-framework.a transport-framework/libtransport-framework.a tools/libtools.a logger/liblogger.a database-connector/libdb-conn.a -lbluetooth /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libsoci_core.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libsoci_postgresql.so -lpq -lpthread /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libboost_program_options.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libboost_system.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libboost_thread.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libboost_regex.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libboost_filesystem.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libboost_unit_test_framework.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libboost_date_time.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libboost_chrono.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libboost_log.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libboost_log_setup.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libssl.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libcrypto.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libglib-2.0.a /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libgthread-2.0.so /home/amr/iot_hub/3rd-party/rpi2_sysroot/usr/lib/arm-linux-gnueabihf/libgmodule-2.0.so sensor-framework/libsensor-framework.a -Wl,-rpath,/lib/arm-linux-gnueabihf:/lib:/usr/lib:/usr/lib/arm-linux-gnueabihf:/usr/lib/gcc/arm-linux-gnueabihf/5:
What can be root cause of that issue?
Found some workaround.
I use different toolchains on OS X (4.9) and Ubuntu (5.1) which seems produces some difference in linking process (or there is no binary compatibility). Installing linaro toolchain on Ubuntu with same version as OS X have fixed the issue.