0

I struggle to get GLFW Windows pre-compiled binaries working within my CLion Project. Those libraries are placed in a external directory. I do not want them to be in my project library but should (of course) be shipped when releasing the application. I am new to C++ but I thought to accomplish this might be as easy as it is in Java (Intellij Idea -> dependencies -> ...).

GLFW Windows pre-compiled binaries

I use MinGW 5.0 and CMake 3.10.2; My CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(Hatsudouki_core)

set(CMAKE_CXX_STANDARD 17)

link_directories(F:\\C++\\ExternalLibraries\\GLFW\\lib-mingw-w64)
include_directories(F:\\C++\\ExternalLibraries\\GLFW\\include)

add_executable(Hatsudouki_core main.cpp)

target_link_libraries(Hatsudouki_core glfw3)

Main.cpp

#include <iostream>
#include <GLFW/glfw3.h>

int main() {
    if (!glfwInit())
         std::cout << "error!" << std::endl;
    else
         std::cout << "success!" << std::endl;
    return 0;
}

Build output

"F:\C++\CLion 2018.1\bin\cmake\bin\cmake.exe" --build C:\Users\simon\CLionProjects\Hatsudouki-core\cmake-build-debug --target Hatsudouki_core -- -j 4
[ 50%] Linking CXX executable Hatsudouki_core.exe
CMakeFiles\Hatsudouki_core.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/simon/CLionProjects/Hatsudouki-core/main.cpp:5: undefined reference to `glfwInit'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Hatsudouki_core.exe] Error 1
CMakeFiles\Hatsudouki_core.dir\build.make:96: recipe for target 'Hatsudouki_core.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Hatsudouki_core.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/Hatsudouki_core.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Hatsudouki_core.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/Hatsudouki_core.dir/rule] Error 2
Makefile:117: recipe for target 'Hatsudouki_core' failed
mingw32-make.exe: *** [Hatsudouki_core] Error 2

I tried following solutions mentioned here:
- GLFW doc and GLFW doc2 (find package does not work, no CMake file)
- Github issue report related to Github issue report 2 which then leads to the solution to put FindGLFW.cmake into some directory? Tried to put it here GLFW\FindGLFW.cmake but does not work
- Linking did not work as well as mentioned here: Stackoverflow

Image GLFW directory: GLFW Windows pre-compiled binaries

I think I just do not understand how CMake, external Libraries and C++ work together to accomplish this fairly easy task. I believe comparison to Java would help (used to work with gradle)

EDIT 1 As suggested I added following: I put the Findglfw3.cmake into PROJECT/cmake/Modules/:

# Copyright (c) 2015 Andrew Kelley
# This file is MIT licensed.
# See http://opensource.org/licenses/MIT

# GLFW_FOUND
# GLFW_INCLUDE_DIR
# GLFW_LIBRARY

find_path(GLFW_INCLUDE_DIR NAMES F:\\C++\\ExternalLibraries\\GLFW\\include\\GLFW\\glfw3.h)

find_library(GLFW_LIBRARY NAMES glfw glfw3)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GLFW DEFAULT_MSG GLFW_LIBRARY GLFW_INCLUDE_DIR)

mark_as_advanced(GLFW_INCLUDE_DIR GLFW_LIBRARY)

And added following lines into my CMakeLists.txt:

find_package(glfw3 REQUIRED)
include_directories(${glfw3_INCLUDE_DIRS})
set(LIBS ${LIBS} ${glfw3_LIBRARIES})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

target_link_libraries(hatsudouki_core ${LIBS})

I also tried in the Findglfw3.cmake:

find_path(GLFW_INCLUDE_DIR NAMES GLFW/glfw3.h)

which is the same in the original file. Both do not work; error:

"F:\C++\CLion 2018.1\bin\cmake\bin\cmake.exe" --build C:\Users\simon\CLionProjects\Hatsudouki-core\cmake-build-debug --target Hatsudouki_core -- -j 4
CMake Error at CMakeLists.txt:6 (find_package):
-- Configuring incomplete, errors occurred!
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
See also "C:/Users/simon/CLionProjects/Hatsudouki-core/cmake-build-debug/CMakeFiles/CMakeOutput.log".
asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.

Makefile:175: recipe for target 'cmake_check_build_system' failed
Could not find a package configuration file provided by "glfw3" with any of
the following names:

glfw3Config.cmake
glfw3-config.cmake

Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files.  If "glfw3"
provides a separate development package or SDK, be sure it has been
installed.
mingw32-make.exe: *** [cmake_check_build_system] Error 1
Simon Pio.
  • 115
  • 1
  • 14
  • C++ linking and Java are two *very* different things. Trying to compare them will only confuse you further. Your `CMakeLists.txt` hardwires paths; this should not be done. You should put `FindGLFW.cmake` to where CMake can find it (e.g. by adding the directory where you put it to `CMAKE_MODULE_PATH`), and then use the CMake command `find_package( GLFW )`. Generally speaking, your question is a lot of questions, and thus a bit hard to answer appropriately... – DevSolar Apr 10 '18 at 12:52
  • Your CMakeLists.txt file is missing `find_package(glfw3)` and in the `target_link_libraries` call you need to reference `glfw`. You might need to add the path to the glfw3 library to your `CMAKE_MODULE_PATH`. See http://www.glfw.org/docs/latest/build_guide.html#build_link_cmake_source for further information. – vre Apr 10 '18 at 13:05
  • Actually, the CMake code seems to be OK (it is not *perfect*, as it uses hardcoded paths instead of `find_package()`, but it should work). Error "undefined reference to 'glfwInit'" means that: 1. GLFW header files have been found. 2. GLFW library has been found. 3. The library doesn't contain `glfwInit` function suitable for given platform. In other words, the problem seems to be compatibility one. – Tsyvarev Apr 10 '18 at 13:23
  • @Tsyvarev so how can I fix this compatibility issue then or even find out if it was a compatibility issue? – Simon Pio. Apr 10 '18 at 16:47

2 Answers2

1

As explained here

  1. Make Findglfw3.cmake file in PROJECT/cmake/Modules/ which looks like

    # GLFW_FOUND
    # GLFW_INCLUDE_DIR
    # GLFW_LIBRARY
    
    set(FIND_GLFW_PATHS "F:\\C++\\ExternalLibraries\\GLFW")
    
    find_path(GLFW_INCLUDE_DIR NAMES GLFW/glfw3 GLFW/glfw3.h PATH_SUFFIXES include PATHS ${FIND_GLFW_PATHS})
    find_library(GLFW_LIBRARY NAMES glfw3 glfw3.a libglfw3 libglfw3.a PATH_SUFFIXES lib-mingw PATHS ${FIND_GLFW_PATHS})
    
    include(FindPackageHandleStandardArgs)
    find_package_handle_standard_args(GLFW DEFAULT_MSG GLFW_LIBRARY GLFW_INCLUDE_DIR)
    
    mark_as_advanced(GLFW_INCLUDE_DIR GLFW_LIBRARY)
    
  2. Define Module Path in CMakeLists.txt

    #Define module path
    list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules")
    
  3. Also Link include directory and library with project in CMakeLists.txt

    #Define static GLFW libraries and header files
    find_package(glfw3 REQUIRED)
    include_directories(${GLFW_INCLUDE_DIR})
    ...
    target_link_libraries(Hatsudouki_core ${GLFW_LIBRARY})
    
Simon Pio.
  • 115
  • 1
  • 14
0

At first you need to install glfw package. If you are using MSYS2 then it can be installed using pacman -S mingw-w64-x86_64-glfw on 64-bit windows and pacman -S mingw-w64-i686-glfw on 32-bit windows. And you need to use find_package to let the cmake locate the glfw library automatically instead of manually locating them. If you are not using MSYS2 then it might be little difficult. Check out this code.

cmake_minimum_required(VERSION 3.10)
project(Hatsudouki_core)

set(CMAKE_CXX_STANDARD 17)

add_executable(Hatsudouki_core main.cpp)

find_package(glfw3 REQUIRED) # Find the GLFW library
target_link_libraries(Hatsudouki_core PRIVATE glfw) # Finally, link to them.

I suggest you to use MSYS2 If you need pre-compiled libraries. It has a very good package manager called as pacman.

Mohit
  • 1,225
  • 11
  • 28
  • Thank you for your answer! I do not use or have MSYS2. Therefore, I tried the other suggestions in the comments. I thought there might be a solution without installing each library I want to use. I will try your suggestion if the other suggestions won't work. – Simon Pio. Apr 10 '18 at 14:45