CMake not wanting to link with libGL.so - opengl

I was coding in KDevelop, ant I set up my CMakeLists.txt to include and link against libGL and freeglut.
However, it gave me the following error:
make[2]: *** No rule to make target '/usr/lib/libGL.so', needed by 'opengl'. Stop.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(opengl)
add_executable(opengl main.cpp)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories( ${OPENGL_INCLUDE_DIRS} ${GLUT_INCLUDE_DIRS} )
target_link_libraries(opengl ${OPENGL_LIBRARIES} ${GLUT_LIBRARY} )

First, what I am about to suggest might depend on CMake version, but I don't think (I'm using cmake version 3.1.0).
Looking at the installed FindOpenGL.cmake file, it looks like the variable you want in target_link_libraries is ${OPENGL_glu_LIBRARY}. Case does matter.
The best way to test an issue like this one is to use message("var: ${var}"), then you would be able to clearly see what your variable is set to during configuration.
Hope this helps!
FYI, I found the find module in the ${cmake_install_dir}/Modules/Find* where cmake_install_dir=/usr/share/cmake on my linux machine.
EDIT**
Sorry, was tired this morning and missed that you wanted GLUT, not GLU. There is a FindGLUT.cmake Module. You should call find_package(GLUT REQUIRED) and use ${GLUT_LIBRARIES} and ${GLUT_INCLUDE_DIR}. Sorry about the confusion!

Related

Using CMAKE find_package with SDL2

I've got a project that uses CMAKE, and needs to link SDL2. I'm trying to understand how find_package works. At one point in the past, I was able to get find_package to work by supplying my own FindSDL2.cmake, but after some new linker errors, I decided to try a different apporoach. The reading I'm doing seems to imply that, after I've installed the libsdl2-dev package, I should be able to just use find_package(SDL2 REQUIRED) and then target_link_libraries(Suqua PRIVATE SDL2::SDL2), but cmake throws a package not found error. Do I need to provide a custom FindSDL2.cmake?
CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
set(CMAKE_CXX_STANDARD 17)
find_package(SDL2 CONFIG REQUIRED)
file(GLOB source_files
"src/*.cpp"
"header/*.h"
)
add_library(Suqua ${source_files} "src/glad.c" )
target_include_directories(Suqua PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/header)
target_include_directories(Suqua PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include)
target_link_libraries(Suqua PRIVATE SDL2::SDL2 enet)
if(UNIX)
target_link_libraries(Suqua PRIVATE stdc++fs)
endif()
Error
CMake Error at Suqua/CMakeLists.txt:13 (add_library):
Target "Suqua" links to target "SDL2::SDL2" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an
ALIAS target is missing?
Thank you, and if you have any other critiques of my CMakeLists, they'd be greatly appreciated!
Answer supplied by Tsyvarev
When using an installed library (not built from source), use the SD2_LIBRARIES variable.
On another note, I actually fixed this earlier, but assumed I was doing something wrong when I got a linker error relating to linking CMAKE_DL_LIBS, which I'd removed because I assumed it didn't do anything :/
Definitely gonna read through that CMake book. Thanks all!

CMake not finding GL/glew.h

I'm using CLion under pop_OS! 20.04 and am currently trying to get CMake to work with GLEW because I want to follow these tutorials to get started with OpenGL. I'm relatively new to C/C++ and completely new to CMake.
I installed the libgl1-mesa-dev,libglew-dev,libglfw3,libglfw3-dev packages with apt-get install and the glew.h is located alongside the other GL header files in /usr/include/GL.
When trying to compile a simple program:
#include <GL/glew.h>
int main() {
return 0;
}
cmake can't find the headerfile:
test/main.cpp:1:10: fatal error: GL/glew.h: No such file or directory
1 | #include <GL/glew.h>
| ^~~~~~~~~~~
Do I have to manually add these header files in CMakeLists.txt for cmake to find them? I tried like a dozen different suggestions but I didn't get it to work. For example, I tried
cmake_minimum_required(VERSION 3.17)
project(test)
set(CMAKE_CXX_STANDARD 14)
find_package(GLEW REQUIRED)
include_directories(${GLEW_INCLUDE_DIRS})
link_libraries(${GLEW_LIBRARIES})
target_link_libraries(test GLEW::GLEW)
but this results in
CMake Error at /app/extra/clion/bin/cmake/linux/share/cmake-3.17/Modules/FindPackageHandleStandardArgs.cmake:164 (message):
Could NOT find GLEW (missing: GLEW_INCLUDE_DIRS GLEW_LIBRARIES)
Is this somehow a problem with CLion and I need to include libraries into my project in a different manner? Or am I using cmake in a wrong way?
Your last try was nearly right.
Forget about include_directories and link_libraries commands. All it needs when doing modern CMake is an add_executable(test main.cpp) followed by a target_link_libraries(test PRIVATE GLEW::glew) command. The target GLEW::glew (case matters here) bundles all compile definitions, include directories, libraries, and further settings that are needed to compile and link your example.
Your CMakeLists.txt would now look like this:
cmake_minimum_required(VERSION 3.17)
project(test)
set(CMAKE_CXX_STANDARD 14)
find_package(GLEW REQUIRED)
add_executable(myTest main.cpp)
target_link_libraries(myTest PRIVATE GLEW::glew)
Edit: Just noticed, that GLEW::GLEW can also be used.
The answer by #vre is OK. I only add how to find it yourself.
First, run
cmake --help-module-list
and look for GLEW. Under Linux, you can simplify this step to
cmake --help-module-list | grep -i Glew
where -i stands for "case insesitive match", as we usually don't remember if it's GLEW or perhaps Glew. Once you know the module name, you can ask for the specific help related to just this module:
cmake --help-module FindGLEW
The answer is there. Read and enjoy!
PS. There's no need to memorize this answer. All can be reduced to cmake --help.

proper way to write a CMakeLists referencing HDF5 in Windows

I've already downloaded an built HDF5 under Windows using CMake, I also generated an installer to install it under Program Files.
Below the CMakeLists.txt I wrote to be able to use HDF5 in a program I already wrote under Linux :
cmake_minimum_required(VERSION 2.8)
project(Hdf5DataFeed)
add_definitions(-DWINDOWS)
find_package(HDF5)
FIND_LIBRARY(HDF5_HL_LIBRARY hdf5_hl)
FIND_LIBRARY(ZLIB zlib)
find_library(ZMQ_LIB zmq)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
include_directories(${ZMQ_LIB_INCLUDE})
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${ZLIB} "C:/Program Files/HDF_Group/HDF5/1.10.1/lib/libszip.lib" ${VTK_LIBRARIES} ${ZMQ_LIB} ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARY} Qt5::Core Qt5::Gui Qt5::Widgets)
target_include_directories(${PROJECT_NAME} PRIVATE ${HDF5_INCLUDE_DIRS})
As you can see above, to link HDF5 under Visual Studio, I needed Zlib, Szip (that I had to enter an absolute path to it, I don't like that), HDF5 library and the HDF5 High Level (Lite) library.
These libraries are located under C:\Program Files\HDF_Group\HDF5\1.10.1\lib :
libhdf5.lib <============
libhdf5.settings
libhdf5_cpp.lib
libhdf5_hl.lib <=====
libhdf5_hl_cpp.lib
libhdf5_tools.lib
libszip.lib <=== ????
libzlib.lib <====
I use CMake-Gui to inform CMake of the libraries path (except for Szip, I don't know why CMake doesn't know about it, and why I don't have the possibility to just feed CMake the library directory instead of indicating the path of few of them).
I want to use CMake-GUI to inform CMake of Szip library path, but this last doesn't create an entry of it, I only have these entries related to HDF5 :
I'm having troubles with HDF5 also under Ubuntu (see this question : hdf5.h no such file or directory under Ubuntu and CMake).
For now, it's only under CentOS 7 that I didn't encounter any issues with HDF5.
If someone can give me/us a final solution that works both on Windows and Ubuntu that would be great !
Does this solution work for you?
cmake_minimum_required(VERSION 2.8)
project(Hdf5DataFeed)
# necessary?
add_definitions(-DWINDOWS)
find_package(HDF5 REQUIRED COMPONENTS C CXX HL)
find_package(ZLIB REQUIRED)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
find_library(ZMQ_LIB zmq)
include_directories(${ZMQ_LIB_INCLUDE} ${HDF5_INCLUDE_DIR}
${ZLIB_INCLUDE_DIRS})
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${HDF5_LIBRARIES}
${HDF5_HL_LIBRARIES} ${ZLIB_LIBRARIES} ${VTK_LIBRARIES}
Qt5::Core Qt5::Gui Qt5::Widgets ${ZMQ_LIB})
Recommendation 1: Surely, there is a way to find Qt5 via find_package, i.e.,
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
and then add the correct variables to include_directories and target_link_libraries. Not sure whether such a possibility exists for the zmq library, though.
Recommendation 2: I think the call the aux_source_directory should be avoided in most cases. Create an explicit list of your source files instead.
For libszip, adding a find_library is better than putting an absolute link to it. For ZLib, it is preferable to use find_library as find_package will require you to feed CMake with an include directory which is not required for HDF5. Finally, it is preferable to use find_package for ZMQ, otherwise, we need to add manually the entry "ZMQ_LIB_INCLUDE".

CMake can't find OpenCVConfig.cmake

I'm trying to make OpenCV work with CMake in Clion on Windows 10 (64bit). I've already set OPENCV_DIR to the OpenCV build folder that contains OpenCVConfig.cmake, but cmake keeps nagging it cannot find the file.
Could not find a package configuration file provided by "OpenCV" with any of the following names:
OpenCVConfig.cmake opencv-config.cmake
Here's my CMakeLists:
cmake_minimum_required(VERSION 3.6)
project(MyProject)
set(OpenCV_DIR "D:/opencv/build/")
find_package(OpenCV REQUIRED core imgproc)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(MyProject main.cpp)
target_link_libraries(MyProject ${OpenCV_LIBS})
And here's the content of the D:/opencv/build/ folder:
What can be the problem here? Thanks in advance!
I've found the problem, totally unexpected for a CMake newcomer like me.
It turns out I installed CMake as part of Cygwin, and thus filesystem paths take the form of /cygdrive/d/opencv/build/ instead. That works without a problem.
So if you're on Windows, please note the different file path style in case CMake is provided by Cygwin.

Clion: Target "[NAME]" not found

When trying to build my project, I see this in the build configurations (it pops up):
"LUCID" is the name of my project. I think it all built fine yesterday, but now after only restating I'm getting this:
Error: Target 'LUCID (LUCID)' not found.
The "Target" dropdown only has that one item in it (and also the "Build All" option). I do have project(LUCID) and add_executable(LUCID ${SOURCE_FILES}) in CMakeLists.txt, as was suggested in this question, although the situation is slightly different.
So, why am I getting this error and what do I do to fix it?
Another thing to note is that all the file names that should be part of my project and are specified in set(SOURCE_FILES ...) are greyed out in the CLion file browser, which they should not be.
I think you may put all you include_directory before add_executable.
And use only the find_package(SDL2 REQUIRED) futher more if you use the REQUIRED keyword you don't have to use the if (lib_FOUND) source here.
You CMake may look like something like this
cmake_minimum_required(VERSION 3.2)
project(LUCID)
set(EXEC_NAME LUCID)
MESSAGE("a test message")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
find_package (Box2D REQUIRED)
find_package (opengl REQUIRED)
find_package (SDL2 REQUIRED)
set(INCLUDE_DIR
sinclude
sinclude/3rdparty
uniheader
D:/freetype-2.5.3/GnuWin32/include
${BOX2D_INCLUDE_DIRS}
${OPENGL_INCLUDE_DIRS}
${SDL2_INCLUDE_DIRS}
)
include_directories(${INCLUDE_DIR})
set(SOURCE_FILES
ssrc/Cam.cpp
#...
#Lots of source and header files in the same form
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
message(STATUS "Boaorm")
add_executable(${EXEC_NAME} ${SOURCE_FILES})
target_link_libraries(${EXEC_NAME} ${BOX2D_LIBRARIES} ${OPENGL_LIBRARIES} ${SDL2_LIBRARY})
For SDL i used this answer, but i don't like to use ${PROJECT_NAME} for executable name (you can choose what you prefer anyway)
Edit :
Multiple target_link_libraries are explained here
The problem with the old cmake was the include_directories after the add_executable and the common toolchain is include -> compile -> link then i just follow this logic.
Reset cache and reload project!
Tools > CMake > Reset Cache and Reload Project
I came across this weird bug yesterday. My CMakeLists.txt is correct (because I can build the project though the terminal).
The end of my CMakeLists.txt looks like this:
add_executable(assignment-1 main.cpp ${SOURCES})
add_library(libassignment-1 STATIC ${SOURCES})
I removed the CMake cache directory, commented out add_library() and reloaded it. Just like that, CLion can now find the assignment-1 executable. Then I uncommented the last line. All the configurations are still fine.