CPPREST SDK on CLion using CMake on Mac - c++

I am attempting to use the CPPREST SDK in CLion on a Mac using CMake. I'm almost there, but I cannot seem to resolve the following linker error:
Undefined symbols for architecture x86_64:
"_ERR_remove_thread_state", referenced from:
boost::asio::ssl::detail::openssl_init_base::do_init::~do_init() in PongRemote.cpp.o
I have specified -lssl and -lcrypto, but still get this "thread state" error. Based on some research, it looks like it is coming from OpenSSL. I used Homebrew to install CPPREST.
I have tested this with literally just a main and the basic includes:
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
with a CMake of:
project(cpprest)
cmake_minimum_required(VERSION 3.8)
set(CMAKE_CXX_STANDARD 14)
# BOOST PACKAGE
find_package(Boost REQUIRED COMPONENTS
atomic
chrono
date_time
exception
filesystem
random
regex
serialization
system
thread
)
include_directories(${Boost_INCLUDE_DIRS})
IF (!Boost_FOUND)
MESSAGE("*** ERROR *** Boost package not found")
RETURN()
ENDIF ()
# Microsoft RESTful API Package (Casablanca)
set(CPPREST_LIBRARIES "/usr/local/opt/openssl/lib")
include_directories("/usr/local/opt/openssl/include")
# Compile and link
# Build the core library and executable
include_directories(${CMAKE_SOURCE_DIR})
set(SOURCE_FILES
main.cpp
)
set(LINK_LIBRARIES
${Boost_LIBRARIES}
${CPPREST_LIBRARIES}
-lssl
-lcrypto
)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${LINK_LIBRARIES})

After taking some time, the linking problem was due to the CMake find commands not working properly. I manually pointed directly to the libraries for both OpenSSL and RESTCPP and the problem was fixed.
Project(cpprest)
cmake_minimum_required(VERSION 3.8)
set(CMAKE_CXX_STANDARD 17)
# BOOST PACKAGE
set(Boost_USE_MULTITHREADED ON) # Default ON
set(Boost_USE_STATIC_LIBS ON) # Default OFF
set(Boost_USE_DEBUG_RUNTIME OFF) # Default ON
set(Boost_USE_DEBUG_PYTHON OFF) # Default OFF
set(Boost_USE_STLPORT OFF) # Default OFF
find_package(Boost REQUIRED COMPONENTS
atomic
chrono
date_time
exception
filesystem
program_options
random
regex
system
serialization
thread
)
IF (!Boost_FOUND)
MESSAGE("*** ERROR *** Boost package not found")
RETURN()
ENDIF ()
include_directories(${Boost_INCLUDE_DIRS})
MESSAGE("Boost_INCLUDE_DIRS:\t" ${Boost_INCLUDE_DIRS})
# Open SSL Package
set(OpenSSL_INCLUDE /usr/local/opt/openssl/include)
set(OpenSSL_LIBRARIES
/usr/local/opt/openssl/lib/libcrypto.dylib
/usr/local/opt/openssl/lib/libssl.dylib)
include_directories(${OpenSSL_INCLUDE})
# Microsoft RESTful API Package (Casablanca)
set(CppREST_INCLUDE /usr/local/opt/cpprestsdk/include)
set(CppREST_LIBRARIES /usr/local/opt/cpprestsdk/lib/libcpprest.dylib)
include_directories(${CppREST_INCLUDE})
# Compile and link
# Build the core library and executable
set(SOURCE_FILES main.cpp)
set(LINK_LIBRARIES
${Boost_LIBRARIES}
${OpenSSL_LIBRARIES}
${CppREST_LIBRARIES}
)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${LINK_LIBRARIES})

Related

Symbol not found error for matplotlibcpp, building with CMake

I am trying to use CMake for including Matplotlibcpp in my C++ project (I am new to both CMake and C++). I am following https://github.com/lava/matplotlib-cpp/issues/236#issuecomment-716510547 for setting up a CMake file. The project builds fine, but when I try to run the executable, here is the error I get:
dyld[73245]: symbol not found in flat namespace
'_PyCapsule_GetPointer'
zsh: abort ./data_measure_img_seq
I'm not sure how to resolve this. Any suggestions? For reference, I am putting my CMakeLists file below:
# set the minimum version
cmake_minimum_required(VERSION "3.13")
# set project
project(image-data-cv)
set(OpenCV_DIR /Users/anshgodha/Developer/opencv/install/lib/cmake/opencv4)
set(CMAKE_CXX_STANDARD 14)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
# for matplotlibcpp
find_package(Python3 COMPONENTS Interpreter Development NumPy REQUIRED)
find_package(PythonLibs 3.0 REQUIRED)
include_directories(${PYTHON3_INCLUDE_DIRS} ${NumPy_INCLUDE_DIRS})
include(FetchContent)
FetchContent_Declare(
matplotlib
GIT_REPOSITORY https://github.com/lava/matplotlib-cpp.git
GIT_TAG f23347fca25219d1c42cbb91608b5556814bf572
)
FetchContent_GetProperties(matplotlib)
if(NOT matplotlib_POPULATED)
FetchContent_Populate(matplotlib)
endif()
include_directories(SYSTEM ${matplotlib_SOURCE_DIR})
set(PROJECTS basic_data_measures;data_measure_img_seq)
foreach(PROJECT ${PROJECTS})
add_executable(${PROJECT} ${PROJECT}/main.cpp)
target_link_libraries(${PROJECT} PUBLIC ${OpenCV_LIBS})
set_target_properties(${PROJECT} PROPERTIES OUTPUT_NAME "${PROJECT}")
endforeach(PROJECT ${PROJECTS})
# link python and numpy
target_link_libraries(data_measure_img_seq
PRIVATE
${PYTHON_LIBRARIES}
Python3::NumPy
)
I was having the same issue compiling using cmake. Perhaps the included cmake doesn't link everything properly. I got around it using e.g.
g++ fill.cpp -o fill -std=c++11 -I/usr/include/python2.7 -lpython2.7

Unable to find the requested Boost libraries boost_lboost_thread

I have been trying to run a Qt project in a CMake environment. The Qt project is using openCV and Boost dependencies. After successfully solving all compiling errors, I have been struggling with a Boost error.
As soon as I run CMake I get the following error:
Running "/usr/bin/cmake /home/labrat/Desktop/cam-proc '-GCodeBlocks - Unix Makefiles'" in /home/labrat/Desktop/cam-proc/build.
**CMake Error at /usr/share/cmake-3.5/Modules/FindBoost.cmake:1677 (message):**
Unable to find the requested Boost libraries.
Boost version: 1.58.0
Boost include path: /usr/include
Could not find the following Boost libraries:
boost_lboost_thread
Some (but not all) of the required Boost libraries were found. You may
need to install these additional Boost libraries. Alternatively, set
BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT
to the location of Boost.
**Call Stack (most recent call first):
src/libCam/CMakeLists.txt:7 (find_package)**
Boost version: 1.58.0
Found the following Boost libraries:
system
thread
filesystem
chrono
date_time
atomic
-- Configuring incomplete, errors occurred!
See also "/home/labrat/Desktop/cam-proc/build/CMakeFiles/CMakeOutput.log".
See also "/home/labrat/Desktop/cam-proc/build/CMakeFiles/CMakeError.log".
*** cmake process exited with exit code 1.
I analyzed the error given CMake Error at /usr/share/cmake-3.5/Modules/FindBoost.cmake:1677 (message): and opened the enormous FindBoost.cmake file and here is the part where the error is, according to the warning, located:
message(SEND_ERROR "Unable to find the requested Boost libraries.\n${Boost_ERROR_REASON}")
# Add pthread library on UNIX if thread component was found
_Boost_consider_adding_pthreads(Boost_LIBRARIES ${Boost_LIBRARIES})
else()
if(Boost_FIND_REQUIRED)
message(SEND_ERROR "Unable to find the requested Boost libraries.\n${Boost_ERROR_REASON}")
else()
if(NOT Boost_FIND_QUIETLY)
# we opt not to automatically output Boost_ERROR_REASON here as
# it could be quite lengthy and somewhat imposing in its requests
# Since Boost is not always a required dependency we'll leave this
# up to the end-user.
if(Boost_DEBUG OR Boost_DETAILED_FAILURE_MSG)
message(STATUS "Could NOT find Boost\n${Boost_ERROR_REASON}")
else()
message(STATUS "Could NOT find Boost")
endif()
endif()
endif()
endif()
Also since another part of the error message said Call Stack (most recent call first): src/libCam/CMakeLists.txt:7 (find_package) here I am attaching the CMakeLists.txt realted to the libcam project if that's helpful:
cmake_minimum_required (VERSION 2.8.12)
project(libCam)
find_package(Qt5Widgets REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Boost COMPONENTS filesystem lboost_thread system REQUIRED)
find_package(Boost COMPONENTS system thread filesystem REQUIRED)
find_package(Qt5PrintSupport REQUIRED)
###
# make sure we use c++11
###
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
INCLUDE_DIRECTORIES(${Qt5Widgets_INCLUDE_DIRS})
include_regular_expression("^([^b]|b[^o]|bo[^o]|boo[^s]|boos[^t]|boost[^/]).*$")
qt5_wrap_ui (UIS_HDRS qtinclude/imagemanager.ui qtinclude/stereomanager.ui qtinclude/stereolistwidget.ui qtinclude/connectionmenu.ui)
file(GLOB LIBCAM_SRCS
"include/*.h"
"include/*.cpp"
"include/*.hpp"
"qtinclude/*.h"
"qtinclude/*.cpp"
"qtinclude/*.hpp"
)
file(GLOB UI_RC
"qtinclude/qdarkstyle/*.qrc"
)
add_library(libCam SHARED ${LIBCAM_SRCS} ${UIS_HDRS} ${UI_RC})
target_include_directories (libCam PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} )
target_link_libraries (libCam Qt5::Widgets Qt5::PrintSupport Qt5::Core Qt5::Quick Qt5::Sql Qt5::XmlPatterns ${Boost_LIBRARIES} ${OpenCV_LIBS} )
I have been googling the error for the past two days looking for information and code to solve this problem, but I am running out of ideas. I tried to download boost 1.55 but it didn't work as the compiler only sees boost 1.58.
Any idea on how to shed light on this matter? Is there something I am leaving out?
The problem is the line
find_package(Boost COMPONENTS filesystem lboost_thread system REQUIRED)
which should be changed to
find_package(Boost COMPONENTS filesystem thread system REQUIRED)
or simply deleted, since it is then equivalent to the following line.

C++ How to link boost libraries with my shared library to work on the target machine?

I try to make jni shared library.
I use boost library in the code of this library.
I successfuly make shared library on the development machine and test's done.
I make boost lib folder which have libboost_*.so files in on target machine and add it to LD_LIBRARY_PATH.
So, I try to test on the target machine (same OS platform with development machine) but target machine cannot link it.
So, my java program find my .so (libsample.so) but libsample.so can't find boost library and throw message ( undefined symbol: _ZTIN5boost6detail16thread_data_baseE)
How can I solve this problem?
I want to pack the boost libaries on my shared library
or I want to dynamically linking with boost library with my shared libarary.
my project's CMakeLists.txt following
cmake_minimum_required(VERSION 3.0)
### This CMakeLists.txt : Root CMake of this project
################## complie settings of this project ##################
set(ARTIFACT_NAME "sample-plugin")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread ")
add_definitions("-Wno-deprecated-declarations")
add_definitions("-Wno-write-strings")
################## Boost Settings ##################
set(Boost_NO_SYSTEM_PATH ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
set(BOOST_INCLUDE_DIR "${BOOST_ROOT}/boost")
include_directories(${BOOST_INCLUDE_DIR})
link_directories(${BOOST_INCLUDE_DIR})
link_directories(${BOOST_LIBRARY_DIR})
find_package(Boost 1.58.0 REQUIRED)
include_directories("$ENV{JAVA_HOME}/include")
if (WIN32)
include_directories("$ENV{JAVA_HOME}/include/win32")
else ()
include_directories("$ENV{JAVA_HOME}/include/linux")
endif ()
add_library(${ARTIFACT_NAME} SHARED ${SOURCES} )
target_link_libraries(${ARTIFACT_NAME} ${Boost_LIBRARIES})
Thanks, Martin Bonner.
I solve this problem.
Here is my edited CMakeLists.txt
################## complie settings of this project ##################
set(ARTIFACT_NAME "sample-plugin")
#set(CMAKE_CXX_STANDARD 11) # 아래에 -std=c++11 옵션과 중복
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC -std=c++11 -pthread ")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_definitions("-Wno-deprecated-declarations")
add_definitions("-Wno-write-strings")
################## Boost Settings ##################
set(Boost_NO_SYSTEM_PATH ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
set(BOOST_INCLUDE_DIR "${BOOST_ROOT}/boost")
include_directories(${BOOST_INCLUDE_DIR})
link_directories(${BOOST_INCLUDE_DIR})
link_directories(${BOOST_LIBRARY_DIR})
unset(Boost_INCLUDE_DIR CACHE)
unset(Boost_LIBRARY_DIRS CACHE)
find_package(Boost 1.58.0 REQUIRED COMPONENTS thread date_time filesystem system program_options )
################## Target Settings ##################
add_library(${ARTIFACT_NAME} SHARED ${SOURCES})
set_target_properties(${ARTIFACT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(${ARTIFACT_NAME} ${Boost_LIBRARIES})
Before this cmake run, I recompile boost libraries with -cxxflags=-fPIC because of this issue
I successfuly make .so library including boost libraries as static in it.

Link boost libraries statically using CMake in CLion

CLion 1.2, with bundled CMake 3.3.2 and MinGW-w64 4.8.4
I need to get a single DLL in a result of building that no need any other libraries to work. But can't link Boost libraries statically. I bootstrapped and built Boost with corresponding MinGW.
cmake_minimum_required(VERSION 3.3)
project(SampleProject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BOOST_ROOT "..\\lib\\boost_1_59_0")
set(Boost_USE_STATIC_LIBS ON)
set(BOOST_COMPONENTS_NEEDED filesystem )
find_package(Boost 1.59.0 REQUIRED COMPONENTS ${BOOST_COMPONENTS_NEEDED})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
include_directories(${Boost_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--kill-at -static-libgcc -static-libstdc++")
add_library(${CMAKE_PROJECT_NAME} SHARED ${SOURCE_FILES})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}\\..\\..\\output")
target_link_libraries(${CMAKE_PROJECT_NAME} ${Boost_LIBRARIES})
Build output:
O:/SampleProject/Cpp/lib/boost_1_59_0/stage/lib/libboost_filesystem-mgw48-mt-d-1_59.a(operations.o): In function error':
O:\SampleProject\Cpp\lib\boost_1_59_0/libs/filesystem/src/operations.cpp:286: undefined reference toboost::system::system_category()'
What else should I do to link with boost?
UPDATE: there is a list of built libraries
libboost_filesystem-mgw48-1_59.a
libboost_filesystem-mgw48-d-1_59.a
libboost_filesystem-mgw48-mt-1_59.a
libboost_filesystem-mgw48-mt-d-1_59.a
libboost_filesystem-mgw48-mt-s-1_59.a
libboost_filesystem-mgw48-mt-sd-1_59.a
libboost_filesystem-mgw48-s-1_59.a
libboost_filesystem-mgw48-sd-1_59.a
libboost_system-mgw48-1_59.a
libboost_system-mgw48-d-1_59.a
libboost_system-mgw48-mt-1_59.a
libboost_system-mgw48-mt-d-1_59.a
libboost_system-mgw48-mt-s-1_59.a
libboost_system-mgw48-mt-sd-1_59.a
libboost_system-mgw48-s-1_59.a
libboost_system-mgw48-sd-1_59.a
This looks like a linker error suggesting that you are not linking to Boost::system
You need to add system to BOOST_COMPONENTS_NEEDED. Change this line and see if it helps
set(BOOST_COMPONENTS_NEEDED system filesystem )

Cmake error when adding Boost library

I am trying to add Boost library to my project using the CMakeLists.txt in the follwing way:
set(BOOST_INCLUDEDIR "C:/boost_1_57_0")
set(BOOST_LIBRARYDIR "C:/boost_1_57_0/stage/lib")
find_package(Boost 1.57.0 COMPONENTS filesystem)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(test test.cpp)
target_link_libraries(test ${Boost_LIBRARIES})
However, I get the followng error: LINK : fatal error LNK1104: cannot open file 'libboost_filesystem-vc120-mt-1_57.lib'
libboost_filesystem-vc120-mt-1_57.lib is located in the stage/lib folder, so I don't know what is going on. I am compiling with Visual Studio 2013.
Any thoughts?
Try setting the Boost_USE_STATIC_LIBS and Boost_USE_MULTITHREADED CMake variables to ON before using find_package, i.e.:
set( Boost_USE_STATIC_LIBS ON )
set( Boost_USE_MULTITHREADED ON )
find_package( Boost 1.57.0 COMPONENTS filesystem )
I've come across this problem before and it seems as though, on multithreaded windows systems, the Boost bootstrap installer compiles multithreaded, static libraries by default. However, the CMake FindBoost script (which is used by find_package) searches for single-threaded, dynamic libraries by default.
Since you're using VS compiler I'll say you're working on Windows.
The error refers to the linker, which is failing to find boost libraries, as noticed.
Taking into account that the library exists in the boost path, my solution was to do a file(COPY) for the specific library, as a last resort.
if(WIN32)
set(BOOST_ROOT "C:/boost_1_57_0")
set(BOOST_LIBRARYDIR ${BOOST_ROOT}/stage/lib/)
endif()
find_package(Boost 1.57.0 EXACT REQUIRED system filesystem)
if(Boost_FOUND)
message(STATUS "found boost, Boost_LIBRARIES <" ${Boost_LIBRARIES} ">")
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
else()
message(STATUS "boost not found")
endif()
target_link_libraries(boost_test ${Boost_LIBRARIES})
file(COPY "${Boost_LIBRARY_DIRS}/boost_filesystem-vc120-mt-1_57.dll" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
You may add some log messages to the CMake in order to know the values returned in the
find_package.
Make sure the architecture (x64) matches.
$ cmake -A x64 ..
Use link_directories command before adding executables just like include_directories.
link_directories(${Boost_LIBRARY_DIRS})