After searching everywhere I could not find anything or anyone to help me figure out how to add GL GLEW and SDL2 Libraries to my CMakeLists.txt. I am using Ubuntu 14.04 LTS and I installed the following libraries with
sudo apt-get install libsdl2-dev #for SDL-2
sudo apt-get install libgl-dev #for GL
sudo apt-get install libglew-dev #for GLEW
This all worked great, and i was able to compile in g++ with this commmand
g++ ./main.cpp ./display.h ./display.cpp ./shader.cpp ./shader.h -l SDL2 -l GL -l GLEW
Now I need to switch to CMake Compiler and I have no clue how to add the libraries GL, GLEW, and SDL2 to the CMakeLists.txt.
The way to include the libraries depends on a few things. Some packages such as sdl2 have pkgconfig files that define the libraries and includes to use.
Cmake comes with a FindPkgConfig module that can get it for you.
For example:
include(FindPkgConfig)
pkg_check_modules(SDL2 REQUIRED sdl2)
target_link_libraries(executablename ${SDL2_LIBRARIES})
You can also manually add them with the target_link_libraries function.
Other packages have "Find" modules like GLEW: /usr/share/cmake-*/Modules/FindGLEW.cmake
CMake has lots of great docs in the man pages, and on their wiki as #Mikael Persson mentioned.
Related
github repo. i am using c++20 with cmake on visual studio to program on wsl and getting error loading shared library. can't find file libmariadb.so.3.
I used the build instructions to build it for Debian & Ubuntu on wls and it was installed in these paths.
so in my cmake I included
find_package(mariadbcpp)
include_directories("/usr/local/include/mariadb")
link_directories("/usr/local/lib/mariadb")
target_link_libraries(${PROJECT_NAME} mariadbcpp)
when I run I get the following error
error while loading shared libraries: libmariadb.so.3: cannot open shared object file: No such file or directory
I tried running
sudo /sbin/ldconfig -v
and I also tried including this in my top level cmake
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
SET(CMAKE_INSTALL_RPATH "/usr/local/lib/mariadb")
to get it working you just need to add this to your cmake
include_directories("/usr/include/mariadb") #path to include folder
add_library(mariadbcpp STATIC IMPORTED)
set_property(TARGET mariadbcpp PROPERTY IMPORTED_LOCATION "/usr/lib/libmariadbcpp.so") #path to libmariadbcpp.so
then just include
#include <conncpp.hpp>
in source
to install I followed this Debian/Ubuntu and in step 10 the command to install libmariadbcpp.so.3 and lib/mariadb... should have been lib64/mariadb...
like so
sudo install lib64/mariadb/libmariadbcpp.so /usr/lib
sudo install lib64/mariadb/libmariadbcpp.so.3 /usr/lib
sudo install lib64/mariadb/plugin/* /usr/lib/mariadb/plugin
I am trying write CMakeLists with FFmpeg package, with compile on Windows and Linux.
First downloaded from FFmpeg-Builds shared releases
I imagine the structure of the project like this:
<project root>
deps/
ffmpeg/
win-x64/
incluve/
lib/
bin/
linux-x64/
incluve/
lib/
bin/
src/
CMakeLists.txt
How to help CMake find libraries: avcodec, avformat, avutil, etc?
Maybe in the folder lib/pkgconfig using PkgConfig it is possible to specify the path.
But I dont know how
The following worked well for me on Linux with cmake.
You will have to find the equivalents for Windows.
I used ffmpeg on Windows, but without cmake (i.e. directly in a Visual Studio project).
Install pkg-config, nasm:
sudo apt-get install -y pkg-config
sudo apt-get install nasm
Download ffmpeg source code:
https://ffmpeg.org/download.html
Build ffmpeg and install it:
tar -xvf <downloaded_filename>
cd /root/folder/with/ffmpeg/src
./configure
make
sudo make install
Add the following to your CMakeLists.txt:
In the beginning:
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBAV REQUIRED IMPORTED_TARGET
libavdevice
libavfilter
libavformat
libavcodec
libswresample
libswscale
libavutil
)
set(CMAKE_FIND_LIBRARY_SUFFIXES .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
In the linker area:
target_link_libraries(${PROJECT_NAME} PUBLIC PkgConfig::LIBAV)
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)
Similar issue here.
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
# Add test cpp file
add_executable(foo foo.cpp)
# Link test executable against gtest & gtest_main
target_link_libraries(foo ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES} pthread)
And my foo.cpp:
#include <gtest/gtest.h>
TEST(sample_test_case, sample_test)
{
EXPECT_EQ(1, 1);
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Now, all works fine when using the g++ compiler. However, when attempting to use QNX's compiler, ntox86-c++, I run into this problem:
CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:97 (MESSAGE):
Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR
GTEST_MAIN_LIBRARY)
I am on Ubuntu using the ntox86-c++ compiler, googletest, and cmake-gui.
What gives?
Google test was probably not properly installed (libgtest-dev may install only source files that needed to be compiled). I had the same problem and I followed the instructions from http://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/
sudo apt-get install libgtest-dev
sudo apt-get install cmake # install cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
#copy or symlink libgtest.a and libgtest_main.a to your /usr/lib folder
sudo cp *.a /usr/lib
This worked for me.
As explained by #detrick, the Ubuntu package libgtest-dev only installs sources, so you need to build and install the libraries yourself.
However, there is a much simpler way for building and installing since Ubuntu 18.04 than the manual commands in other answers:
sudo apt install libgtest-dev build-essential cmake
cd /usr/src/googletest
sudo cmake .
sudo cmake --build . --target install
ntox86-c++ looks like a cross-compiler, libgtest-dev package does not provide compiled library for the target platform (QNX).
Since year 2014 compiled libraries was dropped from libgtest-dev and has been added again in Ubuntu-20.04 focal, so find_package(GTest REQUIRED) does not work on Ubuntu-16.04 xenial and Ubuntu-18.04 bionic. The reason is given in /usr/share/doc/googletest/README.Debian (/usr/share/doc/libgtest-dev/README.Debian) and in e.g. in /usr/src/googletest/googletest/docs/V1_7_FAQ.md "Why is it not recommended to install a pre-compiled copy of Google Test (for example, into /usr/local)" section. Difference in compiler flags for the library and for a test could generate incompatible executable code. The problem with 18.04 and 16.04 is the reason why I have decided to add another answer to the old question.
add_subdirectory could be used to compile gtest provided by system package
set(GTest_ROOT /usr/src/googletest/googletest)
add_subdirectory(${GTest_ROOT}
"${CMAKE_CURRENT_BINARY_DIR}/googletest" EXCLUDE_FROM_ALL)
add_executable(test test.cpp)
target_link_libraries(test gtest_main)
# or just gtest if main function is defined
Instead of using system package for googletest sources there are at least 2 variants how to obtain particular version from git (besides obvious submodule), see
https://cmake.org/cmake/help/latest/module/FetchContent.html
https://github.com/google/googletest/blob/master/googletest/README.md
Some time ago I created a dockerfile and it helps me to keep a kind of recipe for installing later on google test on my systems:
apt-get install -y git g++ make cmake
git clone https://github.com/google/googletest.git
cd googletest
mkdir gbuild && cd gbuild && cmake .. && make
cp -r googletest/include/gtest /usr/local/include
cp gbuild/googlemock/gtest/lib*.a /usr/local/lib
cp gbuild/googlemock/lib*.a /usr/local/lib
I hope it helps :)
I am setting up GLFW and Vulkan for a project of mine. For Vulkan I am using MoltenVk to get Vulkan compat and GLFW for the window creation. The IDE I am using is CLion, which uses the CMake system.
Judging from some github issues, there seems to be support for this setup, but nobody there mentions how it is done.
GLFW is installed through homebrew and MoltenVK manually by adding the MoltenVK and vulkan folders to usr/local/include and the contents of the MacOS folder to usr/local/lib (even though I am quite sure MoltenVK.framework should not be there).
At this point, Clion can see the GLFW and Vulkan headers, but I still need to link them up properly.
the full CMakeLists.txt file is this now:
cmake_minimum_required(VERSION 3.8)
project(VulkanEngine)
#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} “${CMAKE_SOURCE_DIR}/cmake/Modules”)
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES src/main.cpp)
add_executable(VulkanEngine ${SOURCE_FILES})
#Finding and linking GLFW3
find_package(glfw3 3.2 REQUIRED)
if (glfw3_FOUND)
include_directories(${glfw3_INCLUDE_DIRS})
target_link_libraries (VulkanEngine ${glfw3_LIBRARIES})
endif (glfw3_FOUND)
#Finding and linking Vulkan
find_package (Vulkan)
if (Vulkan_FOUND)
include_directories(${Vulkan_INCLUDE_DIRS})
target_link_libraries (VulkanEngine ${Vulkan_LIBRARIES})
endif (Vulkan_FOUND)
the console tells the following when the cmake project has been reloading:
-- Could NOT find Vulkan (missing: Vulkan_LIBRARY)
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/mtesseract/dev/cpp/VulkanEngine/cmake-build-debug
to me this indicates that GLFW has been found and linked, but when I try to build the "hello world" for GLFW, I get the following message:
Undefined symbols for architecture x86_64:
"_glfwInit", referenced from:
_main in main.cpp.o
I got FindVulkan.cmake from the GLFW github page https://git.io/v5ggN, since it has support for MoltenVK, but I am not sure if it is picked up by CMake at all. (I put the file in (projectroot)/cmake/modules/)
At this point, I am out of ideas as to why things are not linking properly, so help is appreciated.
I have found a solution that at least till some degree seems to work. At this point I have another problem to take care of (GLFW doesn't seem to pick up on MoltenVK's presence).
The process thus far is as follows (and can be found on my github page (github.com/mtesseracttech/VulkanEngine):
Vulkan + GLFW + GLM Setup Process with CMake and Package Managers
Windows:
Preparation:
Create CLion Application Project
Install MSYS2 (Cygwin-like package manager that includes a windows port of Arch's PacMan)
Install LunarG VK SDK
Download FindVulkan.cmake from GFLW's Github (it includes a path for MoltenVK(for OSX Later)) and put it in (proj_root/cmake/modules)
MSYS2 Commands:
$ pacman -Su //Updates pacman
$ pacman -Ss {packageName} //Is used for searching for exact package names
Installed Packages through msys2:
$ pacman -S mingw-w64-x86_64-toolchain //Installs the CLion toolchain that includes CMake, Make, GCC, etc.
$ pacman -S mingw-w64-x86_64-glfw //Installs GLFW
$ pacman -S mingw-w64-x86_64-glm //Installs GLM
$ pacman -S mingw-w64-x86_64-vulkan //Vulkan can also be installed through this method, but I went with LunarG
Enter the following in the CMakeLists.txt file in the root of the project:
######################################################################################
cmake_minimum_required(VERSION 3.8)
project(VulkanEngine)
set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES src/main.cpp)
add_executable(VulkanEngine ${SOURCE_FILES})
#Setting up PkgConfig
find_package(PkgConfig REQUIRED)
#Finding and linking GLFW3
pkg_search_module(GLFW3 3.2 REQUIRED glfw3)
if(GLFW3_FOUND)
message(STATUS "Found GLFW, Including and Linking now")
include_directories(${GLFW3_INCLUDE_DIRS})
target_link_libraries(VulkanEngine ${GLFW3_STATIC_LIBRARIES})
endif(GLFW3_FOUND)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
#Finding and linking Vulkan
find_package (Vulkan)
if (VULKAN_FOUND)
message(STATUS "Found Vulkan, Including and Linking now")
include_directories(${VULKAN_INCLUDE_DIR})
target_link_libraries (VulkanEngine ${VULKAN_LIBRARY})
endif (VULKAN_FOUND)
######################################################################################
OSX:
Preparation
Create CLion Application Project
Install HomeBrew (package manager)
Download MoltenVK
Download FindVulkan.cmake from GFLW's Github (it includes a path for MoltenVK(for OSX Later)) and put it in (proj_root/cmake/modules)
Homebrew:
brew update //Updates homebrew
brew install glfw //Installs GLFW
brew install glm //Installs GLM
MoltenVK:
Place MoltenVK/macOS's contents into /usr/local/lib
Place MoltenVK/macOS/MoltenVK.framework/headers's contents into /usr/local/inc/MoltenVK
CMake:
Same as on windows, but in the FindVulkan.cmake file, add at the very end of the elseif(APPLE) block the line:
set(VULKAN_INCLUDE_DIR "/usr/local/include/MoltenVK")
This is not a pretty or flexible solution, but for now it will do.
I am pretty sure this solution is not ideal since GLFW isn't picking up on MoltenVK with this tactic, but at least things compile, so I see that as progress.
Seems your issue is that you haven’t defined the VULKAN_SDK variable. Make sure you point this to the parent folder of /lib & /include inside the SDK and FindVulkan should work
VULKAN_SDK=/opt/vulkan/x86_64 cmake.