I'm trying to run some code with boost, but i can't include any boost file, like "boost/timer/timer.hpp". My CMakeLists contains
cmake_minimum_required(VERSION 3.10)
project(Converter)
find_package(Boost)
include_directories(${BOOST_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARIES})
add_executable(Converter converter.cpp)
TARGET_LINK_LIBRARIES(Converter ${Boost_LIBRARIES})
message("boost lib: ${Boost_LIBRARY_DIRS}, inc: ${Boost_INCLUDE_DIR}")
CMake answer
My cpp file contains
#include <iostream>
#include <boost/timer/timer.hpp>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
And when i am trying to build it, there is a error: "Cannot open include file 'boost/timer/timer.hpp'"
You are using a wrong non existing variable here. To set the include Boost directories to your project you need to use Boost_INCLUDE_DIRS, the case of the variable matters. And your link directories should be set to Boost_LIBRARY_DIRS.
cmake_minimum_required(VERSION 3.10)
project(Converter)
find_package(Boost COMPONENTS timer)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
add_executable(Converter converter.cpp)
target_link_libraries(Converter PUBLIC ${Boost_LIBRARIES})
message("boost lib: ${Boost_LIBRARY_DIRS}, inc: ${Boost_INCLUDE_DIR}")
Your small project can be further simplified by using the imported targets Boost::headers as follows:
cmake_minimum_required(VERSION 3.10)
project(Converter)
find_package(Boost COMPONENTS timer REQUIRED)
add_executable(Converter converter.cpp)
target_link_libraries(Converter PUBLIC Boost::headers Boost::timer)
message("boost lib: ${Boost_LIBRARY_DIRS}, inc: ${Boost_INCLUDE_DIRS}")
Related
I am confused on how to statically include the source code of SDL2. I am trying to do this to make a library I am working on more portable.
When I was setting this up as executable the library was compiled with it fine, but when I changed it to a library it wouldn't include the library.
Currently, when I try to include my library in another project it says "Cannot open include file: 'SDL2/SDL.h': No such file or directory". So it leads me to think that the cause of the error is that the include directories aren't exported with the static library.
My Filesystem:
include
--Header Files
src
--Source Files
extern
--SDL2
build
Here is an example of the file causing the error:
#include <iostream>
#include <SDL.h> //Error
using namespace std;
/* The code */
Here is an example of my main CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
project(MyProject VERSION 1.0.0)
set(SDL2_SOURCE_DIR “${CMAKE_SOURCE_DIR}/extern/SDL2”)
add_subdirectory(extern/SDL2)
add_subdirectory(src)
Here is an example of my src CMakeLists.txt:
set(PROJECT_NAME MyProject)
file(GLOB HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB SOURCES "*.cpp")
add_library(${PROJECT_NAME} ${SOURCES} ${HEADER_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/include" PUBLIC "${CMAKE_SOURCE_DIR}/extern/SDL2/include")
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2main SDL2-static)
set_target_properties( ${PROJECT_NAME}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin"
)
From sdl2 CMakeLists.txt try:
set(SDL_STATIC 1)
add_subdirectory(extern/SDL2) # And I recommend EXCLUDE_FROM_ALL
Also to be sure add a check:
foreach(i IN ITEMS SDL2main SDL2-static)
if(NOT TARGET ${i})
message(FATAL_ERROR "${i} is not a target")
endif()
endif()
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2main SDL2-static)
I have downloaded the HDFql library and put the whole lot in /usr/local/ in my linux system. I now wish to use this library in a ROS application, so I have tried to link it in my CMakeList, which looks as following:
cmake_minimum_required(VERSION 3.2)
project(data_generator)
add_compile_options(-std=c++11)
set(CMAKE_BUILD_TYPE Release) # Release, RelWithDebInfo
set(CMAKE_CXX_FLAGS "-std=c++11 -pthread ${CMAKE_CXX_FLAGS}")
find_package(catkin_simple REQUIRED)
catkin_simple(ALL_DEPS_REQUIRED)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
set(HDFQL_ROOT "/usr/local/hdfql-2.1.0")
include_directories(${HDFQL_ROOT}/include)
find_library(HDFQL_LIB HDFql)
if (HDFQL_LIB)
message("Library(HDFQL_LIB) found in ${HDFQL_LIB}")
else()
message(FATAL_ERROR "Library (HDFQL_LIB) not found")
endif()
cs_add_executable(
${PROJECT_NAME}
src/main.cpp
src/data_create.cpp
src/event_definitions.cpp
)
target_include_directories(${PROJECT_NAME}
PUBLIC "${HDFQL_ROOT}/include"
)
target_link_libraries(${PROJECT_NAME}
${OpenCV_LIBRARIES}
${HDFQL_LIB}
)
# CMake Indexing
FILE(GLOB_RECURSE LibFiles "include/*")
add_custom_target(headers SOURCES ${LibFiles})
cs_install()
cs_export()
When I build this, it works fine and outputs the message:
Library(HDFQL_LIB) found in /usr/local/hdfql-2.1.0/lib/libHDFql.so
with the following in my main.cpp file:
#include <HDFql.hpp>
....
std::cout <<"HDFql version: " << HDFql::Version << std::endl;
the output is: HDFql version: 2.1.0.
However, as soon as I try to actually use functions of the library - for example:
#include <HDFql.hpp>
....
std::cout <<"HDFql version: " << HDFql::Version << std::endl;
HDFql::execute("CREATE FILE /home/user/test.h5");
I get the error:
main.cpp:(.text+0x1858): undefined reference to `HDFql::execute(char const*)'
This suggests to me that while CMake has no issue with the includes, it is having trouble linking the actual library (ie including the libHDFql.a/libHDFql.so files). Can anyone tell me what I'm doing wrong here?
Many thanks!
The problem was that I needed to include the library /usr/local/hdfql-2.1.0/wrapper/cpp/libHDFql.so, where I was using /usr/local/hdfql-2.1.0/lib/libHDFql.so. It's pretty maddening, since the reference manual doesn't make any mention of this and I spent way too long figuring this out. Ohwell, I hope this helps anyone else with this problem.
For reference, here is a minimal catkin-style CMakeLists that will work:
cmake_minimum_required(VERSION 3.2)
project(project_name)
add_compile_options(-std=c++11)
find_package(catkin_simple REQUIRED)
catkin_simple(ALL_DEPS_REQUIRED)
set(HDFQL_ROOT "path/to/hdfql-2.1.0")
include_directories(${HDFQL_ROOT}/include)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "-std=c++11 -pthread ${CMAKE_CXX_FLAGS}")
cs_add_executable(
${PROJECT_NAME}
# your source file 1
# your source file 2
# ...
)
target_include_directories(${PROJECT_NAME}
PUBLIC "${HDFQL_ROOT}/include"
)
target_link_libraries(
${PROJECT_NAME}
${OpenCV_LIBRARIES}
)
target_link_libraries(
${PROJECT_NAME}
"${HDFQL_ROOT}/wrapper/cpp/libHDFql.so"
)
Of course the absolute paths aren't very pretty, the alternative is to add /usr/local/hdfql-2.1.0/lib to the environment variable CMAKE_PREFIX_PATH (eg export CMAKE_PREFIX_PATH="/usr/local/hdfql-2.1.0/lib:$CMAKE_PREFIX_PATH").
I am trying to use uint512_t in a boost library located in:
multiprecision/cpp_int.hpp
however, when I try to include my boost library through CMake:
cmake_minimum_required(VERSION 3.6)
project(BoostTest)
set(CMAKE_CXX_STANDARD 11)
set(BOOSTROOT "/usr/local/Cellar/boost/1.63.0/include")
find_package(Boost REQUIRED)
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
endif()
include_directories(${Boost_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
add_executable(BoostTest ${SOURCE_FILES})
I receive the following error message when compiling:
error: unknown type name 'uint512_t'
I don't know what the problem is. I even included:
"boost/multiprecision/cpp_int.hpp"
Keeping everything else the same in main.cpp, all that was needed was:
using namespace boost::multiprecision
I try to use the SimpleAmqpClient library to build my multi-agent environment for simulation. I have installed the library after cloning its sources, making them:
make
sudo make install
After that, I created the
main.cpp
file:
#include <iostream>
#include <SimpleAmqpClient/SimpleAmqpClient.h>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
just to try it out.
Also, I have the following
CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(SampleProject)
include_directories('/usr/local/include/')
find_package(libSimpleAmqpClient REQUIRED)
include_directories(${libSimpleAmqpClient++_INCLUDE_DIRS})
set(LIBS ${LIBS} ${libSimpleAmqpClient++_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(SampleProject ${SOURCE_FILES})
So, the question is: how to find and link this library.
You need to include the line
target_link_libraries(SampleProject ${LIBS})
after the line
add_executable(SampleProject ${SOURCE_FILES})
This imbues the target SampleProject with properties that tell the generator to generate a linker command which reference the libraries you need.
I have found the solution, thanks to #RichardHodges. The solution was to use
find_library() instead of find_package().
THe final file is the following:
CMakeLists.txt
cmake_minimum_required(VERSION 3.3)
project(SampleProject)
include_directories('/usr/local/include/')
find_library(libSimpleAmqpClient REQUIRED)
include_directories(${libSimpleAmqpClient++_INCLUDE_DIRS})
set(LIBS ${LIBS} ${libSimpleAmqpClient++_LIBRARIES})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(SampleProject ${SOURCE_FILES})
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)