1

I am trying to test out OpenGL on a mac. I am not able to build a hello world project.

project structure:

➜ tree
.
├── CMakeLists.txt
└── main.cpp

0 directories, 2 files

main.cpp

#include <iostream>

// GLEW
#define GLEW_STATIC
#include <GL/glew.h>

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


const GLint WIDTH = 800, HEIGHT = 600;


int main(void){
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "title", nullptr, nullptr);

    int screenWidth, screenHeight;

    // for mac
    glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

    if ( nullptr == window)
    {
        std::cout << "failed to create glfw window" << std::endl;
        glfwTerminate();

        return -1;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;

    if ( GLEW_OK != glewInit() )
    {
        std::cout << "Failed to initialize GLEW" << std::endl;

        return -1;
    }

    glViewport( 0, 0, screenWidth, screenHeight);

    while ( !glfwWindowShouldClose( window) )
    {
        // input from usr
        // processing loop

        glfwPollEvents();
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear( GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers( window);

    }
    glfwTerminate();

    return 0;
}

CMakeList.txt

cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 14)


project(testas)

# Add source files
file(GLOB_RECURSE SOURCE_FILES
        ${CMAKE_SOURCE_DIR}/*.c
        ${CMAKE_SOURCE_DIR}/*.cpp)

# Add header files
file(GLOB_RECURSE HEADER_FILES
        ${CMAKE_SOURCE_DIR}/*.h
        ${CMAKE_SOURCE_DIR}/*.hpp)



add_executable(myopengl
    ${SOURCE_FILES}
    ${HEADER_FILES}
    )



####### DEPENDENCIES

# OpenGL

find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
target_link_libraries(myopengl ${OPENGL_LIBRARIES})
message(STATUS "OPENGL_INCLUDE_DIR at ${OPENGL_INCLUDE_DIR} FOUND")
message(STATUS "OPENGL_LIBRARIES at ${OPENGL_LIBRARIES} FOUND")

# PkfConfig

find_package(PkgConfig REQUIRED)
message(STATUS "PkgConfig FOUND")

# GLFW

pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
target_link_libraries(myopengl ${GLFW_LIBRARIES})
message(STATUS "GLFW_INCLUDE_DIRS at ${GLFW_INCLUDE_DIRS} FOUND")
message(STATUS "GLFW_LIBRARIES at ${GLFW_LIBRARIES} FOUND")

# GLEW

pkg_search_module(GLEW REQUIRED glew)
include_directories(${GLEW_INCLUDE_DIRS})
target_link_libraries(myopengl ${GLEW_LIBRARIES})
message(STATUS "GLEW_INCLUDE_DIRS at ${GLEW_INCLUDE_DIRS} FOUND")
message(STATUS "GLEW_LIBRARIES at ${GLEW_LIBRARIES} FOUND")

# GLM

pkg_search_module(GLM REQUIRED glm)
include_directories(${GLM_INCLUDE_DIRS})
target_link_libraries(myopengl ${GLM_LIBRARIES})
message(STATUS "GLM_INCLUDE_DIRS at ${GLM_INCLUDE_DIRS} FOUND")

execution of cmake .

➜ cmake .
-- OPENGL_INCLUDE_DIR at /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/OpenGL.framework FOUND
-- OPENGL_LIBRARIES at /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/OpenGL.framework;/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/OpenGL.framework FOUND
-- PkgConfig FOUND
-- GLFW_INCLUDE_DIRS at /usr/local/Cellar/glfw/3.3.2/include FOUND
-- GLFW_LIBRARIES at glfw FOUND
-- GLEW_INCLUDE_DIRS at /usr/local/Cellar/glew/2.1.0_1/include FOUND
-- GLEW_LIBRARIES at GLEW FOUND
-- GLM_INCLUDE_DIRS at /usr/local/Cellar/glm/0.9.9.5/include FOUND
-- Configuring done
-- Generating done
-- Build files have been written to: /var/folders/48/0ffynlh529g4zx1_sh37gbtc0000gp/T/my-temp-XXXXXXXXXX.H1AsbU6N

and make error:

➜ make
[ 25%] Linking CXX executable myopengl
ld: library not found for -lglfw
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [myopengl] Error 1
make[1]: *** [CMakeFiles/myopengl.dir/all] Error 2
make: *** [all] Error 2

pkg-config output:

➜ pkg-config --list-all | grep -i opengl
glfw3                 GLFW - A multi-platform library for OpenGL, window and input
glew                  glew - The OpenGL Extension Wrangler library
sdl2                  sdl2 - Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer.
glm                   GLM - OpenGL Mathematics

Unfortunately I don't understand that output. It makes a confusion - the libs are being found, but somehow linker is not able to attach stuff. All of the libraries were installed with the brew command (if it's important), and are brew link'ed.

Spec:

macOS Catalina 10.15.4
cmake version 3.17.1
Apple clang version 11.0.3 (clang-1103.0.32.59)
Kevin
  • 16,549
  • 8
  • 60
  • 74
MixedOne
  • 11
  • 2

1 Answers1

1

While the packages are found, the linker does not know where these libraries reside on your machine. If the libraries are not in the standard system library path, you need to provide the full path to the libraries explicitly.

The pkg_search_module() should provide a XXX_LIBRARY_DIRS variable for each XXX package that was found, which provides the paths to the library files. So, for example, for the GLFW package, you can tell the linker where to find the libraries with target_link_directories():

pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
# Add this line to tell the linker where to find the libraries.
target_link_directories(${GLFW_LIBRARY_DIRS})
target_link_libraries(myopengl ${GLFW_LIBRARIES})
message(STATUS "GLFW_INCLUDE_DIRS at ${GLFW_INCLUDE_DIRS} FOUND")
message(STATUS "GLFW_LIBRARIES at ${GLFW_LIBRARIES} FOUND")
Kevin
  • 16,549
  • 8
  • 60
  • 74