CMake链接glfw3 lib错误 [英] CMake linking glfw3 lib error

查看:901
本文介绍了CMake链接glfw3 lib错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CLion,并且正在使用glfw3 lib编写程序.( http ://www.glfw.org/docs/latest/)

i'm working with CLion, and I'm writing a program using the glfw3 lib.(http://www.glfw.org/docs/latest/)

我为lib安装并正确完成了所有工作,我在.a和.h文件中:

I installed and did everything correctly for the lib i have the .a and .h files in:

/usr/local/lib/libglfw3.a

/usr/local/include/.h(all files)

我现在正在尝试使用该库,但出现链接器错误:

I'm trying to use the library now, but i'm getting the linker error:

undefined reference to 'glViewport'等.我正在使用的所有功能

undefined reference to 'glViewport' etc. etc. all the functions i'm using

我将lib路径添加到了make文件中,我听不懂我在做什么错,我的CMakeLists.txt看起来像这样:

I added the lib path to the make file, I can't understand what i'm doing wrong, my CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.6)
project(testing)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp examples.h)

add_executable(testing ${SOURCE_FILES})
target_link_libraries(testing /usr/local/lib/libglfw3.a)
target_link_libraries(testing /usr/local/lib/libTest.a)

target_link_libraries(testing pthread)

任何帮助将不胜感激.

谢谢!

推荐答案

您不应将绝对路径硬编码到CMake文件中.这使CMake无效.

You should not hard code absolute paths into your CMake files. This renders CMake useless.

在GLFW的文档中,,在那里明确地写着:

In the documentation of GLFW on how to link against it, there it is explicitly written:

find_package(glfw3 3.2 REQUIRED)
find_package(OpenGL REQUIRED)

target_include_directories(myapp PUBLIC ${OPENGL_INCLUDE_DIR})
target_link_libraries(myapp
                      glfw
                      ${OPENGL_gl_LIBRARY})


此外,您可以替换


Moreover, you can replace

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

使用

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

CMake会根据当前使用的编译器自动找出正确的编译器标志.

and CMake will figure out the correct compiler flag automatically depending on the compiler currently in use.

这篇关于CMake链接glfw3 lib错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆