Undefined reference with boost asio ssl [duplicate] - c++

I have a question for people who work with CMakeList.txt in C++. I want to use Podofo project (a project to parse & create pdf).
So my main function is simple as:
#include <iostream>
#include <podofo/podofo.h>
int main() {
PoDoFo::PdfMemDocument pdf;
pdf.Load("/Users/user/path/to.pdf");
int nbOfPage = pdf.GetPageCount();
std::cout << "Our pdf have " << nbOfPage << " pages." << std::endl;
return 0;
}
My CMakeList.txt is:
cmake_minimum_required(VERSION 3.7)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
add_executable(untitled ${SOURCE_FILES})
But I am stuck with this error:
/usr/local/include/podofo/base/PdfEncrypt.h:44:10: fatal error: 'openssl/opensslconf.h' file not found
#include <openssl/opensslconf.h
I tried to include with find_package, find_library .. setting some variables but I do not find the way.
My env is:
macOS
Clion
Podofo installed via home-brew in /usr/local/podofo
OpenSSL installed via home-brew in /usr/local/opt/openssl
Thanks by advance community !!

find_package is the correct approach; you find details about it here.
In your case, you should add these lines:
find_package(OpenSSL REQUIRED)
target_link_libraries(untitled OpenSSL::SSL)
If CMake doesn't find OpenSSL directly, you should set the CMake variable OPENSSL_ROOT_DIR.

Related

cmake cannot find libsndfile

Cmake seems not to find the lib libsndfile. However, it installed on my machine.
find_library(sndfile REQUIRED)
And installed :
yum list installed | grep libsnd
libsndfile.x86_64 1.0.25-11.el7 #base
libsndfile-devel.x86_64 1.0.25-11.el7 #base
The error :
CMake Error at CMakeLists.txt:65 (find_library):
Could not find sndfile using the following names:
CMakeLists.txt :
cmake_minimum_required(VERSION 3.19)
project(untitled1)
set(CMAKE_CXX_STANDARD 11)
find_library(sndfile REQUIRED)
add_executable(untitled1 main.cpp)
Main.cpp
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
It doesn't mean much if it is installed on your machine or not. CMake won't search all your computer for the library. The best thing you can do is to add the location of the installed library to your PATH environment variable.
Here's what CMake documentation says about find_library() HINTS and PATHS arguments:
HINTS, PATHS
Specify directories to search in addition to the default
locations. The ENV var sub-option reads paths from a system
environment variable.
CMake also provides another solution: set CMAKE_PREFIX_PATH
You can find more details about it here.

Undefined reference errors in simple boost serialization

I have a minimal example of Boost serialization where I try to save an integer in a binary archive file
Here is main.cpp:
#include <iostream>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
int main() {
int t = 0;
std::ofstream file("Test.bin");
boost::archive::binary_oarchive archive(file);
archive << t;
file.close();
return 0;
}
and here is the CMake file:
cmake_minimum_required(VERSION 3.15)
project(Test)
set(CMAKE_CXX_STANDARD 17)
find_package(Boost REQUIRED serialization)
add_executable(Test main.cpp)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(Test ${Boost_LIBRARIES})
endif()
When I try to run this program in CLion, I get a large list of undefined reference errors as shown here:
https://pastebin.com/8uX9MZFf
I have setup Boost using vcpkg package manager. I'm compiling using Mingw-w64. The CMake file loads without errors (only a warning that says "New Boost version may have incorrect or missing dependencies and imported targets," though I've heard this warning isn't of concern, as it just means the current version of CMake isn't aware of the newest version of Boost).
I've tried to look for solutions to this everywhere, but I can't seem to find anything that works here. Any help would be appreciated.
I'm using cmake 3.15.3, boost 1.73.0 and mingw-w64 6.0.
EDIT
I uninstalled and reinstalled Boost without using the package manager, and tried getting the serialization library again. In this context, CMake runs into errors saying it can't find Boost with serialization (Though it can find Boost alone). I set Boost_DEBUG to ON and looked at the output, and noticed the following things:
_boost_COMPILER = "-mgw81" (guessed)
CMake seems to guess that the compiler I used to compile boost was mgw81. I'm guessing it got the 8.1 from my gcc version, which is correct.
Searching for SERIALIZATION_LIBRARY_RELEASE: boost_serialization-mgw81-mt-x64-1_73;boost_serialization-mgw81-mt-x64;...
As a result of that compiler selection, it searches for a file with "-mgw81" in the name. The problem is that the library files generated when I built boost are named like so:
libboost_serialization-mgw8-mt-x64-1_73.a
This says "-mgw8" instead of "-mgw81". I don't know how to correct CMake or build boost in such a way that this conflict doesn't happen. I've tried rebuilding boost with toolset=gcc-8.1 instead of toolset=gcc, but I still get "-mgw8" in the library file names.
EDIT 2
I found the solution to the above issue. I've posted it below.
After realizing that the issue was what I mentioned in EDIT, I looked further into how that issue could be resolved, and I found out you can manually set the compiler that is used to search through the variable Boost_COMPILER.
I changed my CMake file to the following:
cmake_minimum_required(VERSION 3.15)
project(Test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:/boost_1_73_0")
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:/boost_1_73_0/libs")
set(Boost_DEBUG ON)
set(Boost_COMPILER -mgw8)
set(Boost_ARCHITECTURE -x64)
set(BOOST_ROOT C:/boost)
set(BOOST_INCLUDEDIR C:/boost/include/boost-1_73/boost)
set(BOOST_LIBRARYDIR C:/boost/lib)
set(BOOST_NO_SYSTEM_PATHS ON)
find_package(Boost REQUIRED serialization)
add_executable(Test main.cpp)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(Test ${Boost_LIBRARIES})
endif()
I believe the critical changes here were setting Boost_COMPILER and Boost_ARCHITECTURE. I realized Boost_ARCHITECTURE needed to be set from this question: Linking boost in CLion project.
With this CMake file, my main.cpp file from above ran properly.

How can I find installed Boost library on ubuntu using CMake?

I've installed Boost using this command
sudo apt-get install libboost-all-dev
and I wrote this simple example in main.cpp
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello, world!" << std::endl;
return 0;
}
And in my CMakeLists.txt I have this:
cmake_minimum_required(VERSION 2.8)
find_package(Boost REQUIRED)
if(NOT Boost_FOUND)
message(SEND_ERROR "Failed to find Boost")
return()
else()
include_directories(${Boost_INCLUDE_DIR})
endif()
add_executable(main main.cpp)
CMake worked correctly, but after launching with make I got a few errors:
main.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()'
How to correctly include boost in my CMakeLists.txt so that cmake will find libraries ?
You need to link against the boost libraries. FindBoost provides the variable Boost_LIBRARIES for this:
add_executable(main main.cpp)
target_link_libraries(main ${Boost_LIBRARIES})
For more information, see the FindBoost documentation. There's an example near the end.
main.cpp:(.text+0x11f): undefined reference to `boost::system::generic_category()'
It's failing at the link step. You're not linking to the system library. You need to do that.
You're not running into any error with regard to CMake making use of boost. You just need to tell it that system needs to be linked in.
To add on to the previous answers, here is the list of Boost librairies you need to link. (as of Boost 1.65)
All other boost libraires can be used by simply including the header.

CMake is not including boost directories into it's generated projects

I have been trying to create a CMake project and then compiling it with Visual Studio 2015. When I Generate the Project Files, however, boost isn't included. Here is the relevant output from CMake upon generation:
Boost version: 1.62.0
Found the following Boost libraries:
system
thread
chrono
date_time
atomic
Configuring done
Generating done
And the paths are all correct. Where should CMake put the include directories, into VC++ Directories?
Where could the build system be going wrong?
The actual CMakeLists.txt is as follows:
#MultiTracker Application
cmake_minimum_required (VERSION 3.1)
project(MultiTracker)
#Additional CMake search modules
#Require C++11
set (CMAKE_CXX_STANDARD 11)
message(STATUS "Generating Makefile for MultiTracker")
file(GLOB SRC_FILES *.cpp)
#Find and link boost
SET(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED system thread)
add_executable(MultiTracker ${SRC_FILES})
#Link internal libraries
#Link 3rd party libraries
target_link_libraries(MultiTracker ${Boost_LIBRARIES})
#The native OS thread library
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(MultiTracker Threads::Threads)
Main.cpp
//System Includes
#include <iostream>
#include <cstdlib>
//Library Includes
#include <boost/program_options.hpp>
#include <boost/format.hpp>
//Local Includes
int main(int iArgc, char *cpArgv[])
{
std::string confName = "Conf.json", outFileName, inFileName;
//setup the program options
boost::program_options::options_description oPODescriptions("Available options");
oPODescriptions.add_options()
("help", "help message")
("confFile", boost::program_options::value<std::string>(&confName)->default_value("pclConf.json"), "Name of the configuration file to use");
boost::program_options::variables_map mapVars;
try
{
boost::program_options::store(boost::program_options::parse_command_line(iArgc, cpArgv, oPODescriptions), mapVars);
boost::program_options::notify(mapVars);
}
catch (std::exception &e)
{
std::cerr << e.what() << std::endl;
return 2;
}
//print the help message
if (mapVars.count("help"))
{
std::cout << "Stack Overflow Test: " << oPODescriptions << std::endl;
return ~0;
}
std::cout << "Press enter to exit" << std::endl;
std::cin.get();
}
Thanks!
You should also include in your question HOW did you managed the Boost dependency in your CMakeLists.txt file.
If you used find_package(Boost) (see reference here),
you should add Boost_INCLUDE_DIRS to your target project include directories,
and Boost_LIBRARIES to the libraries linked by your project.
See a very similar question and answer here: How do you add boost libraries in CMakeLists.txt
Added after question has been edited
you are missing:
target_include_directories(MultiTracker PRIVATE ${Boost_INCLUDE_DIRS})
(not sure about "PRIVATE" in your case)

Error on building a Wt project. Cannot open include file: 'boost/any.hpp'

Wt v. 3.2.2 and boost libraries v. 1.47 had succesfully installed in my computer and no errors occured in the installation process. Some simple Wt and Boost examples were compiled and ran correctly in the testing process. I use CMake, configured for MSVC 2008, to create the build files for my own Wt projects.
However, when I try to build my own project, I get this error (Cannot open include file: 'boost/any.hpp'). As I saw, boost/any.hpp is included in Wt/WApplication header file.
For further help, my CMakeLists.txt files contents are:
CMakeLists.txt placed on project directory:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(WT_EXAMPLE)
SET (WT_CONNECTOR "wthttp" CACHE STRING "Connector used (wthttp or wtfcgi)")
ADD_SUBDIRECTORY(source)
CMakeLists.txt placed on source directory:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(WT_INSTALL_DIR "C:/Program Files/WT/boost_1_47")
SET(BOOST_INSTALL_DIR "C:/Program Files/boost")
ADD_EXECUTABLE(
GOP.wt
Main.C
)
SET(WT_LIBS
optimized wthttp debug wthttpd
optimized wt debug wtd)
SET(BOOST_LIBS
boost_signals boost_regex boost_thread boost_filesystem boost_system
boost_random boost_date_time boost_program_options)
TARGET_LINK_LIBRARIES (
GOP.wt
${WT_LIBS} ${BOOST_LIBS} ${SYSTEM_LIBS}
)
LINK_DIRECTORIES (
${WT_INSTALL_DIR}/lib/
${BOOST_INSTALL_DIR}/lib/
)
INCLUDE_DIRECTORIES(${WT_INSTALL_DIR}/include)
INCLUDE_DIRECTORIES(${BOOST_INSTALL_DIR}/include)
As I saw in the CMakeCache.txt placed on Wt build directory, paths to boost libraries were found, but ...what about this line?
//The directory containing a CMake configuration file for Boost.
Boost_DIR:PATH=Boost_DIR-NOTFOUND
I asked this question on Wt support forum but I didn't get an answer for about 24 hours...
Update: I found that any.hpp is placed on C:\Program Files\boost\boost_1_47\boost\spirit\home\support\algorithm\any.hpp. So, i suspect that there's a concept with the path that searches any.hpp (it's not directly included in boost directory).
Problem solved. I did a new Wt build, I placed and rebuild boost in a new path and I wrote CMakeLists for this project with more caution.
To include <boost/any.hpp>
For example
#include <boost/any.hpp>
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<boost::any> some_values;
some_values.push_back(10);
const char* c_str = "Hello there!";
some_values.push_back(c_str);
some_values.push_back(std::string("Wow!"));
std::string& s = boost::any_cast<std::string&>(some_values.back());
s += " That is great!\n";
std::cout << s;
}
we only need a simple cmake file like
# Defines AppBase library target.
project(recipe_01)
cmake_minimum_required(VERSION 3.5)
include(GNUInstallDirs)
set(CMAKE_EXPORT_COMPILE_COMMANDS "ON")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
if(CMAKE_CXX_STANDARD EQUAL 98 OR CMAKE_CXX_STANDARD LESS 14)
message(FATAL_ERROR "app requires c++14 or newer")
elseif(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()
find_package(Boost 1.60 REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main)