Как связать библиотеку stati c GLEW с проектом CMake с использованием MinGW? - PullRequest
0 голосов
/ 23 апреля 2020

Я пытаюсь сделать простой проект треугольника для изучения OpenGL, но я застрял в ошибке компиляции:

GLFWwindow* initApp() {

  if (!glfwInit()) {
    std::cerr << "Failed to create GLFW context" << std::endl;
    return nullptr;
  }

  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  GLFWwindow* window = glfwCreateWindow(1280, 720, "TriangleApp", nullptr, nullptr);
  if (window == nullptr) {
    std::cerr << "Failed to open GLFW window" << std::endl;
    glfwTerminate();
    return nullptr;
  }

  glfwMakeContextCurrent(window);
  glewExperimental = true;
  if (!glewInit()) {
    std::cerr << "Failed to initialize GL libraries" << std::endl;
    return nullptr;
  }

  return window;
}

CMake говорит мне, что есть неопределенная ссылка на glewInit и glewExperimental. Я включил glew.h перед любым заголовком OpenGL, определил GLEW_STATIC в моем проекте CMake и связал glfw3.a и glew32s.lib с целью, используя CMakeLists.txt:

cmake_minimum_required(VERSION 3.17)

project(TriangleApp)

# GLEW
add_compile_definitions(GLEW_STATIC)
include_directories(libraries/glew/include)
set(GLEW_LINK_LIBRARY ${CMAKE_SOURCE_DIR}/libraries/glew/lib/Release/Win32/glew32s.lib)

# GLFW
include_directories(libraries/glfw/include)
set(GLFW_LINK_LIBRARY ${CMAKE_SOURCE_DIR}/libraries/glfw/lib/libglfw3.a)

# My App
include_directories(headers)
add_executable(TriangleApp source/Main.cpp)

target_link_libraries(TriangleApp
  ${GLEW_LINK_LIBRARY}
  ${GLFW_LINK_LIBRARY})

Вот вывод из Расширение CMake Tasks (vscode):

[main] Building folder: TriangleApp 
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/benjo/source/repos/windows/C++/OpenGL/TriangleApp/build --config Debug --target all -- -j 10
[build] Scanning dependencies of target TriangleApp
[build] [ 50%] Building CXX object CMakeFiles/TriangleApp.dir/source/Main.cpp.obj
[build] [100%] Linking CXX executable TriangleApp.exe
[build] CMakeFiles\TriangleApp.dir/objects.a(Main.cpp.obj): In function `initApp()':
[build] C:/Users/benjo/source/repos/windows/C++/OpenGL/TriangleApp/source/Main.cpp:27: undefined reference to `glewInit'
[build] CMakeFiles\TriangleApp.dir/objects.a(Main.cpp.obj):Main.cpp:(.rdata$.refptr.glewExperimental[.refptr.glewExperimental]+0x0): undefined reference to `glewExperimental'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\TriangleApp.dir\build.make:108: TriangleApp.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:95: CMakeFiles/TriangleApp.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:103: all] Error 2
[build] Build finished with exit code 2
...