GLEW won't work with CMAKE - c++

I'm trying to write a simple program with GLFW and GLEW, and while I could successfully add the needed GLFW libraries I can't do the same with the GLEW ones.
I'm totally new to CMAKE so I don't know what I should do differently. I am using CLion btw. Thanks in advance!
cmake_minimum_required(VERSION 3.3)
project(OpenGLHelloWorld)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(OpenGLHelloWorld ${SOURCE_FILES})
add_subdirectory(../glfw-3.1.2 ${CMAKE_CURRENT_BINARY_DIR}/glfw_bin)
include_directories(../glfw-3.1.2/include)
target_link_libraries(OpenGLHelloWorld glfw ${GLFW_LIBRARIES})
ADD_DEFINITIONS(-DGLEW_STATIC)
include_directories(../glew-1.13.0/include)
link_libraries(../glew-1.13.0/lib)
And the cpp file:
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
using namespace std;
int main() {
if(!glewInit()) {
fprintf(stderr, "Could not start GLFW3\n");
}
return 0;
}
And the error:
undefined reference to `glewInit#0'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\OpenGLHelloWorld.dir\build.make:97: recipe for target 'OpenGLHelloWorld.exe' failed
mingw32-make.exe[3]: *** [OpenGLHelloWorld.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/OpenGLHelloWorld.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/OpenGLHelloWorld.dir/rule] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/OpenGLHelloWorld.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/OpenGLHelloWorld.dir/rule' failed
Makefile:161: recipe for target 'OpenGLHelloWorld' failed
mingw32-make.exe: *** [OpenGLHelloWorld] Error 2

You didn't link your compiled code to glew.
target_link_libraries(OpenGLHelloWorld glfw ${GLFW_LIBRARIES} GLEW)
That should fix glew lib linking to your executable.
HTH.

Related

How to link libraries properly using CMakeLists.txt for MinGW?

I have been pulling out of my hair because of CMakelists.txt. I would like to create a C++ application with GLFW, GLEW libraries, using MinGW and CLion IDE.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.17.3)
project(imguiTask)
set(CMAKE_CXX_STANDARD 17)
add_compile_options(-DGLEW_NO_GLU)
add_executable(${PROJECT_NAME}
src/main.cpp
src/imgui.cpp
src/imgui_draw.cpp
src/imgui_widgets.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC includes)
add_library(libraries STATIC IMPORTED)
target_link_libraries(${PROJECT_NAME} glfw3)
I placed the needed libraries in the libraries folder as you can see in the picture. The folder is located in the project root. It is giving me the following error:
d:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lglfw3
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [imguiTask.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/imguiTask.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/imguiTask.dir/rule] Error 2
mingw32-make.exe: *** [imguiTask] Error 2
It seeems to me, that it does not search in the folder I specified. I don't know why.

CMake build static executable file

I want to build an executable that includes all of libs that I used (opencv, dlib) so it can work on any computer.
Here is my current CMakeList.txt
cmake_minimum_required(VERSION 3.5.1)
project(adc_fr)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(/media/sf_dlib-19.9/dlib dlib_build)
FIND_PACKAGE(OpenCV REQUIRED)
set(SOURCE_FILES
FDetect.cpp
FDetect.h
FExtract.cpp
FExtract.h
main.cpp
)
add_executable(adc_fr ${SOURCE_FILES})
set_target_properties(adc_fr PROPERTIES LINK_FLAGS "-static")
target_link_libraries(adc_fr
${OpenCV_LIBS}
dlib
)
Here is the error I got when linking
/usr/bin/ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libwebp.so'
collect2: error: ld returned 1 exit status
CMakeFiles/adc_fr.dir/build.make:409: recipe for target 'adc_fr' failed
make[2]: *** [adc_fr] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/adc_fr.dir/all' failed
make[1]: *** [CMakeFiles/adc_fr.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I do have libwebp.a available in /usr/lib/x86_64-linux-gnu/
How can I force to use static libwebp.a instead of libwebp.so?
I did try to add #SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a") but it still gave the same error.
Much appreciated!
How about this:
find_library(LIBWEBP_LOCATION libwebp.a)
target_link_libraries(adc_fr
${OpenCV_LIBS}
dlib
${LIBWEBP_LOCATION}
)

GLFW functions not found

I try to compile a program that uses GLFW3 library on Ubuntu 16.04 x86_64. I installed libglfw3 and libglfw3-dev packages. Next, I wrote CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project (Test)
set (CMAKE_CXX_FLAGS "-lGL -lGLEW")
set (CMAKE_EXE_LINKER_FLAGS -lglfw )
add_executable(Test src/main.cpp)
And main.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main() {
if (!glfwInit()) {
return -1;
}
glfwTerminate();
return 0;
}
But I get an error from make command:
main.cpp:(.text+0x5): undefined reference to `glfwInit'
main.cpp:(.text+0x1a): undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
CMakeFiles/Test.dir/build.make:94: recipe for target 'Test' failed
make[2]: *** [Test] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Test.dir/all' failed
make[1]: *** [CMakeFiles/Test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
What do I doing wrong? Why can it not find GLFW3?
Native way for link with library in CMake is target_link_libraries:
cmake_minimum_required (VERSION 2.6)
project (Test)
add_executable(Test src/main.cpp)
target_link_libraries(Test GL GLEW glfw)
Note, that this works only if GL and other libs are installed to the default places, known to compiler and linker. Otherwise it is better to use find_package(GLEW) and other find_package() calls, as noted by #tambre.

Trouble linking header-only library with CMake in Clion

I've been at this for a few hours now, and feeling rather stupid. The library in question is spdlog, which is just a whole bunch of headers. I am using CMake and cygwin to compile.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(spdlog_test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(E:/Dev/libraries/spdlog)
link_directories(E:/Dev/libraries/spdlog)
set(SOURCE_FILES main.cpp)
add_executable(spdlog_test ${SOURCE_FILES})
target_link_libraries(spdlog_test spdlog)
All the paths are definetely correct.
Output when building:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../x86_64-pc-cygwin/bin/ld: cannot find -lspdlog
collect2: error: ld returned 1 exit status
CMakeFiles/spdlog_test.dir/build.make:94: recipe for target 'spdlog_test.exe' failed
make[2]: *** [spdlog_test.exe] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/spdlog_test.dir/all' failed
make[1]: *** [CMakeFiles/spdlog_test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

allegro5 project in CLion, ld: library not found error

I'm making a C++ and allegro5 project for university. I compiled allegro library and it's working well in Xcode for example. But I wanted to do my project in CLion and as soon as try to build project including allegro it throws an error:
ld: library not found for -lallegro_acodec
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [TEST1] Error 1
make[1]: *** [CMakeFiles/TEST1.dir/all] Error 2
make: *** [all] Error 2
CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(TEST1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(TEST1 ${SOURCE_FILES})
INCLUDE_DIRECTORIES( /usr/local/Cellar/allegro/5.0.11/include )
LINK_DIRECTORIES( /usr/local/Cellar/allegro/5.0.11/lib )
TARGET_LINK_LIBRARIES(TEST1
allegro_acodec
allegro_audio
allegro_color
allegro_dialog
allegro_image
allegro_main
allegro_memfile
allegro_physfs
allegro_primitives
allegro_ttf
allegro_font
allegro)
main.cpp:
#include <iostream>
#include <allegro5/allegro.h>
using namespace std;
int main(int argc, char **argv) {
al_init();
return 0;
}
I'm working on OSX 10.11. I could not find solution for my problem. I konw that allegro and CLion are not that popular. Can anyone help me what that error means?
You should issue link_directories before add_executable.
From the documentation about link_directories:
The command will apply only to targets created after it is called.