3

I am trying to link GLFW in my project. There is screenshot of my project structure: enter image description here

This is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(testo)

set(CMAKE_CXX_STANDARD 17)

add_executable(testo main.cpp)
add_library(glfw3 STATIC main.cpp)

include_directories(lib/glfw/include/)

find_library(GLFW glfw3 lib/glfw/lib)
target_link_libraries(testo LINK_PUBLIC ${GLFW})

Howewer, when i try to run project in clion, it gives me error:

====================[ Build | testo | Debug ]===================================
"C:\Program Files\JetBrains\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build C:\Users\Student\testo\cmake-build-debug --target testo -- -j 2
[ 50%] Linking CXX executable testo.exe
CMakeFiles\testo.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Student/testo/main.cpp:5: undefined reference to `glfwInit'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\testo.dir\build.make:87: testo.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:77: CMakeFiles/testo.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/testo.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: testo] Error 2
Marek R
  • 32,568
  • 6
  • 55
  • 140
128 bit guy
  • 33
  • 1
  • 1
  • 3
  • Please, paste your code and error directly into question – Rhathin Dec 09 '19 at 14:15
  • Does this answer your question? [Linking GLEW with CMake](https://stackoverflow.com/questions/27472813/linking-glew-with-cmake). Basically is suppose to be: `target_link_libraries(testo LINK_PUBLIC ${GLEW_LIBRARIES})`. – Marek R Dec 09 '19 at 14:16
  • changing GLFW to GLFW_LIBRARIES didn't work. Same error. – 128 bit guy Dec 09 '19 at 14:47

2 Answers2

4

Here is some github example.
Here is some SO question you are duplicating.

Based on that this should look like this:

cmake_minimum_required(VERSION 3.15)
project(testo)

set(CMAKE_CXX_STANDARD 17)

find_package(GLEW REQUIRED)

add_executable(testo main.cpp)
target_link_libraries(testo PUBLIC ${GLEW_LIBRARIES})
target_include_directories(testo PUBLIC ${GLEW_INCLUDE_DIRS})
Marek R
  • 32,568
  • 6
  • 55
  • 140
0

I think this has something to do with linux adding "lib***.a" [pre/post]fix to the lib filename.

Try adding your lib/glfw/lib directory to your link_directories() just as you do with your include_dicrectories().

Also, add_library() specifies you are trying to compile glfw3 by yourself, but what you tried doing is to only link with it. I've removed it for you.

cmake_minimum_required(VERSION 3.15)
project(testo)

set(CMAKE_CXX_STANDARD 17)

add_executable(testo main.cpp)

include_directories(lib/glfw/include/)
link_directories(lib/glfw/lib)

target_link_libraries(testo LINK_PUBLIC glfw3)
gkpln3
  • 1,317
  • 10
  • 24
  • This answer is worse then solution in question (which in fact just contains a typo). Your solution is platform specific. – Marek R Dec 09 '19 at 14:19
  • It doesn't work for me.https://paste.dimdev.org/evayuxaxeg.makefile – 128 bit guy Dec 09 '19 at 14:24
  • Try installing GLFW binaries from https://www.glfw.org/download.html and then try getting the package like so: `find_package(glfw3 3.3 REQUIRED)` and use it with `target_link_libraries(testo glfw)` – gkpln3 Dec 09 '19 at 14:32