cpprestsdk using vcpkg, cmake - can't find include files - c++

I am porting a Windows project to CentOS Linux that uses cpprestsdk. I use vcpkg on Windows and I thought I would use vcpkg (and cmake) to bring in and build the packages and 'expose' the libs and header files to my project. The sequence fails in trying to get package header file 'known' to my source. This is what I did.
$ vcpkg install boost cpprestsdk
$ vcpkg integrate install
Applied user-wide integration for this vcpkg root.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=/vcpkg/scripts/buildsystems/vcpkg.cmake"
$ cd <source>
$ vi CMakeLists.txt
cmake_minimum_required(VERSION 2.8.9)
project(Domain)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -I../ ")
file(GLOB SOURCES "*.cpp")
#Generate the shared library from the sources
add_library(Domain SHARED ${SOURCES})
install(TARGETS Domain DESTINATION ../lib)
$ cmake -DCMAKE_TOOLCHAIN_FILE=/vcpkg/scripts/buildsystems/vcpkg.cmake -G "Unix Makefiles" .
$ make
[ 7%] Building CXX object CMakeFiles/Domain.dir/BaseDataFactory.cpp.o
In file included from /src/Domain/stdafx.h:4:0,
from /src/Domain/BaseDataFactory.cpp:1:
../Common/Common.h:75:26: fatal error: cpprest/json.h: No such file or directory
#include <cpprest/json.h>
By adding the vcpkg toolchain file parameter to cmake, I thought it was supposed to take care of exposing all of the package paths (lib/header) and write them to the output Makefile?
I tried adding
find_package(cpprestsdk REQUIRED)
Then I got bunch of new errors:
CMake Error at CMakeLists.txt:7 (find_package):
Could not find a package configuration file provided by "cpprestsdk" with
any of the following names:
cpprestsdkConfig.cmake
cpprestsdk-config.cmake
cpprestConfig.cmake
cpprest-config.cmake
cpprestsdk-config.cmake does exist under the vcpkg root directory and I can definitely see the offending header file for the package under the vcpkg root directory, but why does the cmake-generated Makefile not have everything it needs to build? Does each and every package under vcpkg have to be manually included in some way in the CMakeLists.txt file?

I found the problem. Cpprestsdk does not register/expose any cmake find_package() config module. If it did that, this wouldn't be an issue - the generated toolchain file would set everything cmake needs in order to generate the paths to include in the MakeFile.
I added the following line to the CMakeList.txt file and cmake was then able to find the config file:
set(cpprestsdk_DIR "/vcpkg/installed/x64-linux/share/cpprestsdk")
Which is really, really bad, IMHO, to have to hard code a path to find cpprestsdk. I still have the header file path problem, so there's actually much more going wrong/missing. I'll update this post once I get word from the vcpkg/cmake teams.

https://github.com/Microsoft/cpprestsdk/blob/ea4eff7cd1d6110833df869f7591f266816f8328/Release/src/CMakeLists.txt#L282-L285
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/cpprestsdk-config.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/${CPPREST_EXPORT_DIR}
)
and
https://github.com/Microsoft/cpprestsdk/blob/9d8f544001cb74544de6dc8c565592f7e2626d6e/Release/CMakeLists.txt#L22
set(CPPREST_EXPORT_DIR cpprestsdk CACHE STRING "Directory to install CMake config files.")
One thing puzzle me (not a vcpkg expert yet)
https://github.com/Microsoft/vcpkg/blob/master/ports/cpprestsdk/portfile.cmake#L45
vcpkg_fixup_cmake_targets(CONFIG_PATH lib/share/cpprestsdk)
while it should be lib/cpprestsdk IMHO (ed: not checked SHA1 version)
#cardinalPilot did you try to locate the config file on your system ?
can you also try to print the CMAKE_PREFIX_PATH etc to see if vcpkg do correctly its stuff...

Related

Cmake error: Target "restinio::restinio" contains relative path in its INTERFACE_INCLUDE_DIRECTORIES:

Preface: I am using Clion with Cygwin. I have installed vcpkg following their instructions. Then I followed restinio instructions to install restinio using vcpkg. Since restinio required fmt and http-parser I installed both of those too.
I have installed both the x86-windows version and x64-windows version of all 3 packages.
I linked vcpkg cmake file as per their instructions in Clion and have regenerated the CMakeCache
Currently I am trying to build specifically the x64-windows version (I was having other errors with the x86 version and I got further with the x64 version).
I have looked at this, and my initial error is different along with there is stuff inside of the directory that is in the relative path.
cmake_minimum_required(VERSION 3.23)
project(testing2)
set(CMAKE_CXX_STANDARD 14)
# RESTinio dependencies:
# 1. ASIO or Boost::ASIO (goes as headers, vcpkg knows where)
# 2. HTTP parser
find_package(unofficial-http-parser CONFIG REQUIRED)
# 3. fmtlib
find_package(fmt CONFIG REQUIRED)
# RESTinio itself
find_package(restinio CONFIG REQUIRED)
# Make your project dependent on restinio,
# and let cmake deal with all the headers paths and linked libs.
add_executable(testing2 main.cpp)
target_link_libraries(testing2 PRIVATE restinio::restinio)
cmake Options: -DCMAKE_TOOLCHAIN_FILE=C:\dev\vcpkg\vcpkg-master\scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET:STRING=x64-windows
Clion output in the cmake tab:
-- Configuring done
CMake Error in CMakeLists.txt:
Target "restinio::restinio" contains relative path in its
INTERFACE_INCLUDE_DIRECTORIES:
"C:/dev/vcpkg/vcpkg-master/installed/x64-windows/include"
CMake Error in CMakeLists.txt:
IMPORTED_LOCATION not set for imported target
"unofficial::http_parser::http_parser" configuration "Debug".
-- Generating done
CMake Error:
Running
'/cygdrive/c/Program Files/JetBrains/CLion 2021.2.3/bin/ninja/cygwin/ninja.exe' '-C' '/cygdrive/c/Users/Tally/Desktop/DevStuffs/Testing2/cmake-build-debug' '-t' 'recompact'
failed with:
ninja: error: build.ninja:35: loading 'CMakeFiles/rules.ninja': No such file or directory
include CMakeFiles/rules.ninja
^ near here
CMake Generate step failed. Build files cannot be regenerated correctly.
[Finished]
the path: "C:/dev/vcpkg/vcpkg-master/installed/x64-windows/include" exists and has asio, fmt, and restinio dirs along with asio.hpp and http_parser.h.
I have tried adding:
target_link_libraries(main PRIVATE unofficial::http_parser::http_parser) as vcpkg suggests when I install, but it gives me the same error just replaced restinio::restinio with unofficial::http_parser::http_parser.

Generate wasm file using emscripten

I want to compile SealPIR library using emscripten to generate a wasm file.
When using this command:
emcmake cmake .
I get this error:
CMake Error at CMakeLists.txt:19 (find_package):
By not providing "FindSEAL.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "SEAL", but
CMake did not find one.
Could not find a package configuration file provided by "SEAL" (requested
version 3.2.0) with any of the following names:
SEALConfig.cmake
seal-config.cmake
Add the installation prefix of "SEAL" to CMAKE_PREFIX_PATH or set
"SEAL_DIR" to a directory containing one of the above files. If "SEAL"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "/home/Zied/webassembly/SealPIR/CMakeFiles/CMakeOutput.log".
emcmake: error: 'cmake . -DCMAKE_TOOLCHAIN_FILE=/home/Zied/webassembly/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR="/home/Zied/webassembly/emsdk/node/14.15.5_64bit/bin/node"' failed (1)
SEAL is correctly installed. when i run the same command without emcmake it works just fine.
This is my CMakeList
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(SealPIR VERSION 2.1 LANGUAGES CXX)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
add_executable(main
main.cpp
)
add_library(sealpir STATIC
pir.cpp
pir_client.cpp
pir_server.cpp
)
find_package(SEAL 3.2.0 EXACT REQUIRED)
target_link_libraries(main sealpir SEAL::seal)
When using a toolchain file for cross compiling, CMake will by default disable system libraries. It won't search into any directory to avoid finding files that is not compatible with the target system.
You think you didn't used a toolchain file? Think again! emcmake hides that from you. Look carefully at the error output.
Here you compiled the SEAL library, but you installed it in the default path, which is /usr/local.
We can tell CMake to explicitly search there, but I wouldn't recommend, but you can try if it works:
emcmake cmake . -CMAKE_PREFIX_PATH=/usr/local
The proper solution would be to create a directory with all the emscripten libraries in it:
# In the SEAL build directory
emcmake cmake .. -DCMAKE_INSTALL_PREFIX=/home/anblic/webassembly/install
Then after installing the libraries in that directory, you can set the prefix path in the same directory as the install path:
# Assuming you're in a build/ subdirectory
emcmake cmake .. -DCMAKE_PREFIX_PATH=/home/anblic/webassembly/install

Clion with CMake can not see header files of OpenGL on MacOS [duplicate]

How can you link GLEW to a project with CMake?
We've been trying to link GLEW to our project using CMake for at least 3 hours without any success so any help is accepted.
I'm using the FindGLEW.cmake which comes with CMake 3.1.0
CMakeLists.txt
find_package(GLEW REQUIRED)
if (GLEW_FOUND)
include_directories($(GLEW_INCLUDE_DIRS))
endif()
Environment Variables
I'm using MinGW w64 to compile the sources and we successfully linked GLFW and GLM just by copying the includes and libs to their respective folders, but after doing the same with GLEW, CMake still couldn't find it.
Sorry if I wasn't clear enough while formulating the question. I will provide any additional information required.
Edit: I've managed to link the header files by specifying their location in the CMake Cache file, though I'm getting undefined reference to glew functions like glewInit().
Typical CMake scripts like FindGLEW will define variables that specify the paths and files that your project needs. If the script can't automatically identify the correct paths (usually because of nonstandard install location, which is fine), then it leaves these variables up to you to fill in.
With command line CMake, you use the -D flag to define and set the value of a given variable. Other CMake interfaces, like CMake-gui or an IDE integration, give you this ability some other way.
However you do it, you can also modify the cache directly (CMakeCache.txt) and see what CMake is using in there or just clear the cache altogether. You'll have to rerun CMake for it to pick up your changes.
When it comes to linking, that's when you need to tell CMake which libs to link. Use the link_libraries command with what the automated script gives you.
find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIRS})
link_libraries(${GLEW_LIBRARIES})
Other answers do obviously work, but the target based style of cmake makes it even easier since the GLEW find module defines the imported target GLEW::GLEW. All you need is:
find_package(GLEW REQUIRED)
target_link_libraries(YourTarget GLEW::GLEW)
YourTarget is the target that you created with add_executable or add_library. No need to explicitly add include directories, they are added automatically by linking the targets.
The secret of find_package(GLEW) is in FindGLEW.cmake file with cmake install.
find_path(GLEW_INCLUDE_DIR GL/glew.h)
find_library(GLEW_LIBRARY NAMES GLEW glew32 glew glew32s PATH_SUFFIXES lib64)
The find_path and find_library commands find paths in standard system paths. If you want them to find paths in user defined directories, you should tell them.
For example:
set(CMAKE_PREFIX_PATH "d:/libs/glew-1.10.0")
set(CMAKE_LIBRARY_PATH "d:/libs/glew-1.10.0/lib/Release/Win32/")
find_package(GLEW REQUIRED)
Reference:
http://www.cmake.org/cmake/help/v3.0/command/find_path.html
http://www.cmake.org/cmake/help/v3.0/command/find_library.html
I was struggling hard to link glew to cmake through command line on mac. This might be helpful but I am not sure :) I will walk you through step by step of what I have done.
I installed Cmake source from the web.
Then I went inside the cmake folder in terminal and typed
./bootstrap && make && make install
(this will install cmake command line tools on our OS platform)
I have some exercise files. I want cmake to generate xcode files for me for all those exercise files (ex. triangles.cpp, shader.cpp etc) So i made a directory inside exercise files folder.
$ mkdir xcode
$ cd xcode
$ cmake -G "Xcode" ..
At this point, Cmake suppose to install all xcode files that included correct libraries. But there was an error :
$ cmake -G "Xcode" ..
CMake Warning (dev) at CMakeLists.txt:3 (cmake_minimum_required):
Compatibility with CMake < 2.4 is not supported by CMake >= 3.0.
This warning is for project developers. Use -Wno-dev to suppress it.
system name is: Darwin-14.1.0
system processor is: x86_64
-- Could NOT find GLEW (missing: GLEW_INCLUDE_DIR GLEW_LIBRARY)
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
-- Using Cocoa for window creation
-- Using NSGL for context creation
-- Building GLFW only for the native architecture
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
GLEW_LIBRARY
linked by target "TextureLoader" in directory /Users/Mydir/Desktop/Exercise/Exercise Files
-- Configuring incomplete, errors occurred!
Then to make sure I have installed GLEW and all its libraries correctly, I ran
$brew install glew
Yes, I have installed glew already but it was NOT linked. See the Warning below:
Warning: glew-1.12.0 already installed, it's just not linked
Then I ran the following commands:
$ brew unlink glew
$ brew link glew
And I have solved the error. So just make sure that you have linked glew. Hope this helps.
Happy Coding :)
Finally I found a simple and short CMakeLists which works if you have installed everything in default paths.(openGL, glfw and glew)
cmake_minimum_required(VERSION 3.3)
project(openGL_tutorial)
find_package(OpenGL REQUIRED)
if(NOT OPENGL_FOUND)
message("ERROR: OpenGL not found")
endif(NOT OPENGL_FOUND)
set(GL_LIBRARY GL GLU X11)
add_executable(openGL_tutorial main.cpp)
target_link_libraries(openGL_tutorial glfw GLEW libGLEW.so libGLU.so libGL.so)
For what it is worth, in 2023, this works for me, on macOS, with GLEW, GLFW, and CMake installed using Homebrew:
cmake_minimum_required(VERSION 3.10)
project(Project)
add_executable(Project main.cpp)
find_package(glfw3 REQUIRED)
find_package(GLEW REQUIRED)
target_link_libraries(Project glfw GLEW::glew)

How can I solve undefined reference errors with Conan and SDL2 on Ubuntu 16.10?

I am trying to get conan working in a test project by following their Geting Started guide so that I can begin using it in a real project. I am attempting to use SDL2 with glew in a C++ project compiled with g++ on Ubuntu 16.10 x64.
My conanfile.txt looks like this:
[requires]
SDL2/2.0.5#lasote/stable
glew/2.0.0#coding3d/stable
[generators]
cmake
My CMakeLists.txt file looks like this:
project(conantest)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
include(conan/conanbuildinfo.cmake)
conan_basic_setup()
set(SRC_FILES main.cpp Display.cpp)
add_executable(conantest ${SRC_FILES})
My project consists of a directory containing CMakeLists.txt, conanfile.txt, all of my source code, a build directory for Cmake, a conan directory for Conan, and a rebuild.sh. I am using this rebuild.sh script to clean and build the project whenever I make a change to the build environment (the rm -r's, although suboptimal once it is working are there to ensure any errors from the previous setup are removed once I attempt a fix).
My rebuild.sh looks like this:
rm -r ./build/*
rm -r ./conan/*
cd conan
conan install ..
cd ../build
cmake ..
make
When I run this script, everything seems to be working until the
final executable is linked. When this happens, I get Display.cpp:(.text+0x8a): undefined reference to `SDL_Init'. To solve this, I have tried the solutions here and here. Although these are not specific to SDL, I was unable to find any resources that are.
Here is a zip file of the full environment that I am using, including a MCVE.
You need to tell CMake to link against the libraries yourself, since conanbuildinfo.cmake cannot know the name of your target.
Simply adding this line to the bottom of your CMakeLists.txt file will work:
conan_target_link_libraries(conantest)
This command will call target_link_libraries for all dependencies specified in the conanfile.
Here's another CMakeLists.txt example if you would like to use a more "modern" CMake approach which focuses more on targets instead of global settings:
cmake_minimum_required(VERSION 3.5)
project(conantest)
include(conan/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
add_executable(conantest main.cpp Display.cpp)
set_property(TARGET conantest PROPERTY CXX_STANDARD 11)
target_link_libraries(conantest CONAN_PKG::SDL2 CONAN_PKG::glew)

Compile issues: LIBUSB_1 with cmake project on Windows

First Attempt
In my cmake/c++ project I get the following error when compiling:
C:\local\projects\synergy-usb\synergy-through-usb-master>cmake .
You have called ADD_LIBRARY for library cryptopp without any source files. This typically indicates a problem with your CMakeLists.txt file
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
LIBUSB_1_INCLUDE_DIR
used as include directory in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/arch
used as include directory in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/net
LIBUSB_1_LIBRARY
linked by target "arch" in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/arch
-- Configuring incomplete, errors occurred!
See also "C:/local/projects/synergy-usb/synergy-through-usb-master/CMakeFiles/CMakeOutput.log".
So I am missing libusb libraries.
Second Attempt
So now I have found a version of libusb_1 (libusbx-1.0.18-win) which includes the folders:
examples
include
MinGW32
MinGW64
MS32
MS64
I copied MS64/dll (windows 64-bit) contents and put it here:
C:\local\libs\libusbx
I added this path to my PATH variable and then tried to run cmake again:
C:>cd local\projects\synergy-usb\synergy-through-usb-master
C:\local\projects\synergy-usb\synergy-through-usb-master>cmake .
You have called ADD_LIBRARY for library cryptopp without any source files. This typically indicates a problem with your CMakeLists.txt file
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
LIBUSB_1_LIBRARY
linked by target "arch" in directory C:/local/projects/synergy-usb/synergy-through-usb-master/src/lib/arch
-- Configuring incomplete, errors occurred!
See also "C:/local/projects/synergy-usb/synergy-through-usb-master/CMakeFiles/CMakeOutput.log".
Now I am stuck again. I have no idea what it wants me to do. It knows where libusb is...
Edit
Here is the contects (part of) the CMakeLists.txt that has libusb in it:
find_package(libusb-1.0 REQUIRED)
set(inc
.
../base
../common
../mt
../platform
../synergy
${LIBUSB_1_INCLUDE_DIRS}
)
if (UNIX)
list(APPEND inc
../../..
../arch
)
endif()
include_directories(${inc})
add_library(arch STATIC ${src})
set(libs
../lib
${LIBUSB_1_LIBRARIES}
)
if (WIN32)
if (GAME_DEVICE_SUPPORT)
list(APPEND libs synxinhk)
endif()
endif()
target_link_libraries(arch ${libs})
Edit 2
I added the following lines into the findlibusb-1.0.cmake file:
set(LIBUSB_1_LIBRARY C:\\local\\libs\\libusbx\\libusb-1.0.dll)
message(STATUS "***********************************> LIBUSB_1_LIBRARY: ${LIBUSB_1_LIBRARY}")
Here is the output from that:
C:\local\projects\synergy-usb\synergy-through-usb-master>cmake .
-- ***********************************> LIBUSB_1_LIBRARY: C:\local\libs\libusbx\libusb-1.0.dll
-- Found libusb-1.0:
-- - Includes: C:/local/libs/libusbx
-- - Libraries: C:\local\libs\libusbx\libusb-1.0.dll
You have called ADD_LIBRARY for library cryptopp without any source files. This typically indicates a problem with your CMakeLists.txt file
-- Configuring done
CMake Error: CMake can not determine linker language for target: cryptopp
CMake Error: CMake can not determine linker language for target: cryptopp
CMake Error: CMake can not determine linker language for target: cryptopp
CMake Error: CMake can not determine linker language for target: cryptopp
-- Generating done
-- Build files have been written to: C:/local/projects/synergy-usb/synergy-through-usb-master
Still does not build, but this particular library seems to be added ok now :)
synergy had such approach to keep some 3rd party libs compressed in zip in ext subdirectory. synergy-through-usb though kept cmake way to specify include through -DLIBUSB_1_INCLUDE_DIR=... -DLIBUSB_1_LIBRARY=... for Windows.
So you just need go to 'ext' directory and uncompress its zip archives with libraries gtest, gmock and crypto.