How to link opengl library on CMake using keyword signature - c++

I am previously using Visual Studio with NuGet for all package. Now I change to CMake.
Now I am using vcpkg to manage library.
However, I need OpenGl
The command of Cmake to link freeglut, glew, glm, libpng, zlib was provide by vcpkg. But not OpenGL.
cmake_minimum_required(VERSION 3.0)
project(little_plane)
set(CMAKE_CXX_STANDARD 14)
add_executable(little_plane main.cpp)
# ./vcpkg install freeglut
find_package(GLUT REQUIRED)
target_link_libraries(little_plane PRIVATE GLUT::GLUT)
## ./vcpkg install glew
#find_package(GLEW REQUIRED)
#target_link_libraries(little_plane PRIVATE GLEW::GLEW)
#
# glm
find_package(glm CONFIG REQUIRED)
target_link_libraries(little_plane PRIVATE glm)
# ./vcpkg install libpng
find_package(PNG REQUIRED)
target_link_libraries(little_plane PRIVATE PNG::PNG)
##
find_package(ZLIB REQUIRED)
target_link_libraries(little_plane PRIVATE ZLIB::ZLIB)
find_package(OpenGL REQUIRED)
if (OPENGL_FOUND)
message("opengl found")
message("include dir: ${OPENGL_INCLUDE_DIR}")
message("link libraries: ${OPENGL_gl_LIBRARY}")
else (OPENGL_FOUND)
message("opengl not found")
endif()
target_link_libraries(little_plane ${OPENGL_gl_LIBRARY})
find_package(glfw3 CONFIG REQUIRED)
target_link_libraries(little_plane PRIVATE glfw)
With the CMakeLists.txt above, I run cmake .
opengl found
include dir: /usr/include
link libraries: /usr/lib/x86_64-linux-gnu/libGL.so
CMake Error at CMakeLists.txt:40 (target_link_libraries):
The keyword signature for target_link_libraries has already been used with
the target "little_plane". All uses of target_link_libraries with a target
must be either all-keyword or all-plain.
The uses of the keyword signature are here:
* CMakeLists.txt:10 (target_link_libraries)
* CMakeLists.txt:20 (target_link_libraries)
* CMakeLists.txt:24 (target_link_libraries)
* CMakeLists.txt:28 (target_link_libraries)
CMake Error at CMakeLists.txt:44 (target_link_libraries):
The plain signature for target_link_libraries has already been used with
the target "little_plane". All uses of target_link_libraries with a target
must be either all-keyword or all-plain.
The uses of the plain signature are here:
* CMakeLists.txt:40 (target_link_libraries)
-- Configuring incomplete, errors occurred!
Which mean opengl is installed on my system. I just don't know how to use target_link_libraries to link with my project.
Provide answer that can copy and paste into CMakeLists.txt if possible.

All your previous target_link_libraries contain a transitivity keyword (PRIVATE in all cases), but you have not provided any when linking OpenGL. So just add that too:
target_link_libraries(little_plane PRIVATE ${OPENGL_gl_LIBRARY})

Related

CMake Errors including IXWebSocket in Ubuntu 22 (works on MacOS 12.6)

Context:
I have a cpp program built on MacOS 12.6 with the following CMakeLists.txt file.
cmake_minimum_required(VERSION 3.19.0)
project(cpp-test VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(cpp-test main.cpp)
add_library(test-helpers main.cpp ${PROJECT_SOURCE_DIR}/helpers.hpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
# this is super important in order for cmake to include the vcpkg search/lib paths!
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
# find library and its headers
find_path(IXWEBSOCKET_INCLUDE_DIR ixwebsocket/IXWebSocket.h)
find_library(IXWEBSOCKET_LIBRARY ixwebsocket)
find_package(OpenSSL REQUIRED)
find_package(CURL REQUIRED)
# include headers
include_directories(${IXWEBSOCKET_INCLUDE_DIR} ${CURL_INCLUDE_DIR})
# Cmake will automatically fail the generation if the lib was not found, i.e is set to NOTFOUND
target_link_libraries(
${PROJECT_NAME} PRIVATE
${IXWEBSOCKET_LIBRARY}
OpenSSL::SSL
OpenSSL::Crypto
${CURL_LIBRARIES}
"-framework Foundation"
"-framework Security"
"-lz"
)
This compiles just fine. However, when I try to pull it into my Ubuntu VM and try to build it /build> cmake .., I get the following errors
CMake Error in CMakeLists.txt:
Found relative path while evaluating include directories of "cpp-test":
"IXWEBSOCKET_INCLUDE_DIR-NOTFOUND"
CMake Error in CMakeLists.txt:
Found relative path while evaluating include directories of
"test-helpers":
"IXWEBSOCKET_INCLUDE_DIR-NOTFOUND"
-- Generating done
What I have tried...
I have installed vcpkg and created my symlink ln -s /path/to/vcpkg /usr/local/bin/vcpkg.
I have installed ixwebsocket via vcpkg install ixwebsocket, but it seems that the CMAKE_TOOLCHAIN_FILE is not being parsed correctly.
I'm a bit lost, any help would be appreciated
This is not a great answer to the issue, but I ended up resolving it by building ixwebsocket via CMake instead.
It seems that vcpkg was not compatible with the linux distro in my VM.

Unable to find Boost libraries with CMake and vcpkg

I have installed boost-variant2 library using the vcpkg command:
vcpkg install boost-variant2:x64-windows
When vcpkg finished the installation, it prompted this:
The package boost is compatible with built-in CMake targets:
find_package(Boost REQUIRED [COMPONENTS <libs>...])
target_link_libraries(main PRIVATE Boost::boost Boost::<lib1> Boost::<lib2> ...)
so in my CMakeLists.txt I added the following lines:
find_package(Boost COMPONENTS variant2 REQUIRED)
target_link_libraries(MyTarget PRIVATE Boost::variant2)
However, when I run cmake -DCMAKE_TOOLCHAIN_FILE:STRING=/path_to_vcpkg/scripts/buildsystems/vcpkg.cmake I get the following error:
-- Configuring incomplete, errors occurred!
Could NOT find Boost (missing: variant2) (found version "1.78.0")
Looks like variant2 is header-only lib and you can just use Cmake file like this:
cmake_minimum_required(VERSION 3.5)
project(project LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Boost)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(project main.cpp)
U can see list of libs required to be built for here for Windows and here for Unix-like systems

Cannot link sndfile library to cmake project (MacOS)

Currently I'm trying to make some spectogram generation for my uni project. I'm trying to build a static library where all the magic will work and just call it from the main() function.
This is my cmake file:
set(CMAKE_CXX_STANDARD 17)
project(demo)
find_package(SndFile REQUIRED)
add_subdirectory(spectogram)
add_executable(demo main.cpp)
target_link_libraries (demo LINK_PUBLIC Spectrogram)
target_link_libraries(demo PRIVATE SndFile::sndfile)
I have installed libsndfile via homebrew, but find_package() refuses to locate the lib and throws this error:
CMake Error at CMakeLists.txt:6 (find_package):
By not providing "FindSndFile.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "SndFile", but
CMake did not find one.
Could not find a package configuration file provided by "SndFile" with any
of the following names:
SndFileConfig.cmake
sndfile-config.cmake
Add the installation prefix of "SndFile" to CMAKE_PREFIX_PATH or set
"SndFile_DIR" to a directory containing one of the above files. If
"SndFile" provides a separate development package or SDK, be sure it has
been installed.
-- Configuring incomplete, errors occurred!
I`ve made some research and found out that libsndfile does not have any .cmake configs inside like other libs, that I can link easily (like OpenCV or spdlog). I would really appreciate any help to solve this horror.
With help of Tsyvarev, I figured out the solution. I used the pkg-config module and a custom cmake file, I found on the web. I will include my final cmake in case someone else will need it:
cmake_minimum_required(VERSION 3.17)
set(CMAKE_CXX_STANDARD 17)
project(demo)
# - Try to find libsndfile
# Once done, this will define
#
# LIBSNDFILE_FOUND - system has libsndfile
# LIBSNDFILE_INCLUDE_DIRS - the libsndfile include directories
# LIBSNDFILE_LIBRARIES - link these to use libsndfile
# Use pkg-config to get hints about paths
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(LIBSNDFILE_PKGCONF sndfile)
endif(PKG_CONFIG_FOUND)
# Include dir
find_path(LIBSNDFILE_INCLUDE_DIR
NAMES sndfile.h
PATHS ${LIBSNDFILE_PKGCONF_INCLUDE_DIRS}
)
# Library
find_library(LIBSNDFILE_LIBRARY
NAMES sndfile libsndfile-1
PATHS ${LIBSNDFILE_PKGCONF_LIBRARY_DIRS}
)
find_package(PackageHandleStandardArgs)
find_package_handle_standard_args(LibSndFile DEFAULT_MSG LIBSNDFILE_LIBRARY LIBSNDFILE_INCLUDE_DIR)
if(LIBSNDFILE_FOUND)
set(LIBSNDFILE_LIBRARIES ${LIBSNDFILE_LIBRARY})
set(LIBSNDFILE_INCLUDE_DIRS ${LIBSNDFILE_INCLUDE_DIR})
endif(LIBSNDFILE_FOUND)
mark_as_advanced(LIBSNDFILE_LIBRARY LIBSNDFILE_LIBRARIES LIBSNDFILE_INCLUDE_DIR LIBSNDFILE_INCLUDE_DIRS)
include(FindPkgConfig)
pkg_search_module(SndFile REQUIRED sndfile)
include_directories(${LIBSNDFILE_INCLUDE_DIRS})
add_subdirectory(spectogram)
add_executable(demo main.cpp)
message(STATUS "sndfile include dirs path: ${LIBSNDFILE_INCLUDE_DIRS}")
message(STATUS "sndfile libs path: ${LIBSNDFILE_LIBRARIES}")
target_link_libraries (demo LINK_PUBLIC Spectrogram)
target_link_libraries(demo PRIVATE ${LIBSNDFILE_LIBRARIES})

Encounter issue with `find_package` command in Visual Studio CMake

I'm operating under a new learning curve here with c++ and using CMake in Visual Studio. Here is the partial code up until the point where I receive the error:
project(libfranka
VERSION 0.8.0
LANGUAGES CXX
)
list(INSERT CMAKE_MODULE_PATH 0 ${CMAKE_SOURCE_DIR}/cmake)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(MSVC)
add_compile_options(/W0)
else()
add_compile_options(-Wall -Wextra)
endif()
set(THIRDPARTY_SOURCES_DIR "${CMAKE_SOURCE_DIR}/3rdparty" CACHE PATH
"Directory for third-party sources")
## Dependencies
find_package(Poco REQUIRED COMPONENTS Net Foundation)
find_package(Eigen3 REQUIRED)
Once it hits the first find_package is where I encounter the error:
Here is the code within FindPoco.cmake.
find_package(Poco COMPONENTS ${Poco_FIND_COMPONENTS} CONFIG QUIET)
if(Poco_FOUND)
return()
endif()
find_path(Poco_INCLUDE_DIR Poco/Poco.h)
mark_as_advanced(FORCE Poco_INCLUDE_DIR)
foreach(component ${Poco_FIND_COMPONENTS})
set(component_var "Poco_${component}_LIBRARY")
find_library(${component_var} Poco${component})
mark_as_advanced(FORCE ${component_var})
if(${component_var})
set(Poco_${component}_FOUND TRUE)
list(APPEND Poco_LIBRARIES ${component})
if(NOT TARGET Poco::${component})
add_library(Poco::${component} SHARED IMPORTED)
set_target_properties(Poco::${component} PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${Poco_INCLUDE_DIR}
IMPORTED_LOCATION ${${component_var}}
)
endif()
endif()
endforeach()
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Poco
FOUND_VAR Poco_FOUND
REQUIRED_VARS Poco_INCLUDE_DIR Poco_LIBRARIES
VERSION_VAR Poco_VERSION
HANDLE_COMPONENTS
)
I installed poco using vcpkg in a directory titled vcpkg. Within the vcpkg directory is the libfranka directory, which houses the CMakeLists.txt file that I compile in Visual Studio. Here is an image of that directory:
Finally, here is the tutorial that I am using: https://frankaemika.github.io/docs/installation_windows.html#building-from-source
EDIT:
Per the link I followed the instructions for solving the build dependencies and here is an image of that:
Then I ran the CMakeLists.txt again and in the CMake Settings this is what I see:
Note also that I ran through the install of poco again and I noticed this and am unsure if it could be the source of the problem or if it means nothing (again, this was the out put after running vcpkg install poco):
After this I still receive the same error.
Does anyone see what it is that I am doing incorrectly?
Thank you!

CMake cannot find GLEW

I'm trying to link GLEW to my CMake project with little success.
Apparently, it can't find GLEW_LIBRARIES.
I'm using CLion 2019.3.4 and MinGW.
So far I've tried these things:
Defining GLEW_LIBRARIES
Defining GLEW_STATIC_LIBRARIES and GLEW_SHARED_LIBRARIES, as the FindGLEW.cmake file documents that (Line 41 to 45).
Doing both of the above.
Doing 1 and 2 except instead of LIBRARIES it is LIBRARY.
I don't know what else to do.
Heres the CMake lists for reference:
cmake_minimum_required(VERSION 3.15)
project(myProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
set(CMAKE_MODULE_PATH "${CMAKE_HOME_DIRECTORY}/cmake_modules/")
# This part of the code is actually in a separate file,
# called LibrarySetup.cmake
#
# include(LibrarySetup.cmake)
if(WIN32)
set(LIB_PREFIX "")
set(LIB_SUFFIX ".dll")
elseif(UNIX)
set(LIB_PREFIX "lib")
set(LIB_SUFFIX ".lib")
endif()
set(GLFW_INCLUDE_DIR "include/glfw/include/")
set(GLFW_LIBRARY "include/glfw/lib/${LIB_PREFIX}glfw3${LIB_SUFFIX}")
set(GLEW_INCLUDE_DIR "include/glew/include/")
set(GLEW_SHARED_LIBRARIES "include/glew/lib/Release/Win32/glew32.lib")
set(GLEW_STATIC_LIBRARIES "include/glew/lib/Release/Win32/glew32s.lib")
set(GLEW_VERBOSE)
# And here the external file ends.
find_package(GLFW REQUIRED)
find_package(GLEW REQUIRED)
include_directories(${GLFW_INCLUDE_DIR} ${GLEW_INCLUDE_DIRS})
add_subdirectory(include/glfw)
add_executable(myProject main.cpp)
target_link_libraries(myProject ${GLFW_LIBRARY} ${GLEW_LIBRARIES})
And the errors:
~\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\193.6494.38\bin\cmake\win\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" "~\Documents\CLion Projects\myProject"
CMake Error at ~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find GLEW (missing: GLEW_LIBRARIES)
Call Stack (most recent call first):
~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
~/AppData/Local/JetBrains/Toolbox/apps/CLion/ch-0/193.6494.38/bin/cmake/win/share/cmake-3.15/Modules/FindGLEW.cmake:207 (find_package_handle_standard_args)
CMakeLists.txt:12 (find_package)
-- Configuring incomplete, errors occurred!
See also "~/Documents/CLion Projects/myProject/cmake-build-debug/CMakeFiles/CMakeOutput.log".
[Failed to reload]
The version of CMake your script requires ships with FindGLEW which should do the work of finding the library for you (i.e. set up a Glew target, define include and library paths, etc). You can see the documentation for this module by running:
cmake --help-module findglew
Providing include paths and library definitions of GLEW to your executable should be as simple as:
find_package(GLEW REQUIRED)
add_executable(myProject main.cpp)
target_link_libraries(myProject GLEW::GLEW)
This will provide include and lib paths via the transitive dependency of the GLEW::GLEW target. You should not need to set the paths manually as your example does. The find module will search in the default system locations for the library. If it can't find it you can provide it with a hint via setting the GLEW_ROOT variable to point to your local install location.
set(GLEW_ROOT <my location of GLEW>)
How did you install GLEW? Can you provide an indication of where it is installed on your system and that might make it easier to see why the find module failed?