简体   繁体   中英

GLFW + CMake + Clion many undefined referenences

complete CMake Noob here. I am trying to use glfw library in Clion IDE and even after 2 days of googling, i cannot understand what am i missing/doing wrong.

Here are the details:

OS: Windows 8 Pro 64bit

IDE: Clion

Make tool: CMake

Complier: MinGW v3.21

Error log:

"C:\Program Files (x86)\JetBrains\CLion 1.2.3\bin\cmake\bin\cmake.exe"--build C:\Users\PranayKarani\.CLion12\system\cmake\generated\185d1d8\185d1d8\Debug --target openGL -- -j 4
[ 50%] Linking CXX executable openGL.exe
CMakeFiles\openGL.dir/objects.a(main.cpp.obj): In function `key_callback':
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:15: undefined reference to `glfwSetWindowShouldClose'
CMakeFiles\openGL.dir/objects.a(main.cpp.obj): In function `main':
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:22: undefined reference to `glfwSetErrorCallback'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:24: undefined reference to `glfwInit'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:27: undefined reference to `glfwCreateWindow'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:30: undefined reference to `glfwTerminate'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:34: undefined reference to `glfwMakeContextCurrent'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:35: undefined reference to `glfwSwapInterval'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:37: undefined reference to `glfwSetKeyCallback'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:44: undefined reference to `glfwGetFramebufferSize'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:47: undefined reference to `_imp__glViewport@16'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:48: undefined reference to `_imp__glClear@4'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:50: undefined reference to `_imp__glMatrixMode@4'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:51: undefined reference to `_imp__glLoadIdentity@0'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:52: undefined reference to `_imp__glOrtho@48'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:53: undefined reference to `_imp__glMatrixMode@4'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:55: undefined reference to `_imp__glLoadIdentity@0'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:56: undefined reference to `glfwGetTime'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:56: undefined reference to `_imp__glRotatef@16'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:58: undefined reference to `_imp__glBegin@4'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:59: undefined reference to `_imp__glColor3f@12'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:60: undefined reference to `_imp__glVertex3f@12'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:61: undefined reference to `_imp__glColor3f@12'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:62: undefined reference to `_imp__glVertex3f@12'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:63: undefined reference to `_imp__glColor3f@12'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:64: undefined reference to `_imp__glVertex3f@12'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:65: undefined reference to `_imp__glEnd@0'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:67: undefined reference to `glfwSwapBuffers'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:68: undefined reference to `glfwPollEvents'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:39: undefined reference to `glfwWindowShouldClose'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:71: undefined reference to `glfwDestroyWindow'
E:/Pranay/BotX games/C++ projects/openGL/main.cpp:73: undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\openGL.dir\build.make:97: recipe for target 'openGL.exe' failed
mingw32-make.exe[3]: *** [openGL.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/openGL.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/openGL.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/openGL.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/openGL.dir/rule] Error 2
Makefile:117: recipe for target 'openGL' failed
mingw32-make.exe: *** [openGL] Error 2

Project structure:

在此处输入图像描述

CMakeList.txt

cmake_minimum_required(VERSION 3.3)
project(openGL)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)

add_executable(openGL ${SOURCE_FILES})

#add_library(A SHARED IMPORTED)
#set_property(TARGET A PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/GLFW/lib-mingw-w64/glfw3.dll)

add_library(B STATIC IMPORTED)
set_property(TARGET B PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/GLFW/lib-mingw-w64/glfw3dll.a)

add_library(C STATIC IMPORTED)
set_property(TARGET C PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/GLFW/lib-mingw-w64/libglfw3.a)

target_link_libraries(openGL B C)

This is how i fixed this headache:

  1. Downloaded 32 bit of glfw binaries (even though i am on 64 bit machine)

  2. Renamed and Copy pasted selected files from the binaries in my project like this: 在此处输入图像描述

  3. modified my CMakeLists.txt like this:

在此处输入图像描述

  1. build and hit Run!

Add the following at the end of the CMakeLists.txt

find_package(glfw3 REQUIRED )
target_link_libraries(projectname  GL glfw)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM