'Cannot compile c++ file with glfw with conan on m1 mac
I tried to create a window in c++ with OpenGL and glfw with conan package manager on m1 mac. But I couldn't compile the source file.
Using librarys:
- OpenGL
- glfw
Using build tools:
- conan
- cmake
- ninja
main.cpp
#include <GLFW/glfw3.h>
#include <cstdlib>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <memory>
int main() {
if (glfwInit() == GLFW_FALSE) {
std::cout << "Cannot initialize GLFW" << std::endl;
return 1;
}
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
int width = 1260;
int height = 960;
std::string title = "opengl-demo";
GLFWwindow *const window(glfwCreateWindow(width, height, title.c_str(), NULL, NULL));
if (window == NULL) {
std::cout << "Cannot create GLFW Window!";
return 1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwShowWindow(window);
for (int i=0;i<1000000;i++){}
glfwTerminate();
glfwSetErrorCallback(NULL);
glfwDestroyWindow(window);
}
conanfile.txt
[requires]
glfw/3.3.6
[generators]
cmake
error message:
$ ninja
FAILED: bin/opengl-demo
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/opengl-demo.dir/src/main.cpp.o -o bin/opengl-demo -L/Users/*<user name>*/.conan/data/glfw/3.3.6/_/_/package/24b3a23034248276e5247366e59252156cd95f1e/lib && :
Undefined symbols for architecture arm64:
"_glfwCreateWindow", referenced from:
Window::Window(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in main.cpp.o
"_glfwDefaultWindowHints", referenced from:
_main in main.cpp.o
"_glfwDestroyWindow", referenced from:
Window::~Window() in main.cpp.o
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwMakeContextCurrent", referenced from:
Window::Window(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in main.cpp.o
"_glfwSetErrorCallback", referenced from:
_main in main.cpp.o
"_glfwSwapInterval", referenced from:
Window::Window(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in main.cpp.o
"_glfwTerminate", referenced from:
_main in main.cpp.o
"_glfwWindowHint", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(opengl-by-glfw)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_package(OpenGL REQUIRED)
add_executable(opengl-by-glfw src/main.cpp)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
repository: https://github.com/javaboy-github/opengl-by-glfw
I googled this issue but I don't know how to do it with conan. Please help me!
Edit: I changed CMakeLists.txt according to below comments.
cmake_minimum_required(VERSION 3.22)
project(opengl-by-glfw)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
add_definitions("-std=c++11")
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.16.1/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake"
EXPECTED_HASH SHA256=396e16d0f5eabdc6a14afddbcfff62a54a7ee75c6da23f32f7a31bc85db23484
TLS_VERIFY ON)
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_cmake_autodetect(settings)
conan_cmake_run(CONANFILE conanfile.txt
BASIC_SETUP CMAKE_TARGETS
BUILD missing
ARCH armv8
) # setting for m1 mac
find_package(OpenGL REQUIRED)
add_executable(opengl-by-glfw src/main.cpp)
target_link_libraries(opengl-by-glfw OpenGL::GL)
target_link_libraries(opengl-by-glfw CONAN_PKG::glfw)
target_link_libraries(opengl-by-glfw CONAN_PKG::glm)
But another error such as below happened.
FAILED: bin/opengl-by-glfw
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -O3 -DNDEBUG -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/opengl-by-glfw.dir/src/main.cpp.o -o bin/opengl-by-glfw /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/OpenGL.framework/OpenGL.tbd -lglfw3 -framework Cocoa -framework IOKit -framework CoreFoundation -framework OpenGL /Users/*<user name>*/.conan/data/glfw/3.3.6/_/_/package/24b3a23034248276e5247366e59252156cd95f1e/lib/libglfw3.a -framework Cocoa -framework IOKit -framework CoreFoundation -framework OpenGL && :
ld: library not found for -lglfw3
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
