undefined reference to boost::system::generic_category() cmake - c++

I have an undefined reference while trying to compile my Qt5 project.
I am usually pretty confident with using CMake but this time I have a strange error that I can't figure out:
undefined reference to boost::system::generic_category()
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to 'boost::system::generic_category()'
My configurations are:
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Qt5.15
cmake version 3.16.3
After typing whereis boost the outcome wasboost: /usr/include/boost and after applying the great power of dpkg -s libboost-dev | grep 'Version' :) the version is Version: 1.71.0.0ubuntu2
I don't understand what is happening, below an example of how my CMakeLists.txt is structured:
cmake_minimum_required (VERSION 3.1)
project(projectA)
set (OpenCV_DIR /home/to/opencv/build)
find_package( OpenCV REQUIRED )
find_package( Boost COMPONENTS system thread filesystem REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5Widgets)
find_package(Qt5PrintSupport)
#make them into headers
qt5_wrap_ui (UI_HDRS ${UI})
add_executable(projectA main/main.cpp ui/qdarkstyle/style.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (projectA Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS} Qt5::PrintSupport)
add_library(projectA_lib SHARED ${SRCS} ${UI_HDRS})
target_include_directories (projectA_lib PUBLIC "src/" "ui/")
link_directories(${Boost_LIBRARY_DIRS})
target_link_libraries (projectA_lib Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS})
I have searched and applied solutions I saw on all possible sources I was able to find such as:
This source but that didn't work.
Also from here it seems that this solution shall be applied:
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS system)
# the call to include_directories is now useless:
# the Boost::system imported target used below
# embeds the include directories
project(APP C CXX)
add_executable(APP src.cpp)
target_link_libraries(APP Boost::system)
However that also didn't do any specific benefits to finding the solution.
Other posts I consulted were this, this one but no answer was provided.
This post was useful but that also didn't provide any advice that didn't already know.
Thanks for pointing to the right direction and trying to find a solution.

Related

How can I make fat execuatable file in CMAKE?

I am newbie on CMAKE. I am trying to make "fat executable file".
The term, fat executeable file, I mentioned, is a executable binary file which contains all shared libraries and can be just run in another environment.
Example (Trouble Situation)
There is ComputerA which is a computer that I used for developing. There is GCC, Boost libraries, librados etc.
And there is ComputerB which is a computer that I wanted to execute the binary that I built.
* Of course, Both Computer A and B have some conditions. Same Intel CPU Arch. (i7), Same CentOS.
I built binary file in ComputerA using CMAKE with this command, cmake .. && make. And I copied this file to ComputerB and executed.
When I execute the copied binary file in Computer B, it said like below.
[root#ComputerB ~]# ./binaryFile 123.123.123.123
./binaryFile: error while loading shared libraries: libboost_json.so.1.80.0: cannot open shared object file: No such file or directory
I understand what this error message say. There is no shared library which it needed. So, what I want to ask in this community is, What is CMAKE Command for containing shared libraries.
Similar thing :: fat JAR
The reason, why I called "fat executable file", is that there is a word "fat JAR" in Java. I am not familiar with Java. But it is commonly used word in Java community.
My CMAKE File
It contains some libraries like Boost, Rados, RBD etc. (There will be more according to progress of developing)
cmake_minimum_required(VERSION 3.2.0)
project(BinaryHelper VERSION 0.1.0)
find_package(Boost REQUIRED COMPONENTS json)
include_directories(${Boost_INCLUDE_DIR})
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lrados -std=c++17")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
add_executable(
${PROJECT_NAME}
main.cpp
utils/binary.cpp
utils/message.cpp
utils/header.cpp
)
target_link_libraries (
${PROJECT_NAME}
${Boost_LIBRARIES}
rados
rbd
Threads::Threads
)
Thanks to #HolyBlackCat, I solved the problem with static link.
Modified CMAKE
cmake_minimum_required(VERSION 3.2.0)
project(BinaryHelper VERSION 0.1.0)
find_package(Boost REQUIRED COMPONENTS json)
include_directories(${Boost_INCLUDE_DIR})
set(Boost_USE_STATIC_LIBS ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lrados -std=c++17")
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
add_library(
boost_json
STATIC
IMPORTED
)
set_target_properties(
boost_json
PROPERTIES
IMPORTED_LOCATION /usr/local/lib/libboost_json.a
)
add_executable(
${PROJECT_NAME}
main.cpp
utils/binary.cpp
utils/message.cpp
utils/header.cpp
)
# target_link_libraries (
# ${PROJECT_NAME}
# ${Boost_LIBRARIES}
# rados
# rbd
# Threads::Threads
# )
target_link_libraries(
${PROJECT_NAME}
boost_json
rados
rbd
Threads::Threads
)
There is additional(modified) keywords, add_library, set_target_properties, target_link_libraries.

How to add OpenXLSX to my project in c++?

I have a project and I want to use OpenXLSX as excel library. However, I can't add this to my project.
I move all OpenXLSX files into my project folder, it didn't work. Also moved them to my Desktop, it didn't work either.
Here is my CMakeList.txt file:
#cmake_minimum_required(VERSION 3.5)
#project(PCL_Visualizer LANGUAGES CXX)
#set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_CXX_STANDARD_REQUIRED ON)
#add_executable(PCL_Visualizer main.cpp)
cmake_minimum_required(VERSION 3.5)
project(mainwindow)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# init_qt: Let's do the CMake job for us
set(CMAKE_AUTOMOC ON) # For meta object compiler
set(CMAKE_AUTORCC ON) # Resource files
set(CMAKE_AUTOUIC ON) # UI files
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/home/fatih/Desktop")
# Find the QtWidgets library
find_package(Qt5 REQUIRED Widgets)
find_package(VTK REQUIRED)
find_package(PCL 1.7.1 REQUIRED)
find_package(OpenXLSX REQUIRED)
# Fix a compilation bug under ubuntu 16.04 (Xenial)
#list(REMOVE_ITEM PCL_LIBRARIES "vtkproj4")
include_directories(${PCL_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/OpenXLSX-master)
#include_directories(OpenXLSX-master/library/headers)
#include_directories(OpenXLSX-master/library/external/pugixml)
#include_directories(OpenXLSX-master/library/external/nowide/nowide)
#include_directories(OpenXLSX-master/library/external/zippy)
add_definitions(${PCL_DEFINITIONS})
set(project_SOURCES main.cpp mainwindow.cpp)
add_executable(${PROJECT_NAME} ${project_SOURCES} icons.qrc)
target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES} Qt5::Widgets)
Everytime I try to cmake .. and make, i get this error.
Could not find a package configuration file provided by "OpenXLSX" with any
of the following names:
OpenXLSXConfig.cmake
openxlsx-config.cmake
Add the installation prefix of "OpenXLSX" to CMAKE_PREFIX_PATH or set
"OpenXLSX_DIR" to a directory containing one of the above files. If
"OpenXLSX" provides a separate development package or SDK, be sure it has
been installed.
I just add these commands to my CMakeList.txt and problem solved partially.
include_directories(/home/fatih/Desktop/OpenXLSX-master/library)
include_directories(/home/fatih/Desktop/OpenXLSX-master/build/library)
include_directories(/home/fatih/Desktop/OpenXLSX-master/library/headers)
link_directories(/home/fatih/Desktop/OpenXLSX-master/build/library)
link_directories(/home/fatih/Desktop/OpenXLSX-master/build/library/headers)
link_directories(/home/fatih/Desktop/OpenXLSX-master/library/headers)
target_link_libraries(${PROJECT_NAME} OpenXLSX VERSION 0.2.0 LANGUAGES CXX)
However, now I get different type of error.
/usr/bin/ld: cannot find -lOpenXLSX
/usr/bin/ld: cannot find -lVERSION
/usr/bin/ld: cannot find -l0.2.0
/usr/bin/ld: cannot find -lLANGUAGES
/usr/bin/ld: cannot find -lCXX
/usr/bin/ld: cannot find -lOpenXLSX
/usr/bin/ld: cannot find -lVERSION
/usr/bin/ld: cannot find -l0.2.0
/usr/bin/ld: cannot find -lLANGUAGES
/usr/bin/ld: cannot find -lCXX
Edit: I solved that problem too. If someone gets an error like this, I changed the target_link_library comment to this:
target_link_libraries(${PROJECT_NAME} /home/fatih/Desktop/OpenXLSX-master/build/output/libOpenXLSX-shared.so)

CMake, MinGW=Undefined Reference to GetIpAddrTable-> iphlpapi.h

I have a CMake project that works pretty good in Linux, but it doesn't under Windows.
The first issue comes by final linking:
[100%] Linking CXX shared library libProjlib.dll
CMakeFiles\Projlib.dir/objects.a(configserver.cpp.obj): In function `ConfigServer::findAdapter(unsigned long*)':
C:/Users/jose/Documents/git-Projects/Proj/Projlib/configserver.cpp:859: undefined reference to `GetIpAddrTable'
Error 1
findAdapter belongs to C:\Qt\Tools\mingw810_64\x86_64-w64-mingw32\include\iphlpapi.h, but doesn't link automatic
My project is missconfigured, I think. I would like to select the MinGW compiler, automatic through CMakeLists.txt (For now, I configure the compiler means cmake-gui.exe ..), and naturally fix the issue, but I don't know how. (Google didn't gave me a solution)
Please, any help or suggestion, link.. etc. will be wellcomed.
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
set(CMAKE_PROJECT_NAME "testProject")
project(${CMAKE_PROJECT_NAME})
set(CMAKE_BUILD_TYPE Debug)
if("Windows" STREQUAL "${CMAKE_SYSTEM_NAME}")
message(STATUS "_______________________________________Compiling on Windows")
elseif("Linux" STREQUAL "${CMAKE_SYSTEM_NAME}")
message(STATUS "_______________________________________Compiling on GNU/Linux :-)")
endif()
set(CMAKE_BUILD_PARALLEL_LEVEL 8)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#list(APPEND CMAKE_PREFIX_PATH "/home/enigma/Qt/5.15.2/gcc_64") #linux
list(APPEND CMAKE_PREFIX_PATH "C:\\Qt\\Tools\\mingw810_64")
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
message(STATUS "___________________________________________________${CMAKE_BUILD_TYPE}")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(binPath "binDebugProj")
elseif()
set(binPath "binProj")
endif()
set(TMP_BUILD "tmpBuild")
set(Proj_LIB "Projlib")
add_subdirectory(${Proj_LIB} "${CMAKE_SOURCE_DIR}/../${binPath}/${TMP_BUILD}/${Proj_LIB}")
include_directories(${Proj_LIB})
The Solution is:
target_link_libraries(${CARDEA_LIB} PUBLIC Qt5::Core Qt5::Network iphlpapi)
At the end did work. (I was savin in anoter CMakeLists.txt AGGHH, too musch hours today)

undefined reference to `boost::system::generic_category()

I'm developing a cpp project in Clion. For the build I use cmake. Last weekend I upgraded from ubuntu 18.04 to 20.04, from then I cannot build my project and I get the error:
/usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'. It seems that something in the linking isn't right. I reinstalled all packages, tried different versions but nothing. My CMakeLists.txt is:
cmake_minimum_required(VERSION 3.7)
project(pki_cpp)
set(CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
#find_package(libmongocxx REQUIRED)
find_package(mongocxx REQUIRED)
find_package(bsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories(SYSTEM ./lib)
set(BOOST_ROOT /usr/lib/)
find_package(Boost 1.71.0 REQUIRED COMPONENTS system chrono thread filesystem)
include_directories(${Boost_INCLUDE_DIR})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
# A custom library
add_subdirectory(asnDatatypes)
include_directories({CMAKE_CURRENT_SOURCE_DIR} asnDatatypes)
set_property(TARGET asnLibrary PROPERTY POSITION_INDEPENDENT_CODE ON)
# end custom library
#cryptopp library
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/contrib/cmake")
find_package(CryptoPP REQUIRED)
include_directories(${CRYPTOPP_INCLUDE_DIR})
link_directories(${CryptoPP_INCLUDE_DIR})
# end cryptopp
add_executable(pki_cpp main.cpp rootCA.cpp rootCA.h)
target_include_directories(pki_cpp PRIVATE ${Boost_INCLUDE_DIRS} ${LIBMONGOCXX_INCLUDE_DIRS} )
link_directories(pki_cpp ${Boost_LIBRARY_DIRS})
target_link_libraries(pki_cpp ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_LIBRARIES} Boost::system Threads::Threads boost_system asnLibrary OpenSSL::SSL ${CRYPTOPP_LIBRARIES} ${LIBMONGOCXX_LIBRARIES} mongo::bsoncxx_shared mongo::mongocxx_shared ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} Boost::system)
Any help is appreciated!
That's a lot of stuff, all randomly ordered and commented out. The obvious error is a failure to link Boost System
I'd go with
FIND_PACKAGE(Boost 1.65.0
COMPONENTS system program_options REQUIRED)
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${Boost_INLUDE_DIRS})
LINK_LIBRARIES(${Boost_LIBRARIES})
Alternatively you can be specific and link specific libs to specific targets
TARGET_LINK_LIBRARIES(mylib ${Boost_SYSTEM_LIBRARY})
TARGET_LINK_LIBRARIES(myexecutable ${Boost_THREAD_LIBRARY})

I still get this error undefined reference to: boost::log::v2_mt_posix::trivial::logger::get()

Using cmake I am not able to use boost::log
I still get this error undefined reference to:
boost::log::v2_mt_posix::trivial::logger::get()
I am using boost 1.71. I have this working like:
- g++ ../log_example.cpp -DBOOST_LOG_DYN_LINK -lpthread -lboost_log -o log_example
linker error while linking boost log (undefined references to boost::log::v2_mt_posix::sinks)
linking boost log using cmake error
However, With cmake I have try several different options mention before. Not a single one has works for cmake.
The cmake is as follows
cmake_minimum_required(VERSION 3.10)
project(log_example)
set(CMAKE_CXX_STANDARD 17)
set(Boost_USE_DEBUG_LIBS OFF) # ignore debug libs and
set(Boost_USE_RELEASE_LIBS ON) # only find release libs
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(CMAKE_CXX_COMPILER "g++")
ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)
find_package(Boost 1.70.0 COMPONENTS thread log REQUIRED log_setup)
include_directories(${BOOST_INCLUDE_DIRS})
add_executable(log_example log_example.cpp)
target_link_libraries(log_example pthread ${Boost_LIBRARIES} ${Boost_LOG_LIBRARY} )
set(CMAKE_CXX_FLAGS "-g -Wall -DBOOST_LOG_DYN_LINK")
solved
Awesome thanks every one. The cmake now looks like this and works perfectly, less clutter and to the point.
cmake_minimum_required(VERSION 3.10)
project(log_example)
set(CMAKE_CXX_STANDARD 17)
find_package(Boost 1.70.0 COMPONENTS thread log log_setup)
include_directories(${BOOST_INCLUDE_DIRS})
add_executable(log_example log_example.cpp)
target_link_libraries(log_example pthread Boost::log Boost::log_setup )